|
| 1 | +<?php |
| 2 | +class version { |
| 3 | + static protected $global_single_version='(([0-9]+)(\\.([0-9]+)(\\.([0-9]+))?)?)'; |
| 4 | + static protected $global_single_xrange='(([0-9xX*]+)(\\.([0-9xX*]+)(\\.([0-9xX*]+))?)?)'; |
| 5 | + static protected $global_single_comparator='([<>]=?)?\\s*'; |
| 6 | + static protected $range_mask='%1$s\\s+-\\s+%1$s'; |
| 7 | + static protected $regexp_mask='/%s/'; |
| 8 | + private $chunks=array(); |
| 9 | + /** |
| 10 | + * standarizes the comparator/range/whatever-string to chunks |
| 11 | + * Enter description here ... |
| 12 | + * @param unknown_type $versions |
| 13 | + */ |
| 14 | + function __construct($versions) { |
| 15 | + $versions=preg_replace('/'.self::$global_single_comparator.'(\\s+-\\s+)?'.self::$global_single_xrange.'/','$1$2$3',$versions); //Paste comparator and version together |
| 16 | + $versions=preg_replace('/\\s+/', ' ', $versions); //Condense multiple spaces to one |
| 17 | + if(strstr($versions, '-')) $versions=self::rangesToComparators($versions); //Replace all ranges with comparators |
| 18 | + if(strstr($versions, 'x')||strstr($versions,'X')||strstr($versions,'*')) $versions=self::xRangesToComparators($versions); //Replace all x-ranges with comparators |
| 19 | + $or=explode('||', $versions); |
| 20 | + foreach($or as &$orchunk) { |
| 21 | + $orchunk=trim($orchunk); //Remove spaces |
| 22 | + $and=explode(' ', $orchunk); |
| 23 | + foreach($and as &$achunk) { |
| 24 | + $achunk=self::standarizeSingleComparator($achunk); |
| 25 | + } |
| 26 | + $orchunk=$and; |
| 27 | + } |
| 28 | + $this->chunks=$or; |
| 29 | + } |
| 30 | + /** |
| 31 | + * Get the whole or object as a string |
| 32 | + * Enter description here ... |
| 33 | + */ |
| 34 | + function getString() { |
| 35 | + $or=$this->chunks; |
| 36 | + foreach($or as &$orchunk) { |
| 37 | + $orchunk=implode(' ',$orchunk); |
| 38 | + } |
| 39 | + return implode('||', $or); |
| 40 | + } |
| 41 | + function __toString() { |
| 42 | + return $this->getString(); |
| 43 | + } |
| 44 | + /** |
| 45 | + * standarizes a single version |
| 46 | + * @param string $version |
| 47 | + * @param bool $hasComparator Set to true if the version string has a comparator in front of it |
| 48 | + * @throws versionException |
| 49 | + * @return string |
| 50 | + */ |
| 51 | + static function standarize($version,$hasComparator=false) { |
| 52 | + $matches=array(); |
| 53 | + $expression=sprintf(self::$regexp_mask,self::$global_single_version); |
| 54 | + if(!preg_match($expression,$version,$matches)) throw new versionException('Invalid version string given'); |
| 55 | + if($hasComparator) { //If there is a comparator set undefined parts to 0 |
| 56 | + self::matchesToVersionParts($matches, $major, $minor, $patch); |
| 57 | + return $major.'.'.$minor.'.'.$patch; |
| 58 | + } |
| 59 | + else { //If it is just a number, convert to a range |
| 60 | + self::matchesToVersionParts($matches, $major, $minor, $patch, 'x'); |
| 61 | + $version=$major.'.'.$minor.'.'.$patch; |
| 62 | + return self::xRangesToComparators($version); |
| 63 | + } |
| 64 | + } |
| 65 | + /** |
| 66 | + * standarizes a single version with comparators |
| 67 | + * @param string $version |
| 68 | + * @throws versionException |
| 69 | + * @return string |
| 70 | + */ |
| 71 | + static private function standarizeSingleComparator($version) { |
| 72 | + $expression=sprintf(self::$regexp_mask,self::$global_single_comparator.self::$global_single_version); |
| 73 | + $matches=array(); |
| 74 | + if(!preg_match($expression,$version,$matches)) throw new versionException('Invalid version string given'); |
| 75 | + $comparators=$matches[1]; |
| 76 | + $version=$matches[2]; |
| 77 | + $hasComparators=true; |
| 78 | + if($comparators=='') $hasComparators=false; |
| 79 | + $version=self::standarize($version, $hasComparators); |
| 80 | + return $comparators.$version; |
| 81 | + } |
| 82 | + /** |
| 83 | + * standarizes a bunch of versions with comparators |
| 84 | + * @param string $versions |
| 85 | + * @return string |
| 86 | + */ |
| 87 | + static private function standarizeMultipleComparators($versions) { |
| 88 | + $versions=preg_replace('/'.self::$global_single_comparator.self::$global_single_xrange.'/','$1$2',$versions); //Paste comparator and version together |
| 89 | + $versions=preg_replace('/\\s+/', ' ', $versions); //Condense multiple spaces to one |
| 90 | + $or=explode('||', $versions); |
| 91 | + foreach($or as &$orchunk) { |
| 92 | + $orchunk=trim($orchunk); //Remove spaces |
| 93 | + $and=explode(' ', $orchunk); |
| 94 | + foreach($and as &$achunk) { |
| 95 | + $achunk=self::standarizeSingleComparator($achunk); |
| 96 | + } |
| 97 | + $orchunk=implode(' ',$and); |
| 98 | + } |
| 99 | + $versions=implode('||',$or); |
| 100 | + return $versions; |
| 101 | + } |
| 102 | + /** |
| 103 | + * standarizes a bunch of version ranges to comparators |
| 104 | + * @param string $range |
| 105 | + * @throws versionException |
| 106 | + * @return string |
| 107 | + */ |
| 108 | + static private function rangesToComparators($range) { |
| 109 | + $range_expression=sprintf(self::$range_mask,self::$global_single_version); |
| 110 | + $expression=sprintf(self::$regexp_mask,$range_expression); |
| 111 | + if(!preg_match($expression,$range)) throw new versionException('Invalid range given'); |
| 112 | + $versions=preg_replace($expression, '>=$1 <$7', $range); |
| 113 | + $versions=self::standarizeMultipleComparators($versions); |
| 114 | + return $versions; |
| 115 | + } |
| 116 | + /** |
| 117 | + * standarizes a bunch of x-ranges to comparators |
| 118 | + * @param string $ranges |
| 119 | + * @return string |
| 120 | + */ |
| 121 | + static private function xRangesToComparators($ranges) { |
| 122 | + $expression=sprintf(self::$regexp_mask,self::$global_single_xrange); |
| 123 | + return preg_replace_callback($expression, array('self','xRangesToComparatorsCallback'), $ranges); |
| 124 | + } |
| 125 | + /** |
| 126 | + * Callback for xRangesToComparators() |
| 127 | + * @internal |
| 128 | + * @param array $matches |
| 129 | + */ |
| 130 | + static private function xRangesToComparatorsCallback($matches) { |
| 131 | + self::matchesToVersionParts($matches, $major, $minor, $patch); |
| 132 | + $wildcards=array('x','X','*'); |
| 133 | + if(in_array($major, $wildcards,true)) return '>=0.0.0'; |
| 134 | + if(in_array($minor, $wildcards,true)) return '>='.$major.'.0.0 <'.($major+1).'.0.0'; |
| 135 | + if(in_array($patch, $wildcards,true)) return '>='.$major.'.'.$minor.'.0 <'.$major.'.'.($minor+1).'.0'; |
| 136 | + return $major.'.'.$minor.'.'.$patch; |
| 137 | + } |
| 138 | + /** |
| 139 | + * Converts matches to named version parts |
| 140 | + * @param array $matches |
| 141 | + * @param string $major |
| 142 | + * @param string $minor |
| 143 | + * @param string $patch |
| 144 | + * @param string $default |
| 145 | + * @param int $offset |
| 146 | + */ |
| 147 | + static private function matchesToVersionParts($matches, &$major, &$minor, &$patch, $default=0, $offset=2) { |
| 148 | + $major=$minor=$patch=$default; |
| 149 | + switch(count($matches)) { |
| 150 | + case $offset+5: $patch=$matches[$offset+4]; |
| 151 | + case $offset+3: $minor=$matches[$offset+2]; |
| 152 | + case $offset+1: $major=$matches[$offset]; |
| 153 | + } |
| 154 | + if(is_numeric($patch)) $patch=intval($patch); |
| 155 | + if(is_numeric($minor)) $minor=intval($minor); |
| 156 | + if(is_numeric($major)) $major=intval($major); |
| 157 | + } |
| 158 | +} |
| 159 | +class versionException extends Exception {} |
0 commit comments