Select   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 43
ccs 20
cts 20
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A unselectAll() 0 6 2
A setValue() 0 11 2
A getValue() 0 13 3
1
<?php
2
3
namespace SP\Crawler\Element;
4
5
/**
6
 * @author    Ivan Kerin <[email protected]>
7
 * @copyright 2015, Clippings Ltd.
8
 * @license   http://spdx.org/licenses/BSD-3-Clause
9
 */
10
class Select extends AbstractElement implements InputInterface
11
{
12
    const SELECTED = './/option[@selected]';
13
    const VALUE = './/option[@value = "%s" or (not(@value) and contains(normalize-space(), "%s"))]';
14
15 1
    public function unselectAll()
16
    {
17 1
        foreach ($this->getChildren(Select::SELECTED) as $option) {
18 1
            $option->removeAttribute('selected');
19 1
        }
20 1
    }
21
    /**
22
     * @param string $value
23
     */
24 1
    public function setValue($value)
25
    {
26 1
        $this->unselectAll();
27
28 1
        $xpath = sprintf(Select::VALUE, $value, $value);
29
30 1
        foreach ($this->getChildren($xpath) as $option)
31
        {
32 1
            $option->setAttribute('selected', 'selected');
33 1
        }
34 1
    }
35
36
    /**
37
     * @return string
38
     */
39 1
    public function getValue()
40
    {
41 1
        $values = [];
42
43 1
        foreach ($this->getChildren(Select::SELECTED) as $option)
44
        {
45 1
            $values[] = $option->hasAttribute('value')
46 1
                ? $option->getAttribute('value')
47 1
                : $option->textContent;
48 1
        }
49
50 1
        return reset($values);
51
    }
52
}
53