123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- <?php
- namespace WhichBrowser\Model;
- use WhichBrowser\Model\Primitive\Base;
- class Version extends Base
- {
- /** @var string|null */
- public $value = null;
- /** @var boolean */
- public $hidden = false;
- /** @var string */
- public $nickname;
- /** @var string */
- public $alias;
- /** @var int */
- public $details;
- /** @var boolean */
- public $builds;
- /**
- * Determine if the version is lower, equal or higher than the specified value
- *
- * @param string The operator, must be <, <=, =, >= or >
- * @param mixed The value, can be an integer, float or string with a version number
- *
- * @return boolean
- */
- public function is()
- {
- $valid = false;
- $arguments = func_get_args();
- if (count($arguments)) {
- $operator = '=';
- $compare = null;
- if (count($arguments) == 1) {
- $compare = $arguments[0];
- }
- if (count($arguments) >= 2) {
- $operator = $arguments[0];
- $compare = $arguments[1];
- }
- if (!is_null($compare)) {
- $min = min(substr_count($this->value, '.'), substr_count($compare, '.')) + 1;
- $v1 = $this->toValue($this->value, $min);
- $v2 = $this->toValue($compare, $min);
- switch ($operator) {
- case '<':
- $valid = $v1 < $v2;
- break;
- case '<=':
- $valid = $v1 <= $v2;
- break;
- case '=':
- $valid = $v1 == $v2;
- break;
- case '>':
- $valid = $v1 > $v2;
- break;
- case '>=':
- $valid = $v1 >= $v2;
- break;
- }
- }
- }
- return $valid;
- }
- /**
- * Return an object with each part of the version number
- *
- * @return object
- */
- public function getParts()
- {
- $parts = explode('.', $this->value);
- return (object) [
- 'major' => !empty($parts[0]) ? intval($parts[0]) : 0,
- 'minor' => !empty($parts[1]) ? intval($parts[1]) : 0,
- 'patch' => !empty($parts[2]) ? intval($parts[2]) : 0,
- ];
- }
- /**
- * Return the major version as an integer
- *
- * @return integer
- */
- public function getMajor()
- {
- return $this->getParts()->major;
- }
- /**
- * Return the minor version as an integer
- *
- * @return integer
- */
- public function getMinor()
- {
- return $this->getParts()->minor;
- }
- /**
- * Return the patch number as an integer
- *
- * @return integer
- */
- public function getPatch()
- {
- return $this->getParts()->patch;
- }
- /**
- * Convert a version string seperated by dots into a float that can be compared
- *
- * @internal
- *
- * @param string Version string, with elements seperated by a dot
- * @param int The maximum precision
- *
- * @return float
- */
- private function toValue($value = null, $count = null)
- {
- if (is_null($value)) {
- $value = $this->value;
- }
- $parts = explode('.', $value);
- if (!is_null($count)) {
- $parts = array_slice($parts, 0, $count);
- }
- $result = $parts[0];
- if (count($parts) > 1) {
- $result .= '.';
- $count = count($parts);
- for ($p = 1; $p < $count; $p++) {
- $result .= substr('0000' . $parts[$p], -4);
- }
- }
- return floatval($result);
- }
- /**
- * Return the version as a float
- *
- * @return float
- */
- public function toFloat()
- {
- return floatval($this->value);
- }
- /**
- * Return the version as an integer
- *
- * @return int
- */
- public function toNumber()
- {
- return intval($this->value);
- }
- /**
- * Return the version as a human readable string
- *
- * @return string
- */
- public function toString()
- {
- if (!empty($this->alias)) {
- return $this->alias;
- }
- $version = '';
- if (!empty($this->nickname)) {
- $version .= $this->nickname . ' ';
- }
- if (!empty($this->value)) {
- if (preg_match("/([0-9]+)(?:\.([0-9]+))?(?:\.([0-9]+))?(?:\.([0-9]+))?(?:([ab])([0-9]+))?/", $this->value, $match)) {
- $v = [ $match[1] ];
- if (array_key_exists(2, $match) && strlen($match[2])) {
- $v[] = $match[2];
- }
- if (array_key_exists(3, $match) && strlen($match[3])) {
- $v[] = $match[3];
- }
- if (array_key_exists(4, $match) && strlen($match[4])) {
- $v[] = $match[4];
- }
- if (!empty($this->details)) {
- if ($this->details < 0) {
- array_splice($v, $this->details, 0 - $this->details);
- }
- if ($this->details > 0) {
- array_splice($v, $this->details, count($v) - $this->details);
- }
- }
- if (isset($this->builds) && !$this->builds) {
- $count = count($v);
- for ($i = 0; $i < $count; $i++) {
- if ($v[$i] > 999) {
- array_splice($v, $i, 1);
- }
- }
- }
- $version .= implode($v, '.');
- if (array_key_exists(5, $match) && strlen($match[5])) {
- $version .= $match[5] . (!empty($match[6]) ? $match[6] : '');
- }
- }
- }
- return $version;
- }
- /**
- * Get an array of all defined properties
- *
- * @internal
- *
- * @return array
- */
- public function toArray()
- {
- $result = [];
- if (!empty($this->value)) {
- if (!empty($this->details)) {
- $parts = explode('.', $this->value);
- $result['value'] = join('.', array_slice($parts, 0, $this->details));
- } else {
- $result['value'] = $this->value;
- }
- }
- if (!empty($this->alias)) {
- $result['alias'] = $this->alias;
- }
- if (!empty($this->nickname)) {
- $result['nickname'] = $this->nickname;
- }
- if (isset($result['value']) && !isset($result['alias']) && !isset($result['nickname'])) {
- return $result['value'];
- }
- return $result;
- }
- }
|