comparison vendor/phar-io/version/src/VersionConstraintParser.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents
children
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
1 <?php
2 /*
3 * This file is part of PharIo\Version.
4 *
5 * (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11 namespace PharIo\Version;
12
13 class VersionConstraintParser {
14 /**
15 * @param string $value
16 *
17 * @return VersionConstraint
18 *
19 * @throws UnsupportedVersionConstraintException
20 */
21 public function parse($value) {
22
23 if (strpos($value, '||') !== false) {
24 return $this->handleOrGroup($value);
25 }
26
27 if (!preg_match('/^[\^~\*]?[\d.\*]+$/', $value)) {
28 throw new UnsupportedVersionConstraintException(
29 sprintf('Version constraint %s is not supported.', $value)
30 );
31 }
32
33 switch ($value[0]) {
34 case '~':
35 return $this->handleTildeOperator($value);
36 case '^':
37 return $this->handleCaretOperator($value);
38 }
39
40 $version = new VersionConstraintValue($value);
41
42 if ($version->getMajor()->isAny()) {
43 return new AnyVersionConstraint();
44 }
45
46 if ($version->getMinor()->isAny()) {
47 return new SpecificMajorVersionConstraint(
48 $value,
49 $version->getMajor()->getValue()
50 );
51 }
52
53 if ($version->getPatch()->isAny()) {
54 return new SpecificMajorAndMinorVersionConstraint(
55 $value,
56 $version->getMajor()->getValue(),
57 $version->getMinor()->getValue()
58 );
59 }
60
61 return new ExactVersionConstraint($value);
62 }
63
64 /**
65 * @param $value
66 *
67 * @return OrVersionConstraintGroup
68 */
69 private function handleOrGroup($value) {
70 $constraints = [];
71
72 foreach (explode('||', $value) as $groupSegment) {
73 $constraints[] = $this->parse(trim($groupSegment));
74 }
75
76 return new OrVersionConstraintGroup($value, $constraints);
77 }
78
79 /**
80 * @param string $value
81 *
82 * @return AndVersionConstraintGroup
83 */
84 private function handleTildeOperator($value) {
85 $version = new Version(substr($value, 1));
86 $constraints = [
87 new GreaterThanOrEqualToVersionConstraint($value, $version)
88 ];
89
90 if ($version->getPatch()->isAny()) {
91 $constraints[] = new SpecificMajorVersionConstraint(
92 $value,
93 $version->getMajor()->getValue()
94 );
95 } else {
96 $constraints[] = new SpecificMajorAndMinorVersionConstraint(
97 $value,
98 $version->getMajor()->getValue(),
99 $version->getMinor()->getValue()
100 );
101 }
102
103 return new AndVersionConstraintGroup($value, $constraints);
104 }
105
106 /**
107 * @param string $value
108 *
109 * @return AndVersionConstraintGroup
110 */
111 private function handleCaretOperator($value) {
112 $version = new Version(substr($value, 1));
113
114 return new AndVersionConstraintGroup(
115 $value,
116 [
117 new GreaterThanOrEqualToVersionConstraint($value, $version),
118 new SpecificMajorVersionConstraint($value, $version->getMajor()->getValue())
119 ]
120 );
121 }
122 }