AbstractConstraint.php
3 years ago
Constraint.php
3 years ago
ConstraintInterface.php
6 years ago
EmptyConstraint.php
3 years ago
MultiConstraint.php
3 years ago
MultiConstraint.php
121 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of composer/semver. |
| 5 | * |
| 6 | * (c) Composer <https://github.com/composer> |
| 7 | * |
| 8 | * For the full copyright and license information, please view |
| 9 | * the LICENSE file that was distributed with this source code. |
| 10 | */ |
| 11 | |
| 12 | namespace Composer\Semver\Constraint; |
| 13 | |
| 14 | /** |
| 15 | * Defines a conjunctive or disjunctive set of constraints. |
| 16 | */ |
| 17 | class MultiConstraint implements ConstraintInterface |
| 18 | { |
| 19 | /** @var ConstraintInterface[] */ |
| 20 | protected $constraints; |
| 21 | |
| 22 | /** @var string|null */ |
| 23 | protected $prettyString; |
| 24 | |
| 25 | /** @var bool */ |
| 26 | protected $conjunctive; |
| 27 | |
| 28 | /** |
| 29 | * @param ConstraintInterface[] $constraints A set of constraints |
| 30 | * @param bool $conjunctive Whether the constraints should be treated as conjunctive or disjunctive |
| 31 | */ |
| 32 | public function __construct(array $constraints, $conjunctive = true) |
| 33 | { |
| 34 | $this->constraints = $constraints; |
| 35 | $this->conjunctive = $conjunctive; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @return ConstraintInterface[] |
| 40 | */ |
| 41 | public function getConstraints() |
| 42 | { |
| 43 | return $this->constraints; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @return bool |
| 48 | */ |
| 49 | public function isConjunctive() |
| 50 | { |
| 51 | return $this->conjunctive; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @return bool |
| 56 | */ |
| 57 | public function isDisjunctive() |
| 58 | { |
| 59 | return !$this->conjunctive; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @param ConstraintInterface $provider |
| 64 | * |
| 65 | * @return bool |
| 66 | */ |
| 67 | public function matches(ConstraintInterface $provider) |
| 68 | { |
| 69 | if (false === $this->conjunctive) { |
| 70 | foreach ($this->constraints as $constraint) { |
| 71 | if ($constraint->matches($provider)) { |
| 72 | return true; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | foreach ($this->constraints as $constraint) { |
| 80 | if (!$constraint->matches($provider)) { |
| 81 | return false; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @param string|null $prettyString |
| 90 | */ |
| 91 | public function setPrettyString($prettyString) |
| 92 | { |
| 93 | $this->prettyString = $prettyString; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @return string |
| 98 | */ |
| 99 | public function getPrettyString() |
| 100 | { |
| 101 | if ($this->prettyString) { |
| 102 | return $this->prettyString; |
| 103 | } |
| 104 | |
| 105 | return (string) $this; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @return string |
| 110 | */ |
| 111 | public function __toString() |
| 112 | { |
| 113 | $constraints = array(); |
| 114 | foreach ($this->constraints as $constraint) { |
| 115 | $constraints[] = (string) $constraint; |
| 116 | } |
| 117 | |
| 118 | return '[' . implode($this->conjunctive ? ' ' : ' || ', $constraints) . ']'; |
| 119 | } |
| 120 | } |
| 121 |