|
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\Submit' => 'self::*[@type="submit" and (self::input or self::button)]', |
|
19
|
|
|
'SP\Crawler\Element\Checkbox' => 'self::input[@type="checkbox"]', |
|
20
|
|
|
'SP\Crawler\Element\Radio' => 'self::input[@type="radio"]', |
|
21
|
|
|
'SP\Crawler\Element\File' => 'self::input[@type="file"]', |
|
22
|
|
|
'SP\Crawler\Element\Input' => 'self::input', |
|
23
|
|
|
'SP\Crawler\Element\Select' => 'self::select', |
|
24
|
|
|
'SP\Crawler\Element\Textarea' => 'self::textarea', |
|
25
|
|
|
'SP\Crawler\Element\Option' => 'self::option', |
|
26
|
|
|
'SP\Crawler\Element\Anchor' => 'self::a', |
|
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
|
|
|
|