PHP-cli-factorio-updater/scripts/class.FactorioVersion.php

106 lines
3.0 KiB
PHP
Raw Normal View History

2024-11-10 03:40:05 +01:00
<?php
/**
* Namespace for classes and functions related to the game Factorio.
* @package NAE\Factorio
*/
2024-11-10 03:40:05 +01:00
namespace NAE\Factorio;
/**
* Class for parsing and comparing Factorio versions.
* @author "Marcel Naeve" <php@naeve.info>
* @license MIT License (http://www.opensource.org/licenses/mit)
*/
2024-11-10 03:40:05 +01:00
class FactorioVersion {
/**
* @var string The Factorio Version
*/
2024-11-10 03:40:05 +01:00
private $version;
/**
* @var int The Factorio version converted to an comparable integer.
*/
2024-11-10 03:40:05 +01:00
private $intVersion;
/**
* Fills a string with zeros at the beginning till string length matches the target length.
* @param string $str string to fill
* @param int $target_length Target length of the string
* @return string string with zeros at the beginning till string length matches
2024-11-10 03:40:05 +01:00
*/
private function zero_fill(string $str, int $target_length) {
if(strlen($str) >= $target_length) {
return $str;
}
$missing = ( $target_length - strlen($str) );
for($i=0;$i<$target_length;$i++) {
$str = "0".$str;
}
return $str;
}
/**
* Formats the Factorio version string to a comparable integer.
* @param string $vs Factorio version string (example: 2.0.14)
* @return int factorio version as comparable integer (example: versionFormat("2.0.14") -> "000200000014" -> int(200000014))
2024-11-10 03:40:05 +01:00
*/
private function versionFormat(string $vs) : int {
$parts = explode(".", $vs);
$new = "";
foreach($parts as $i => $v) {
$new .= $this->zero_fill($v, 4);
}
return intval($new);
}
/**
* Constructor.
* @param string $version Factorio version string (example: 2.0.14)
*/
2024-11-10 03:40:05 +01:00
public function __construct(string $version) {
$this->version = $version;
$this->intVersion = $this->versionFormat($version);
}
/**
* Returns the Factorio version string (example: 2.0.14).
* @return string Factorio version string (example: 2.0.14)
*/
2024-11-10 03:40:05 +01:00
public function getVersion() : string {
return $this->version;
}
/**
* Returns the Factorio version as a comparable integer (example: versionFormat("2.0.14") -> int(200000014)).
* @return integer Factorio version as a comparable integer (example: versionFormat("2.0.14") -> int(200000014)).
*/
2024-11-10 03:40:05 +01:00
public function getIntVersion() : int {
return $this->intVersion;
}
/**
* Checks if the current Factorio version is newer than another version.
* @param FactorioVersion $other Another FactorioVersion object
*/
2024-11-10 03:40:05 +01:00
public function isNewer(FactorioVersion $other) : bool {
return $this->intVersion > $other->getIntVersion();
}
/**
* Checks if the current Factorio version is older than another version.
* @param FactorioVersion $other Another FactorioVersion object
*/
2024-11-10 03:40:05 +01:00
public function isOlder(FactorioVersion $other) : bool {
return $this->intVersion < $other->getIntVersion();
}
}