1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* micrometa |
5
|
|
|
* |
6
|
|
|
* @category Jkphl |
7
|
|
|
* @package Jkphl\Micrometa |
8
|
|
|
* @subpackage Jkphl\Micrometa\Infrastructure\Parser |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Jkphl\Micrometa\Infrastructure\Parser; |
38
|
|
|
|
39
|
|
|
use Jkphl\Micrometa\Application\Contract\ParsingResultInterface; |
40
|
|
|
use Jkphl\Micrometa\Ports\Format; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Link rel parser |
44
|
|
|
* |
45
|
|
|
* @package Jkphl\Micrometa |
46
|
|
|
* @subpackage Jkphl\Micrometa\Infrastructure |
47
|
|
|
*/ |
48
|
|
|
class LinkType extends AbstractParser |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* Format |
52
|
|
|
* |
53
|
|
|
* @var int |
54
|
|
|
*/ |
55
|
|
|
const FORMAT = Format::LINK_TYPE; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Parse a DOM document |
59
|
|
|
* |
60
|
|
|
* @param \DOMDocument $dom DOM Document |
61
|
|
|
* @return ParsingResultInterface Micro information items |
62
|
|
|
*/ |
63
|
2 |
|
public function parseDom(\DOMDocument $dom) |
64
|
|
|
{ |
65
|
2 |
|
$this->logger->info('Running parser: '.(new \ReflectionClass(__CLASS__))->getShortName()); |
66
|
2 |
|
$items = []; |
67
|
|
|
|
68
|
2 |
|
$xpath = new \DOMXPath($dom); |
69
|
2 |
|
$xpath->registerNamespace('html', self::HTML_PROFILE_URI); |
70
|
|
|
|
71
|
|
|
// Run through all <link> elements with a `rel` attribute |
72
|
|
|
/** @var \DOMElement $linkType */ |
73
|
2 |
|
foreach ($xpath->query('//*[local-name(.) = "link" or local-name(.) = "a"][@rel]') as $linkType) { |
74
|
|
|
$item = [ |
75
|
2 |
|
'type' => $this->parseLinkType($linkType->getAttribute('rel')), |
76
|
2 |
|
'id' => $linkType->getAttribute('id') ?: null, |
77
|
2 |
|
'properties' => $this->parseProperties($linkType), |
78
|
|
|
]; |
79
|
2 |
|
$items[] = (object)$item; |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
return new ParsingResult(self::FORMAT, $items); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Process the item types |
87
|
|
|
* |
88
|
|
|
* @param string $relAttr rel attribute value |
89
|
|
|
* @return array Item types |
90
|
|
|
*/ |
91
|
2 |
|
protected function parseLinkType($relAttr) |
92
|
|
|
{ |
93
|
2 |
|
$type = []; |
94
|
2 |
|
foreach (preg_split('/\040+/', $relAttr) as $rel) { |
95
|
2 |
|
$type[] = (object)['name' => $rel, 'profile' => self::HTML_PROFILE_URI]; |
96
|
|
|
} |
97
|
2 |
|
return $type; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Parse the LinkType attributes |
102
|
|
|
* |
103
|
|
|
* @param \DOMElement $linkType LinkType element |
104
|
|
|
* @return array Properties |
105
|
|
|
*/ |
106
|
2 |
|
protected function parseProperties(\DOMElement $linkType) |
107
|
|
|
{ |
108
|
2 |
|
$properties = []; |
109
|
|
|
/** |
110
|
|
|
* @var string $attributeName Attribute name |
111
|
|
|
* @var \DOMAttr $attribute Attribute |
112
|
|
|
*/ |
113
|
2 |
|
foreach ($linkType->attributes as $attributeName => $attribute) { |
114
|
2 |
|
if (!in_array($attributeName, ['rel', 'id'])) { |
115
|
2 |
|
$profile = $attribute->lookupNamespaceUri($attribute->prefix ?: null); |
116
|
|
|
$property = (object)[ |
117
|
2 |
|
'name' => $attributeName, |
118
|
2 |
|
'profile' => $profile, |
119
|
2 |
|
'values' => $this->parseAttributeValue($profile, $attributeName, $attribute->value), |
120
|
|
|
]; |
121
|
2 |
|
$properties[] = $property; |
122
|
|
|
} |
123
|
|
|
} |
124
|
2 |
|
return $properties; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Parse an attribute value |
129
|
|
|
* |
130
|
|
|
* @param string $profile Profile |
131
|
|
|
* @param string $attribute Attribute name |
132
|
|
|
* @param string $value Attribute value |
133
|
|
|
* @return array Attribute values |
134
|
|
|
*/ |
135
|
2 |
|
protected function parseAttributeValue($profile, $attribute, $value) |
136
|
|
|
{ |
137
|
|
|
// If it's a HTML attribute |
138
|
2 |
|
if ($profile == LinkType::HTML_PROFILE_URI) { |
139
|
|
|
switch ($attribute) { |
140
|
|
|
// Space delimited lists |
141
|
2 |
|
case 'sizes': |
142
|
2 |
|
return array_filter(preg_split('/\040+/', $value)); |
143
|
|
|
// Space or comma delimited lists |
144
|
2 |
|
case 'charset': |
145
|
2 |
|
return array_filter(preg_split('/[,\040]+/', $value)); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
2 |
|
return [$value]; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|