106 lines
3.0 KiB
PHP
106 lines
3.0 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Namespace for classes and functions related to the game Factorio.
|
|
* @package NAE\Factorio
|
|
*/
|
|
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)
|
|
*/
|
|
class FactorioVersion {
|
|
|
|
/**
|
|
* @var string The Factorio Version
|
|
*/
|
|
private $version;
|
|
|
|
/**
|
|
* @var int The Factorio version converted to an comparable integer.
|
|
*/
|
|
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
|
|
*/
|
|
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))
|
|
*/
|
|
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)
|
|
*/
|
|
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)
|
|
*/
|
|
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)).
|
|
*/
|
|
public function getIntVersion() : int {
|
|
return $this->intVersion;
|
|
}
|
|
|
|
/**
|
|
* Checks if the current Factorio version is newer than another version.
|
|
* @param FactorioVersion $other Another FactorioVersion object
|
|
*/
|
|
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
|
|
*/
|
|
public function isOlder(FactorioVersion $other) : bool {
|
|
return $this->intVersion < $other->getIntVersion();
|
|
}
|
|
|
|
} |