scratch – Blame information for rev 87

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