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 item |
60
|
|
|
*/ |
61
|
2 |
|
protected static function createItem(array $item) |
62
|
|
|
{ |
63
|
|
|
$microformatItem = [ |
64
|
2 |
|
'type' => self::createTypes($item['type']), |
65
|
|
|
'lang' => null, |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
// Create the properties (if any) |
69
|
2 |
|
if (isset($item['properties']) && is_array($item['properties'])) { |
70
|
2 |
|
$microformatItem['properties'] = self::createProperties($item['properties'], $microformatItem['lang']); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Create the value (if any) |
74
|
2 |
|
if (isset($item['value'])) { |
75
|
2 |
|
$microformatItem['value'] = self::createValue($item['value']); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Create the nested children (if any) |
79
|
2 |
|
if (isset($item['children']) && is_array($item['children'])) { |
80
|
1 |
|
$microformatItem['children'] = self::createFromParserResult($item['children']); |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
return (object)$microformatItem; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Refine the item types |
88
|
|
|
* |
89
|
|
|
* @param array $types Types |
90
|
|
|
* @return array Refined types |
91
|
|
|
*/ |
92
|
2 |
|
protected static function createTypes(array $types) |
93
|
|
|
{ |
94
|
2 |
|
return array_map( |
95
|
|
|
function ($type) { |
96
|
2 |
|
return (object)['profile' => self::MF2_PROFILE_URI, 'name' => $type]; |
97
|
2 |
|
}, $types |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Refine the item properties |
103
|
|
|
* |
104
|
|
|
* @param array $properties Properties |
105
|
|
|
* @param string $lang Item language |
106
|
|
|
* @return array Refined properties |
107
|
|
|
*/ |
108
|
2 |
|
protected static function createProperties(array $properties, &$lang) |
109
|
|
|
{ |
110
|
|
|
// Extract the language (if present) |
111
|
2 |
|
$properties = self::createLanguage($properties, $lang); |
112
|
|
|
|
113
|
2 |
|
$microformatProperties = []; |
114
|
2 |
|
foreach ($properties as $propertyName => $propertyValues) { |
115
|
|
|
// Process property values |
116
|
2 |
|
if (is_array($propertyValues)) { |
117
|
2 |
|
$microformatProperties[] = (object)[ |
118
|
2 |
|
'profile' => self::MF2_PROFILE_URI, |
119
|
2 |
|
'name' => $propertyName, |
120
|
2 |
|
'values' => self::createProperty($propertyValues) |
121
|
|
|
]; |
122
|
|
|
} |
123
|
|
|
} |
124
|
2 |
|
return $microformatProperties; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Extract a language value from a value list |
129
|
|
|
* |
130
|
|
|
* @param array $values Value list |
131
|
|
|
* @param string $lang Language |
132
|
|
|
* @return array Remaining values |
133
|
|
|
*/ |
134
|
2 |
|
protected static function createLanguage(array $values, &$lang) |
135
|
|
|
{ |
136
|
|
|
// If this is an alternate values list |
137
|
2 |
|
if (isset($values['html-lang'])) { |
138
|
|
|
if (is_string($values['html-lang'])) { |
139
|
|
|
$lang = trim($values['html-lang']) ?: null; |
140
|
|
|
} |
141
|
|
|
unset($values['html-lang']); |
142
|
|
|
} |
143
|
|
|
|
144
|
2 |
|
return $values; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Tag values with a language (if possible) |
149
|
|
|
* |
150
|
|
|
* @param array $values Values |
151
|
|
|
* @return array Language tagged values |
152
|
|
|
*/ |
153
|
1 |
|
protected static function tagLanguage(array $values) |
154
|
|
|
{ |
155
|
1 |
|
$lang = null; |
156
|
1 |
|
$values = self::createLanguage($values, $lang); |
157
|
|
|
return $lang ? array_map(function ($value) use ($lang) { |
158
|
|
|
return (object)['value' => $value, 'lang' => $lang]; |
159
|
1 |
|
}, $values) : $values; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Refine the item property values |
164
|
|
|
* |
165
|
|
|
* @param array $propertyValues Property values |
166
|
|
|
* @return array Refined property values |
167
|
|
|
*/ |
168
|
2 |
|
protected static function createProperty(array $propertyValues) |
169
|
|
|
{ |
170
|
2 |
|
return array_map( |
171
|
2 |
|
function ($propertyValue) { |
172
|
2 |
|
if (is_array($propertyValue)) { |
173
|
2 |
|
return isset($propertyValue['type']) ? |
174
|
2 |
|
self::createItem($propertyValue) : self::tagLanguage($propertyValue); |
175
|
|
|
} |
176
|
2 |
|
return $propertyValue; |
177
|
2 |
|
}, |
178
|
2 |
|
$propertyValues |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Refine the item value |
184
|
|
|
* |
185
|
|
|
* @param string $value Value |
186
|
|
|
* @return string Refined value |
187
|
|
|
*/ |
188
|
2 |
|
protected static function createValue($value) |
189
|
|
|
{ |
190
|
2 |
|
return $value; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Refine and convert the Microformats parser result |
195
|
|
|
* |
196
|
|
|
* @param array $items Items |
197
|
|
|
* @return array Refined items |
198
|
|
|
*/ |
199
|
2 |
|
public static function createFromParserResult(array $items) |
200
|
|
|
{ |
201
|
2 |
|
return array_map([self::class, 'createItem'], $items); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|