Drupal investigation

Specificity.php 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\CssSelector\Node;
  11. /**
  12. * Represents a node specificity.
  13. *
  14. * This component is a port of the Python cssselect library,
  15. * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
  16. *
  17. * @see http://www.w3.org/TR/selectors/#specificity
  18. *
  19. * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
  20. *
  21. * @internal
  22. */
  23. class Specificity
  24. {
  25. const A_FACTOR = 100;
  26. const B_FACTOR = 10;
  27. const C_FACTOR = 1;
  28. /**
  29. * @var int
  30. */
  31. private $a;
  32. /**
  33. * @var int
  34. */
  35. private $b;
  36. /**
  37. * @var int
  38. */
  39. private $c;
  40. /**
  41. * Constructor.
  42. *
  43. * @param int $a
  44. * @param int $b
  45. * @param int $c
  46. */
  47. public function __construct($a, $b, $c)
  48. {
  49. $this->a = $a;
  50. $this->b = $b;
  51. $this->c = $c;
  52. }
  53. /**
  54. * @param Specificity $specificity
  55. *
  56. * @return self
  57. */
  58. public function plus(Specificity $specificity)
  59. {
  60. return new self($this->a + $specificity->a, $this->b + $specificity->b, $this->c + $specificity->c);
  61. }
  62. /**
  63. * Returns global specificity value.
  64. *
  65. * @return int
  66. */
  67. public function getValue()
  68. {
  69. return $this->a * self::A_FACTOR + $this->b * self::B_FACTOR + $this->c * self::C_FACTOR;
  70. }
  71. /**
  72. * Returns -1 if the object specificity is lower than the argument,
  73. * 0 if they are equal, and 1 if the argument is lower.
  74. *
  75. * @param Specificity $specificity
  76. *
  77. * @return int
  78. */
  79. public function compareTo(Specificity $specificity)
  80. {
  81. if ($this->a !== $specificity->a) {
  82. return $this->a > $specificity->a ? 1 : -1;
  83. }
  84. if ($this->b !== $specificity->b) {
  85. return $this->b > $specificity->b ? 1 : -1;
  86. }
  87. if ($this->c !== $specificity->c) {
  88. return $this->c > $specificity->c ? 1 : -1;
  89. }
  90. return 0;
  91. }
  92. }