72 lines
1.8 KiB
PHP
72 lines
1.8 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace NAE\Factorio;
|
||
|
|
||
|
|
||
|
class FactorioVersion {
|
||
|
|
||
|
private $version;
|
||
|
private $intVersion;
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Füllt eine Zeichenkette am anfang mit Nullen auf bis die Ziellänge erreicht ist.
|
||
|
* @param string Zu füllende Zeichenkette
|
||
|
* @param int Ziellänge der gefüllten Zeichenkette
|
||
|
* @return string die Zeichenkette mit Nullen vorne angefügt um die Ziellänge zu erreichen
|
||
|
*/
|
||
|
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;
|
||
|
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Formatiert die Factorio Versionsnummern um zu einem Vergleichbaren Integer.
|
||
|
* @param string Factorio Versions Zeichenkette Beispiel: 2.0.14
|
||
|
* @return int zum einfachen Vergleich formatierter Versions-Integer. Beispiel: versionsFormat("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);
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
public function __construct(string $version) {
|
||
|
$this->version = $version;
|
||
|
$this->intVersion = $this->versionFormat($version);
|
||
|
}
|
||
|
|
||
|
public function getVersion() : string {
|
||
|
return $this->version;
|
||
|
}
|
||
|
|
||
|
public function getIntVersion() : int {
|
||
|
return $this->intVersion;
|
||
|
}
|
||
|
|
||
|
public function isNewer(FactorioVersion $other) : bool {
|
||
|
return $this->intVersion > $other->getIntVersion();
|
||
|
}
|
||
|
|
||
|
public function isOlder(FactorioVersion $other) : bool {
|
||
|
return $this->intVersion < $other->getIntVersion();
|
||
|
}
|
||
|
|
||
|
}
|