MultiConstraint.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Package\LinkConstraint;
  12. /**
  13. * Defines a conjunctive or disjunctive set of constraints on the target of a package link
  14. *
  15. * @author Nils Adermann <naderman@naderman.de>
  16. * @author Jordi Boggiano <j.boggiano@seld.be>
  17. */
  18. class MultiConstraint implements LinkConstraintInterface
  19. {
  20. protected $constraints;
  21. protected $prettyString;
  22. protected $conjunctive;
  23. /**
  24. * Sets operator and version to compare a package with
  25. *
  26. * @param array $constraints A set of constraints
  27. * @param bool $conjunctive Whether the constraints should be treated as conjunctive or disjunctive
  28. */
  29. public function __construct(array $constraints, $conjunctive = true)
  30. {
  31. $this->constraints = $constraints;
  32. $this->conjunctive = $conjunctive;
  33. }
  34. public function matches(LinkConstraintInterface $provider)
  35. {
  36. if (false === $this->conjunctive) {
  37. foreach ($this->constraints as $constraint) {
  38. if ($constraint->matches($provider)) {
  39. return true;
  40. }
  41. }
  42. return false;
  43. }
  44. foreach ($this->constraints as $constraint) {
  45. if (!$constraint->matches($provider)) {
  46. return false;
  47. }
  48. }
  49. return true;
  50. }
  51. public function setPrettyString($prettyString)
  52. {
  53. $this->prettyString = $prettyString;
  54. }
  55. public function getPrettyString()
  56. {
  57. if ($this->prettyString) {
  58. return $this->prettyString;
  59. }
  60. return $this->__toString();
  61. }
  62. public function __toString()
  63. {
  64. $constraints = array();
  65. foreach ($this->constraints as $constraint) {
  66. $constraints[] = $constraint->__toString();
  67. }
  68. return '['.implode($this->conjunctive ? ', ' : ' | ', $constraints).']';
  69. }
  70. }