Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\CssSelector\Node; Chris@0: Chris@0: /** Chris@0: * Represents a node specificity. Chris@0: * Chris@0: * This component is a port of the Python cssselect library, Chris@0: * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect. Chris@0: * Chris@0: * @see http://www.w3.org/TR/selectors/#specificity Chris@0: * Chris@0: * @author Jean-François Simon Chris@0: * Chris@0: * @internal Chris@0: */ Chris@0: class Specificity Chris@0: { Chris@0: const A_FACTOR = 100; Chris@0: const B_FACTOR = 10; Chris@0: const C_FACTOR = 1; Chris@0: Chris@0: private $a; Chris@0: private $b; Chris@0: private $c; Chris@0: Chris@0: /** Chris@0: * @param int $a Chris@0: * @param int $b Chris@0: * @param int $c Chris@0: */ Chris@0: public function __construct($a, $b, $c) Chris@0: { Chris@0: $this->a = $a; Chris@0: $this->b = $b; Chris@0: $this->c = $c; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return self Chris@0: */ Chris@16: public function plus(self $specificity) Chris@0: { Chris@0: return new self($this->a + $specificity->a, $this->b + $specificity->b, $this->c + $specificity->c); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns global specificity value. Chris@0: * Chris@0: * @return int Chris@0: */ Chris@0: public function getValue() Chris@0: { Chris@0: return $this->a * self::A_FACTOR + $this->b * self::B_FACTOR + $this->c * self::C_FACTOR; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns -1 if the object specificity is lower than the argument, Chris@0: * 0 if they are equal, and 1 if the argument is lower. Chris@0: * Chris@0: * @return int Chris@0: */ Chris@16: public function compareTo(self $specificity) Chris@0: { Chris@0: if ($this->a !== $specificity->a) { Chris@0: return $this->a > $specificity->a ? 1 : -1; Chris@0: } Chris@0: Chris@0: if ($this->b !== $specificity->b) { Chris@0: return $this->b > $specificity->b ? 1 : -1; Chris@0: } Chris@0: Chris@0: if ($this->c !== $specificity->c) { Chris@0: return $this->c > $specificity->c ? 1 : -1; Chris@0: } Chris@0: Chris@0: return 0; Chris@0: } Chris@0: }