|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Version package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) Nikola Posa <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For full copyright and license information, please refer to the LICENSE file, |
|
9
|
|
|
* located at the package root folder. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Version\Constraint\Parser; |
|
13
|
|
|
|
|
14
|
|
|
use Version\Constraint\CompositeConstraint; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @author Nikola Posa <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class AbstractMultiPartParser extends AbstractParser |
|
20
|
|
|
{ |
|
21
|
|
|
const OPERATOR_OR = '||'; |
|
22
|
|
|
|
|
23
|
|
|
protected $constraintParts = []; |
|
24
|
|
|
|
|
25
|
10 |
|
protected function doParse() |
|
26
|
|
|
{ |
|
27
|
10 |
|
if (!$this->isMultiPartConstraint()) { |
|
28
|
7 |
|
return $this->buildConstraintFromStringUnit($this->constraintString); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
3 |
|
$this->splitConstraintParts(); |
|
32
|
|
|
|
|
33
|
3 |
|
return $this->buildCompositeConstraint(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
10 |
|
protected function isMultiPartConstraint() |
|
37
|
|
|
{ |
|
38
|
10 |
|
return (false !== strpos($this->constraintString, ' ')); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
3 |
|
protected function splitConstraintParts() |
|
42
|
|
|
{ |
|
43
|
3 |
|
$this->constraintParts = explode(' ' , $this->constraintString); |
|
44
|
3 |
|
} |
|
45
|
|
|
|
|
46
|
3 |
|
protected function buildCompositeConstraint() |
|
47
|
|
|
{ |
|
48
|
3 |
|
$compositeAndConstraints = $compositeOrConstraints = []; |
|
49
|
|
|
|
|
50
|
3 |
|
foreach ($this->constraintParts as $constraintPart) { |
|
51
|
3 |
|
if (!$this->isOperator($constraintPart)) { |
|
52
|
3 |
|
$compositeAndConstraints[] = $this->buildConstraintFromStringUnit($constraintPart); |
|
53
|
3 |
|
continue; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
2 |
|
$constraintOperator = $constraintPart; |
|
57
|
|
|
|
|
58
|
|
|
switch ($constraintOperator) { |
|
59
|
2 |
|
case self::OPERATOR_OR : |
|
60
|
2 |
|
$compositeOrConstraints[] = CompositeConstraint::fromAndConstraints($compositeAndConstraints); |
|
61
|
2 |
|
$compositeAndConstraints = []; |
|
62
|
2 |
|
break; |
|
63
|
|
|
} |
|
64
|
3 |
|
} |
|
65
|
|
|
|
|
66
|
3 |
|
if (!empty($compositeOrConstraints)) { |
|
67
|
2 |
|
if (empty($compositeAndConstraints)) { |
|
68
|
|
|
//invalid OR constraint; no right side |
|
69
|
1 |
|
$this->error(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
$compositeOrConstraints[] = CompositeConstraint::fromAndConstraints($compositeAndConstraints); |
|
73
|
|
|
|
|
74
|
1 |
|
return CompositeConstraint::fromOrConstraints($compositeOrConstraints); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
return CompositeConstraint::fromAndConstraints($compositeAndConstraints); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
3 |
|
protected function isOperator($constraintPart) |
|
81
|
|
|
{ |
|
82
|
3 |
|
return in_array($constraintPart, [ |
|
83
|
3 |
|
self::OPERATOR_OR, |
|
84
|
3 |
|
]); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|