Selector   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 131
rs 10
c 0
b 0
f 0
wmc 20

20 Methods

Rating   Name   Duplication   Size   Complexity  
A adjacent() 0 3 1
A sibling() 0 3 1
A elementClass() 0 3 1
A elementID() 0 3 1
A __construct() 0 5 1
A pseudoElement() 0 3 1
A anyElement() 0 3 1
A directDescendant() 0 3 1
A toArray() 0 3 1
A pseudoClass() 0 3 1
A attribute() 0 6 1
A getIterator() 0 3 1
A element() 0 3 1
A combinator() 0 5 1
A anyDescendant() 0 3 1
A anotherSelector() 0 5 1
A elementNS() 0 4 1
A anyElementInNS() 0 4 1
A attributeNS() 0 7 1
A count() 0 3 1
1
<?php
2
/** @file
3
 * A selector.
4
 */
5
6
namespace QueryPath\CSS;
7
8
/**
9
 * A CSS Selector.
10
 *
11
 * A CSS selector is made up of one or more Simple Selectors
12
 * (SimpleSelector).
13
 *
14
 * @attention
15
 * The Selector data structure is a LIFO (Last in, First out). This is
16
 * because CSS selectors are best processed "bottom up". Thus, when
17
 * iterating over 'a>b>c', the iterator will produce:
18
 * - c
19
 * - b
20
 * - a
21
 * It is assumed, therefore, that any suitable querying engine will
22
 * traverse from the bottom (`c`) back up.
23
 *
24
 * @b     Usage
25
 *
26
 * This class is an event handler. It can be plugged into an Parser and
27
 * receive the events the Parser generates.
28
 *
29
 * This class is also an iterator. Once the parser has completed, the
30
 * captured selectors can be iterated over.
31
 *
32
 * @code
33
 * <?php
34
 * $selectorList = new \QueryPath\CSS\Selector();
35
 * $parser = new \QueryPath\CSS\Parser($selector, $selectorList);
36
 *
37
 * $parser->parse();
38
 *
39
 * foreach ($selectorList as $simpleSelector) {
40
 *   // Do something with the SimpleSelector.
41
 *   print_r($simpleSelector);
42
 * }
43
 * ?>
44
 * @endode
45
 *
46
 *
47
 * @since QueryPath 3.0.0
48
 */
49
class Selector implements EventHandler, \IteratorAggregate, \Countable
50
{
51
52
    protected $selectors = [];
53
    protected $currSelector;
54
    protected $selectorGroups = [];
55
    protected $groupIndex = 0;
56
57
    public function __construct()
58
    {
59
        $this->currSelector = new SimpleSelector();
60
61
        $this->selectors[$this->groupIndex][] = $this->currSelector;
62
    }
63
64
    public function getIterator(): \Traversable
65
    {
66
        return new \ArrayIterator($this->selectors);
67
    }
68
69
    /**
70
     * Get the array of SimpleSelector objects.
71
     *
72
     * Normally, one iterates over a Selector. However, if it is
73
     * necessary to get the selector array and manipulate it, this
74
     * method can be used.
75
     */
76
    public function toArray()
77
    {
78
        return $this->selectors;
79
    }
80
81
    public function count(): int
82
    {
83
        return count($this->selectors);
84
    }
85
86
    public function elementID($id)
87
    {
88
        $this->currSelector->id = $id;
89
    }
90
91
    public function element($name)
92
    {
93
        $this->currSelector->element = $name;
94
    }
95
96
    public function elementNS($name, $namespace = NULL)
97
    {
98
        $this->currSelector->ns = $namespace;
99
        $this->currSelector->element = $name;
100
    }
101
102
    public function anyElement()
103
    {
104
        $this->currSelector->element = '*';
105
    }
106
107
    public function anyElementInNS($ns)
108
    {
109
        $this->currSelector->ns = $ns;
110
        $this->currSelector->element = '*';
111
    }
112
113
    public function elementClass($name)
114
    {
115
        $this->currSelector->classes[] = $name;
116
    }
117
118
    public function attribute($name, $value = NULL, $operation = EventHandler::IS_EXACTLY)
119
    {
120
        $this->currSelector->attributes[] = [
121
            'name'  => $name,
122
            'value' => $value,
123
            'op'    => $operation,
124
        ];
125
    }
126
127
    public function attributeNS($name, $ns, $value = NULL, $operation = EventHandler::IS_EXACTLY)
128
    {
129
        $this->currSelector->attributes[] = [
130
            'name'  => $name,
131
            'value' => $value,
132
            'op'    => $operation,
133
            'ns'    => $ns,
134
        ];
135
    }
136
137
    public function pseudoClass($name, $value = NULL)
138
    {
139
        $this->currSelector->pseudoClasses[] = ['name' => $name, 'value' => $value];
140
    }
141
142
    public function pseudoElement($name)
143
    {
144
        $this->currSelector->pseudoElements[] = $name;
145
    }
146
147
    public function combinator($combinatorName)
148
    {
149
        $this->currSelector->combinator = $combinatorName;
150
        $this->currSelector = new SimpleSelector();
151
        array_unshift($this->selectors[$this->groupIndex], $this->currSelector);
152
        //$this->selectors[]= $this->currSelector;
153
    }
154
155
    public function directDescendant()
156
    {
157
        $this->combinator(SimpleSelector::DIRECT_DESCENDANT);
158
    }
159
160
    public function adjacent()
161
    {
162
        $this->combinator(SimpleSelector::ADJACENT);
163
    }
164
165
    public function anotherSelector()
166
    {
167
        $this->groupIndex++;
168
        $this->currSelector = new SimpleSelector();
169
        $this->selectors[$this->groupIndex] = [$this->currSelector];
170
    }
171
172
    public function sibling()
173
    {
174
        $this->combinator(SimpleSelector::SIBLING);
175
    }
176
177
    public function anyDescendant()
178
    {
179
        $this->combinator(SimpleSelector::ANY_DESCENDANT);
180
    }
181
}
182