Rule.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. protected $id;
  21. protected $weak;
  22. public $watch1;
  23. public $watch2;
  24. public $next1;
  25. public $next2;
  26. public $ruleHash;
  27. public function __construct(array $literals, $reason, $reasonData)
  28. {
  29. // sort all packages ascending by id
  30. usort($literals, array($this, 'compareLiteralsById'));
  31. $this->literals = $literals;
  32. $this->reason = $reason;
  33. $this->reasonData = $reasonData;
  34. $this->disabled = false;
  35. $this->weak = false;
  36. $this->watch1 = (count($this->literals) > 0) ? $literals[0]->getId() : 0;
  37. $this->watch2 = (count($this->literals) > 1) ? $literals[1]->getId() : 0;
  38. $this->type = -1;
  39. $this->ruleHash = substr(md5(implode(',', array_map(function ($l) {
  40. return $l->getId();
  41. }, $this->literals))), 0, 5);
  42. }
  43. public function getHash()
  44. {
  45. return $this->ruleHash;
  46. }
  47. public function setId($id)
  48. {
  49. $this->id = $id;
  50. }
  51. public function getId()
  52. {
  53. return $this->id;
  54. }
  55. /**
  56. * Checks if this rule is equal to another one
  57. *
  58. * Ignores whether either of the rules is disabled.
  59. *
  60. * @param Rule $rule The rule to check against
  61. * @return bool Whether the rules are equal
  62. */
  63. public function equals(Rule $rule)
  64. {
  65. if ($this->ruleHash !== $rule->ruleHash) {
  66. return false;
  67. }
  68. if (count($this->literals) != count($rule->literals)) {
  69. return false;
  70. }
  71. for ($i = 0, $n = count($this->literals); $i < $n; $i++) {
  72. if ($this->literals[$i]->getId() !== $rule->literals[$i]->getId()) {
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. public function setType($type)
  79. {
  80. $this->type = $type;
  81. }
  82. public function getType()
  83. {
  84. return $this->type;
  85. }
  86. public function disable()
  87. {
  88. $this->disabled = true;
  89. }
  90. public function enable()
  91. {
  92. $this->disabled = false;
  93. }
  94. public function isDisabled()
  95. {
  96. return $this->disabled;
  97. }
  98. public function isEnabled()
  99. {
  100. return !$this->disabled;
  101. }
  102. public function isWeak()
  103. {
  104. return $this->weak;
  105. }
  106. public function setWeak($weak)
  107. {
  108. $this->weak = $weak;
  109. }
  110. public function getLiterals()
  111. {
  112. return $this->literals;
  113. }
  114. public function isAssertion()
  115. {
  116. return 1 === count($this->literals);
  117. }
  118. public function getNext(Literal $literal)
  119. {
  120. if ($this->watch1 == $literal->getId()) {
  121. return $this->next1;
  122. } else {
  123. return $this->next2;
  124. }
  125. }
  126. public function getOtherWatch(Literal $literal)
  127. {
  128. if ($this->watch1 == $literal->getId()) {
  129. return $this->watch2;
  130. } else {
  131. return $this->watch1;
  132. }
  133. }
  134. /**
  135. * Formats a rule as a string of the format (Literal1|Literal2|...)
  136. *
  137. * @return string
  138. */
  139. public function __toString()
  140. {
  141. $result = ($this->isDisabled()) ? 'disabled(' : '(';
  142. foreach ($this->literals as $i => $literal) {
  143. if ($i != 0) {
  144. $result .= '|';
  145. }
  146. $result .= $literal;
  147. }
  148. $result .= ')';
  149. return $result;
  150. }
  151. /**
  152. * Comparison function for sorting literals by their id
  153. *
  154. * @param Literal $a
  155. * @param Literal $b
  156. * @return int 0 if the literals are equal, 1 if b is larger than a, -1 else
  157. */
  158. private function compareLiteralsById(Literal $a, Literal $b)
  159. {
  160. if ($a->getId() === $b->getId()) {
  161. return 0;
  162. }
  163. return $a->getId() < $b->getId() ? -1 : 1;
  164. }
  165. }