Rule.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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\DependencyResolver;
  12. /**
  13. * @author Nils Adermann <naderman@naderman.de>
  14. */
  15. class Rule
  16. {
  17. protected $disabled;
  18. protected $literals;
  19. protected $type;
  20. public $watch1;
  21. public $watch2;
  22. public $next1;
  23. public $next2;
  24. public function __construct(array $literals, $reason, $reasonData)
  25. {
  26. // sort all packages ascending by id
  27. usort($literals, array($this, 'compareLiteralsById'));
  28. $this->literals = $literals;
  29. $this->reason = $reason;
  30. $this->reasonData = $reasonData;
  31. $this->disabled = false;
  32. $this->watch1 = (count($this->literals) > 0) ? $literals[0]->getId() : 0;
  33. $this->watch2 = (count($this->literals) > 1) ? $literals[1]->getId() : 0;
  34. $this->type = -1;
  35. }
  36. /**
  37. * Checks if this rule is equal to another one
  38. *
  39. * Ignores whether either of the rules is disabled.
  40. *
  41. * @param Rule $rule The rule to check against
  42. * @return bool Whether the rules are equal
  43. */
  44. public function equals(Rule $rule)
  45. {
  46. if (count($this->literals) != count($rule->literals)) {
  47. return false;
  48. }
  49. for ($i = 0, $n = count($this->literals); $i < $n; $i++) {
  50. if (!$this->literals[$i]->equals($rule->literals[$i])) {
  51. return false;
  52. }
  53. }
  54. return true;
  55. }
  56. public function setType($type)
  57. {
  58. $this->type = $type;
  59. }
  60. public function getType()
  61. {
  62. return $this->type;
  63. }
  64. public function disable()
  65. {
  66. $this->disabled = true;
  67. }
  68. public function enable()
  69. {
  70. $this->disabled = false;
  71. }
  72. public function isDisabled()
  73. {
  74. return $this->disabled;
  75. }
  76. public function isEnabled()
  77. {
  78. return !$this->disabled;
  79. }
  80. public function getLiterals()
  81. {
  82. return $this->literals;
  83. }
  84. public function isAssertion()
  85. {
  86. return 1 === count($this->literals);
  87. }
  88. public function getNext(Literal $literal)
  89. {
  90. if ($this->watch1->equals($literal)) {
  91. return $this->next1;
  92. } else {
  93. return $this->next2;
  94. }
  95. }
  96. public function getOtherWatch(Literal $literal)
  97. {
  98. if ($this->watch1->equals($literal)) {
  99. return $this->watch2;
  100. } else {
  101. return $this->watch1;
  102. }
  103. }
  104. /**
  105. * Formats a rule as a string of the format (Literal1|Literal2|...)
  106. *
  107. * @return string
  108. */
  109. public function __toString()
  110. {
  111. $result = '(';
  112. foreach ($this->literals as $i => $literal) {
  113. if ($i != 0) {
  114. $result .= '|';
  115. }
  116. $result .= $literal;
  117. }
  118. $result .= ')';
  119. return $result;
  120. }
  121. /**
  122. * Comparison function for sorting literals by their id
  123. *
  124. * @param Literal $a
  125. * @param Literal $b
  126. * @return int 0 if the literals are equal, 1 if b is larger than a, -1 else
  127. */
  128. private function compareLiteralsById(Literal $a, Literal $b)
  129. {
  130. if ($a->getId() === $b->getId()) {
  131. return 0;
  132. }
  133. return $a->getId() < $b->getId() ? -1 : 1;
  134. }
  135. }