Rule2Literals.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. $literals = $rule->getLiterals();
  58. if (2 != count($literals)) {
  59. return false;
  60. }
  61. if ($this->literal1 !== $literals[0]) {
  62. return false;
  63. }
  64. if ($this->literal2 !== $literals[1]) {
  65. return false;
  66. }
  67. return true;
  68. }
  69. public function isAssertion()
  70. {
  71. return false;
  72. }
  73. /**
  74. * Formats a rule as a string of the format (Literal1|Literal2|...)
  75. *
  76. * @return string
  77. */
  78. public function __toString()
  79. {
  80. $result = $this->isDisabled() ? 'disabled(' : '(';
  81. $result .= $this->literal1 . '|' . $this->literal2 . ')';
  82. return $result;
  83. }
  84. }