Rule2Literals.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. use Composer\Package\PackageInterface;
  13. use Composer\Package\Link;
  14. /**
  15. * @author Nils Adermann <naderman@naderman.de>
  16. */
  17. class Rule2Literals extends Rule
  18. {
  19. protected $literal1;
  20. protected $literal2;
  21. /**
  22. * @param int $literal1
  23. * @param int $literal2
  24. * @param int $reason A RULE_* constant describing the reason for generating this rule
  25. * @param Link|PackageInterface $reasonData
  26. * @param array $job The job this rule was created from
  27. */
  28. public function __construct($literal1, $literal2, $reason, $reasonData, $job = null)
  29. {
  30. parent::__construct($reason, $reasonData, $job);
  31. if ($literal1 < $literal2) {
  32. $this->literal1 = $literal1;
  33. $this->literal2 = $literal2;
  34. } else {
  35. $this->literal1 = $literal2;
  36. $this->literal2 = $literal1;
  37. }
  38. }
  39. public function getLiterals()
  40. {
  41. return array($this->literal1, $this->literal2);
  42. }
  43. public function getHash()
  44. {
  45. return $this->literal1.','.$this->literal2;
  46. }
  47. /**
  48. * Checks if this rule is equal to another one
  49. *
  50. * Ignores whether either of the rules is disabled.
  51. *
  52. * @param Rule $rule The rule to check against
  53. * @return bool Whether the rules are equal
  54. */
  55. public function equals(Rule $rule)
  56. {
  57. // specialized fast-case
  58. if ($rule instanceof self) {
  59. if ($this->literal1 !== $rule->literal1) {
  60. return false;
  61. }
  62. if ($this->literal2 !== $rule->literal2) {
  63. return false;
  64. }
  65. return true;
  66. }
  67. $literals = $rule->getLiterals();
  68. if (2 != count($literals)) {
  69. return false;
  70. }
  71. if ($this->literal1 !== $literals[0]) {
  72. return false;
  73. }
  74. if ($this->literal2 !== $literals[1]) {
  75. return false;
  76. }
  77. return true;
  78. }
  79. public function isAssertion()
  80. {
  81. return false;
  82. }
  83. /**
  84. * Formats a rule as a string of the format (Literal1|Literal2|...)
  85. *
  86. * @return string
  87. */
  88. public function __toString()
  89. {
  90. $result = $this->isDisabled() ? 'disabled(' : '(';
  91. $result .= $this->literal1 . '|' . $this->literal2 . ')';
  92. return $result;
  93. }
  94. }