You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.8 KiB

3 years ago
  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. public const A_FACTOR = 100;
  26. public const B_FACTOR = 10;
  27. public const C_FACTOR = 1;
  28. private $a;
  29. private $b;
  30. private $c;
  31. public function __construct(int $a, int $b, int $c)
  32. {
  33. $this->a = $a;
  34. $this->b = $b;
  35. $this->c = $c;
  36. }
  37. public function plus(self $specificity): self
  38. {
  39. return new self($this->a + $specificity->a, $this->b + $specificity->b, $this->c + $specificity->c);
  40. }
  41. public function getValue(): int
  42. {
  43. return $this->a * self::A_FACTOR + $this->b * self::B_FACTOR + $this->c * self::C_FACTOR;
  44. }
  45. /**
  46. * Returns -1 if the object specificity is lower than the argument,
  47. * 0 if they are equal, and 1 if the argument is lower.
  48. */
  49. public function compareTo(self $specificity): int
  50. {
  51. if ($this->a !== $specificity->a) {
  52. return $this->a > $specificity->a ? 1 : -1;
  53. }
  54. if ($this->b !== $specificity->b) {
  55. return $this->b > $specificity->b ? 1 : -1;
  56. }
  57. if ($this->c !== $specificity->c) {
  58. return $this->c > $specificity->c ? 1 : -1;
  59. }
  60. return 0;
  61. }
  62. }