4 * This file is part of composer/semver.
6 * (c) Composer <https://github.com/composer>
8 * For the full copyright and license information, please view
9 * the LICENSE file that was distributed with this source code.
12 namespace Composer\Semver\Constraint;
15 * Defines a conjunctive or disjunctive set of constraints.
17 class MultiConstraint implements ConstraintInterface
19 /** @var ConstraintInterface[] */
20 protected $constraints;
23 protected $prettyString;
26 protected $conjunctive;
29 * @param ConstraintInterface[] $constraints A set of constraints
30 * @param bool $conjunctive Whether the constraints should be treated as conjunctive or disjunctive
32 public function __construct(array $constraints, $conjunctive = true)
34 $this->constraints = $constraints;
35 $this->conjunctive = $conjunctive;
39 * @return ConstraintInterface[]
41 public function getConstraints()
43 return $this->constraints;
49 public function isConjunctive()
51 return $this->conjunctive;
57 public function isDisjunctive()
59 return !$this->conjunctive;
63 * @param ConstraintInterface $provider
67 public function matches(ConstraintInterface $provider)
69 if (false === $this->conjunctive) {
70 foreach ($this->constraints as $constraint) {
71 if ($constraint->matches($provider)) {
79 foreach ($this->constraints as $constraint) {
80 if (!$constraint->matches($provider)) {
89 * @param string $prettyString
91 public function setPrettyString($prettyString)
93 $this->prettyString = $prettyString;
99 public function getPrettyString()
101 if ($this->prettyString) {
102 return $this->prettyString;
105 return $this->__toString();
111 public function __toString()
113 $constraints = array();
114 foreach ($this->constraints as $constraint) {
115 $constraints[] = (string) $constraint;
118 return '[' . implode($this->conjunctive ? ' ' : ' || ', $constraints) . ']';