1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* micrometa |
5
|
|
|
* |
6
|
|
|
* @category Jkphl |
7
|
|
|
* @package Jkphl\Micrometa |
8
|
|
|
* @subpackage Jkphl\Micrometa\Infrastructure |
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\Factory; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Microformats factory |
41
|
|
|
* |
42
|
|
|
* @package Jkphl\Micrometa |
43
|
|
|
* @subpackage Jkphl\Micrometa\Infrastructure |
44
|
|
|
*/ |
45
|
|
|
class MicroformatsFactory |
46
|
|
|
{ |
47
|
|
|
/** |
48
|
|
|
* Microformats 2 profile URI |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
* @link http://microformats.org/wiki/microformats-2# |
52
|
|
|
*/ |
53
|
|
|
const MF2_PROFILE_URI = 'http://microformats.org/profile/'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Refine an item |
57
|
|
|
* |
58
|
|
|
* @param array $item Item |
59
|
|
|
* @return \stdClass Refined Microformats item |
60
|
|
|
*/ |
61
|
4 |
|
protected static function createItem(array $item) |
62
|
|
|
{ |
63
|
4 |
|
$microformatItem = ['type' => self::createTypes($item['type']), 'lang' => null]; |
64
|
4 |
|
self::processProperties($item, $microformatItem); |
65
|
4 |
|
self::processValue($item, $microformatItem); |
66
|
4 |
|
self::processChildren($item, $microformatItem); |
67
|
4 |
|
return (object)$microformatItem; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Process the item properties |
72
|
|
|
* |
73
|
|
|
* @param array $item Item |
74
|
|
|
* @param array $microformatItem Refined Microformats item |
75
|
|
|
*/ |
76
|
4 |
|
protected static function processProperties(array $item, array &$microformatItem) |
77
|
|
|
{ |
78
|
|
|
// Create the properties (if any) |
79
|
4 |
|
if (isset($item['properties']) && is_array($item['properties'])) { |
80
|
4 |
|
$microformatItem['properties'] = self::createProperties($item['properties'], $microformatItem['lang']); |
81
|
|
|
} |
82
|
4 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Process the item value |
86
|
|
|
* |
87
|
|
|
* @param array $item Item |
88
|
|
|
* @param array $microformatItem Refined Microformats item |
89
|
|
|
*/ |
90
|
4 |
|
protected static function processValue(array $item, array &$microformatItem) |
91
|
|
|
{ |
92
|
4 |
|
if (isset($item['value'])) { |
93
|
4 |
|
$microformatItem['value'] = self::createValue($item['value']); |
94
|
|
|
} |
95
|
4 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Process the nested item children |
99
|
|
|
* |
100
|
|
|
* @param array $item Item |
101
|
|
|
* @param array $microformatItem Refined Microformats item |
102
|
|
|
*/ |
103
|
4 |
|
protected static function processChildren(array $item, array &$microformatItem) |
104
|
|
|
{ |
105
|
4 |
|
if (isset($item['children']) && is_array($item['children'])) { |
106
|
2 |
|
$microformatItem['children'] = self::createFromParserResult($item['children']); |
107
|
|
|
} |
108
|
4 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Refine the item types |
112
|
|
|
* |
113
|
|
|
* @param array $types Types |
114
|
|
|
* @return array Refined types |
115
|
|
|
*/ |
116
|
4 |
|
protected static function createTypes(array $types) |
117
|
|
|
{ |
118
|
4 |
|
return array_map( |
119
|
|
|
function ($type) { |
120
|
4 |
|
return (object)['profile' => self::MF2_PROFILE_URI, 'name' => $type]; |
121
|
4 |
|
}, $types |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Refine the item properties |
127
|
|
|
* |
128
|
|
|
* @param array $properties Properties |
129
|
|
|
* @param string $lang Item language |
130
|
|
|
* @return array Refined properties |
131
|
|
|
*/ |
132
|
4 |
|
protected static function createProperties(array $properties, &$lang) |
133
|
|
|
{ |
134
|
|
|
// Extract the language (if present) |
135
|
4 |
|
$properties = self::createLanguage($properties, $lang); |
136
|
|
|
|
137
|
4 |
|
$microformatProperties = []; |
138
|
4 |
|
foreach ($properties as $propertyName => $propertyValues) { |
139
|
|
|
// Process property values |
140
|
4 |
|
if (is_array($propertyValues)) { |
141
|
4 |
|
$microformatProperties[] = (object)[ |
142
|
4 |
|
'profile' => self::MF2_PROFILE_URI, |
143
|
4 |
|
'name' => $propertyName, |
144
|
4 |
|
'values' => self::createProperty($propertyValues) |
145
|
|
|
]; |
146
|
|
|
} |
147
|
|
|
} |
148
|
4 |
|
return $microformatProperties; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Extract a language value from a value list |
153
|
|
|
* |
154
|
|
|
* @param array $values Value list |
155
|
|
|
* @param string $lang Language |
156
|
|
|
* @return array Remaining values |
157
|
|
|
*/ |
158
|
4 |
|
protected static function createLanguage(array $values, &$lang) |
159
|
|
|
{ |
160
|
|
|
// If this is an alternate values list |
161
|
4 |
|
if (isset($values['html-lang'])) { |
162
|
1 |
|
if (is_string($values['html-lang'])) { |
163
|
1 |
|
$lang = trim($values['html-lang']) ?: null; |
164
|
|
|
} |
165
|
1 |
|
unset($values['html-lang']); |
166
|
|
|
} |
167
|
|
|
|
168
|
4 |
|
return $values; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Tag values with a language (if possible) |
173
|
|
|
* |
174
|
|
|
* @param array $values Values |
175
|
|
|
* @return array Language tagged values |
176
|
|
|
*/ |
177
|
2 |
|
protected static function tagLanguage(array $values) |
178
|
|
|
{ |
179
|
2 |
|
$lang = null; |
180
|
2 |
|
$values = self::createLanguage($values, $lang); |
181
|
|
|
return $lang ? array_map(function ($value) use ($lang) { |
182
|
1 |
|
return (object)['value' => $value, 'lang' => $lang]; |
183
|
2 |
|
}, $values) : $values; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Refine the item property values |
188
|
|
|
* |
189
|
|
|
* @param array $propertyValues Property values |
190
|
|
|
* @return array Refined property values |
191
|
|
|
*/ |
192
|
4 |
|
protected static function createProperty(array $propertyValues) |
193
|
|
|
{ |
194
|
4 |
|
return array_map( |
195
|
4 |
|
function ($propertyValue) { |
196
|
4 |
|
if (is_array($propertyValue)) { |
197
|
4 |
|
return isset($propertyValue['type']) ? |
198
|
4 |
|
self::createItem($propertyValue) : self::tagLanguage($propertyValue); |
199
|
|
|
} |
200
|
4 |
|
return $propertyValue; |
201
|
4 |
|
}, |
202
|
4 |
|
$propertyValues |
203
|
|
|
); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Refine the item value |
208
|
|
|
* |
209
|
|
|
* @param string $value Value |
210
|
|
|
* @return string Refined value |
211
|
|
|
*/ |
212
|
4 |
|
protected static function createValue($value) |
213
|
|
|
{ |
214
|
4 |
|
return $value; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Refine and convert the Microformats parser result |
219
|
|
|
* |
220
|
|
|
* @param array $items Items |
221
|
|
|
* @return array Refined items |
222
|
|
|
*/ |
223
|
4 |
|
public static function createFromParserResult(array $items) |
224
|
|
|
{ |
225
|
4 |
|
return array_map([self::class, 'createItem'], $items); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|