|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Version; |
| 4 | + |
| 5 | +use Version\Constraint\AnythingConstraint; |
| 6 | +use Version\Constraint\MultiConstraint; |
| 7 | +use Version\Constraint\SimpleConstraint; |
| 8 | + |
| 9 | +abstract class Constraint |
| 10 | +{ |
| 11 | + public abstract function matches(Constraint $constraint); |
| 12 | + |
| 13 | + /** |
| 14 | + * @param string $input |
| 15 | + * @return Constraint |
| 16 | + */ |
| 17 | + public static function parse($input) |
| 18 | + { |
| 19 | + $input = trim($input); |
| 20 | + |
| 21 | + if (strlen($input) == 0) { |
| 22 | + throw new \UnexpectedValueException('Empty.'); |
| 23 | + } |
| 24 | + |
| 25 | + $input = explode(',', $input); |
| 26 | + if (count($input) > 1) { |
| 27 | + $and = true; |
| 28 | + } else { |
| 29 | + $input = explode('|', $input[0]); |
| 30 | + $and = false; |
| 31 | + } |
| 32 | + |
| 33 | + if (count($input) > 1) { |
| 34 | + $constraints = array(); |
| 35 | + foreach ($input as $constraint) { |
| 36 | + $constraints[] = self::parse($constraint); |
| 37 | + } |
| 38 | + return new MultiConstraint($constraints, $and); |
| 39 | + } |
| 40 | + |
| 41 | + $input = $input[0]; |
| 42 | + |
| 43 | + $regex = '/^' . |
| 44 | + '(?:([\*|x])\.)?' . |
| 45 | + '(?:([\*|x])\.)?' . |
| 46 | + '(?:([\*|x])\.)?' . |
| 47 | + '(?:([\*|x]))?' . |
| 48 | + '$/'; |
| 49 | + |
| 50 | + if (preg_match($regex, $input, $matches)) { |
| 51 | + return new AnythingConstraint(); |
| 52 | + } |
| 53 | + |
| 54 | + $regex = '/^' . |
| 55 | + '(?:(' . Operator::REGEX . '))? *' . |
| 56 | + '(?:(\d+|\*|x)\.)?' . |
| 57 | + '(?:(\d+|\*|x)\.)?' . |
| 58 | + '(?:(\d+|\*|x)\.)?' . |
| 59 | + '(?:(\d+|\*|x))?' . |
| 60 | + '(?:' . Stability::REGEX . ')?' . |
| 61 | + '$/'; |
| 62 | + |
| 63 | + if(!preg_match($regex, $input, $matches)) { |
| 64 | + throw new \UnexpectedValueException('Invalid type: ' . $input); |
| 65 | + } |
| 66 | + |
| 67 | + if (isset($matches[1]) && strlen($matches[1]) > 0) { |
| 68 | + $operator = $matches[1]; |
| 69 | + } else { |
| 70 | + $operator = '='; |
| 71 | + } |
| 72 | + $operator = new Operator($operator); |
| 73 | + |
| 74 | + $parts = array(); |
| 75 | + |
| 76 | + if (isset($matches[2]) && strlen($matches[2]) > 0) $parts[] = $matches[2]; |
| 77 | + if (isset($matches[3]) && strlen($matches[3]) > 0) $parts[] = $matches[3]; |
| 78 | + if (isset($matches[4]) && strlen($matches[4]) > 0) $parts[] = $matches[4]; |
| 79 | + if (isset($matches[5]) && strlen($matches[5]) > 0) $parts[] = $matches[5]; |
| 80 | + |
| 81 | + if ((string)$operator == '~') { |
| 82 | + $end = count($parts); |
| 83 | + } else { |
| 84 | + $end = null; |
| 85 | + } |
| 86 | + |
| 87 | + while (count($parts) < 4) { |
| 88 | + $parts[] = 0; |
| 89 | + } |
| 90 | + |
| 91 | + $max = $parts; |
| 92 | + |
| 93 | + if ($end) { |
| 94 | + if ($end == 1) { |
| 95 | + $max[0]++; |
| 96 | + } elseif ($end == 2) { |
| 97 | + $max[0]++; |
| 98 | + $max[1] = 0; |
| 99 | + } elseif ($end == 3) { |
| 100 | + $max[1]++; |
| 101 | + $max[2] = 0; |
| 102 | + } elseif ($end == 4) { |
| 103 | + $max[2]++; |
| 104 | + $max[3] = 0; |
| 105 | + } else { |
| 106 | + echo $end; |
| 107 | + die($end); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if ($parts[3] === 'x' || $parts[3] === '*') { |
| 112 | + $parts[3] = 0; |
| 113 | + $max[3] = 0; |
| 114 | + $max[2]++; |
| 115 | + } |
| 116 | + |
| 117 | + if ($parts[2] === 'x' || $parts[2] === '*') { |
| 118 | + $parts[2] = 0; |
| 119 | + $max[2] = 0; |
| 120 | + $max[1]++; |
| 121 | + } |
| 122 | + |
| 123 | + if ($parts[1] === 'x' || $parts[1] === '*') { |
| 124 | + $parts[1] = 0; |
| 125 | + $max[1] = 0; |
| 126 | + $max[0]++; |
| 127 | + } |
| 128 | + |
| 129 | + $version = new Version($parts[0]); |
| 130 | + if (isset($parts[1])) $version->setMinor($parts[1]); |
| 131 | + if (isset($parts[2])) $version->setRevision($parts[2]); |
| 132 | + if (isset($parts[3])) $version->setMicro($parts[3]); |
| 133 | + |
| 134 | + if (isset($matches[6]) && strlen($matches[6]) > 0) { |
| 135 | + if (strtolower($matches[5]) == 'rc') { |
| 136 | + $stability = 'RC'; |
| 137 | + } elseif (in_array(strtolower($matches[6]), array('pl', 'patch', 'p'))) { |
| 138 | + $stability = 'patch'; |
| 139 | + } elseif (in_array(strtolower($matches[6]), array('beta', 'b'))) { |
| 140 | + $stability = 'beta'; |
| 141 | + } elseif (strtolower($matches[6]) == 'stable') { |
| 142 | + $stability = 'stable'; |
| 143 | + } else { |
| 144 | + throw new \UnexpectedValueException('Invalid type: ' . $input); |
| 145 | + } |
| 146 | + $version->setStability(new Stability($stability, $matches[7])); |
| 147 | + } |
| 148 | + |
| 149 | + foreach ($parts as $k => $v) { |
| 150 | + if ($v != $max[$k]) { |
| 151 | + if ($input == '<=1.2.3') { |
| 152 | + print_r($parts); |
| 153 | + print_r($max); |
| 154 | + print_r(array_diff($parts, $max)); |
| 155 | + die; |
| 156 | + } |
| 157 | + $maxVersion = new Version($max[0]); |
| 158 | + if (isset($max[1])) $maxVersion->setMinor($max[1]); |
| 159 | + if (isset($max[2])) $maxVersion->setRevision($max[2]); |
| 160 | + if (isset($max[3])) $maxVersion->setMicro($max[3]); |
| 161 | + |
| 162 | + if ((string)$version == '0.0.0.0') { |
| 163 | + return new SimpleConstraint(new Operator('<'), $maxVersion); |
| 164 | + } |
| 165 | + if (isset($matches[6]) && strtolower($matches[6]) == 'stable') { |
| 166 | + $version->setStability(new Stability()); |
| 167 | + } |
| 168 | + return new MultiConstraint(array( |
| 169 | + new SimpleConstraint(new Operator('>='), $version), |
| 170 | + new SimpleConstraint(new Operator('<'), $maxVersion) |
| 171 | + )); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + return new SimpleConstraint($operator, $version); |
| 176 | + } |
| 177 | + |
| 178 | + abstract public function isSubsetOf(Constraint $constraint); |
| 179 | + |
| 180 | + public function isIncluding(Constraint $constraint) |
| 181 | + { |
| 182 | + return $constraint->isSubsetOf($this); |
| 183 | + } |
| 184 | +} |
0 commit comments