Completed
Push — master ( cce6a6...8438be )
by Ivan
03:16
created

InputMap   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getInputClasses() 0 4 1
A __construct() 0 5 1
A getReader() 0 4 1
A inputMatches() 0 4 1
A getInputClass() 0 10 3
A create() 0 6 1
A get() 0 8 2
1
<?php
2
3
namespace SP\Crawler;
4
5
use SplObjectStorage;
6
use DOMXPath;
7
use DOMElement;
8
use InvalidArgumentException;
9
10
/**
11
 * @author    Ivan Kerin <[email protected]>
12
 * @copyright 2015, Clippings Ltd.
13
 * @license   http://spdx.org/licenses/BSD-3-Clause
14
 */
15
class InputMap
16
{
17
    private static $inputClasses = [
18
        'SP\Crawler\Element\Checkbox'    => 'self::input[@type="checkbox"]',
19
        'SP\Crawler\Element\Radio'       => 'self::input[@type="radio"]',
20
        'SP\Crawler\Element\File'        => 'self::input[@type="file"]',
21
        'SP\Crawler\Element\Input'       => 'self::input',
22
        'SP\Crawler\Element\Select'      => 'self::select',
23
        'SP\Crawler\Element\Textarea'    => 'self::textarea',
24
        'SP\Crawler\Element\Option'      => 'self::option',
25
        'SP\Crawler\Element\Anchor'      => 'self::a',
26
        'SP\Crawler\Element\Submit'      => 'self::*[@type="submit" and (self::input or self::button)]',
27
    ];
28
29
    /**
30
     * @return array
31
     */
32 1
    public static function getInputClasses()
33
    {
34 1
        return self::$inputClasses;
35
    }
36
37
    /**
38
     * @var Reader
39
     */
40
    private $reader;
41
42
    /**
43
     * @var SplObjectStorage
44
     */
45
    private $items;
46
47
    /**
48
     * @param Reader $reader
49
     */
50 1
    public function __construct(Reader $reader)
51
    {
52 1
        $this->reader = $reader;
53 1
        $this->items = new SplObjectStorage();
54 1
    }
55
56
    /**
57
     * @return Reader
58
     */
59 1
    public function getReader()
60
    {
61 1
        return $this->reader;
62
    }
63
64
    /**
65
     * @param  DOMElement $element
66
     * @param  string     $xpath
67
     * @return boolean
68
     */
69 3
    public function inputMatches(DOMElement $element, $xpath)
70
    {
71 3
        return (bool) $this->reader->query($xpath, $element)->item(0);
72
    }
73
74
    /**
75
     * @param  DOMElement $element
76
     * @throws InvalidArgumentException
77
     * @return string
78
     */
79 1
    public function getInputClass(DOMElement $element)
80
    {
81 1
        foreach (self::$inputClasses as $class => $xpath) {
82 1
            if ($this->inputMatches($element, $xpath)) {
83 1
                return $class;
84
            }
85 1
        }
86
87 1
        throw new InvalidArgumentException(sprintf('%s is not an input', $element->tagName));
88
    }
89
90
    /**
91
     * @param  DOMElement $element
92
     * @throws InvalidArgumentException
93
     * @return Element\AbstractElement
94
     */
95 1
    public function create(DOMElement $element)
96
    {
97 1
        $class = $this->getInputClass($element);
98
99 1
        return new $class($this->reader, $element);
100
    }
101
102
    /**
103
     * @param  DOMElement $element
104
     * @return Element\AbstractElement
105
     */
106 1
    public function get(DOMElement $element)
107
    {
108 1
        if (empty($this->items[$element])) {
109 1
            $this->items[$element] = $this->create($element);
110 1
        }
111
112 1
        return $this->items[$element];
113
    }
114
}
115