Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types = 1);
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
namespace PharIo\Version;
11
 
12
class VersionConstraintParser {
13
    /**
14
     * @throws UnsupportedVersionConstraintException
15
     */
16
    public function parse(string $value): VersionConstraint {
17
        if (\strpos($value, '|') !== false) {
18
            return $this->handleOrGroup($value);
19
        }
20
 
21
        if (!\preg_match('/^[\^~*]?v?[\d.*]+(?:-.*)?$/i', $value)) {
22
            throw new UnsupportedVersionConstraintException(
23
                \sprintf('Version constraint %s is not supported.', $value)
24
            );
25
        }
26
 
27
        switch ($value[0]) {
28
            case '~':
29
                return $this->handleTildeOperator($value);
30
            case '^':
31
                return $this->handleCaretOperator($value);
32
        }
33
 
34
        $constraint = new VersionConstraintValue($value);
35
 
36
        if ($constraint->getMajor()->isAny()) {
37
            return new AnyVersionConstraint();
38
        }
39
 
40
        if ($constraint->getMinor()->isAny()) {
41
            return new SpecificMajorVersionConstraint(
42
                $constraint->getVersionString(),
43
                $constraint->getMajor()->getValue() ?? 0
44
            );
45
        }
46
 
47
        if ($constraint->getPatch()->isAny()) {
48
            return new SpecificMajorAndMinorVersionConstraint(
49
                $constraint->getVersionString(),
50
                $constraint->getMajor()->getValue() ?? 0,
51
                $constraint->getMinor()->getValue() ?? 0
52
            );
53
        }
54
 
55
        return new ExactVersionConstraint($constraint->getVersionString());
56
    }
57
 
58
    private function handleOrGroup(string $value): OrVersionConstraintGroup {
59
        $constraints = [];
60
 
61
        foreach (\preg_split('{\s*\|\|?\s*}', \trim($value)) as $groupSegment) {
62
            $constraints[] = $this->parse(\trim($groupSegment));
63
        }
64
 
65
        return new OrVersionConstraintGroup($value, $constraints);
66
    }
67
 
68
    private function handleTildeOperator(string $value): AndVersionConstraintGroup {
69
        $constraintValue = new VersionConstraintValue(\substr($value, 1));
70
 
71
        if ($constraintValue->getPatch()->isAny()) {
72
            return $this->handleCaretOperator($value);
73
        }
74
 
75
        $constraints = [
76
            new GreaterThanOrEqualToVersionConstraint(
77
                $value,
78
                new Version(\substr($value, 1))
79
            ),
80
            new SpecificMajorAndMinorVersionConstraint(
81
                $value,
82
                $constraintValue->getMajor()->getValue() ?? 0,
83
                $constraintValue->getMinor()->getValue() ?? 0
84
            )
85
        ];
86
 
87
        return new AndVersionConstraintGroup($value, $constraints);
88
    }
89
 
90
    private function handleCaretOperator(string $value): AndVersionConstraintGroup {
91
        $constraintValue = new VersionConstraintValue(\substr($value, 1));
92
 
93
        $constraints = [
94
            new GreaterThanOrEqualToVersionConstraint($value, new Version(\substr($value, 1)))
95
        ];
96
 
97
        if ($constraintValue->getMajor()->getValue() === 0) {
98
            $constraints[] = new SpecificMajorAndMinorVersionConstraint(
99
                $value,
100
                $constraintValue->getMajor()->getValue() ?? 0,
101
                $constraintValue->getMinor()->getValue() ?? 0
102
            );
103
        } else {
104
            $constraints[] = new SpecificMajorVersionConstraint(
105
                $value,
106
                $constraintValue->getMajor()->getValue() ?? 0
107
            );
108
        }
109
 
110
        return new AndVersionConstraintGroup(
111
            $value,
112
            $constraints
113
        );
114
    }
115
}