1 | <?php |
||
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 | 4 | protected static function createItem(array $item) |
|
62 | { |
||
63 | $microformatItem = [ |
||
64 | 4 | 'type' => self::createTypes($item['type']), |
|
65 | 4 | 'lang' => null, |
|
66 | 4 | ]; |
|
67 | |||
68 | // Create the properties (if any) |
||
69 | 4 | if (isset($item['properties']) && is_array($item['properties'])) { |
|
70 | 4 | $microformatItem['properties'] = self::createProperties($item['properties'], $microformatItem['lang']); |
|
71 | 4 | } |
|
72 | |||
73 | // Create the value (if any) |
||
74 | 4 | if (isset($item['value'])) { |
|
75 | 4 | $microformatItem['value'] = self::createValue($item['value']); |
|
76 | 4 | } |
|
77 | |||
78 | // Create the nested children (if any) |
||
79 | 4 | if (isset($item['children']) && is_array($item['children'])) { |
|
80 | 2 | $microformatItem['children'] = self::createFromParserResult($item['children']); |
|
81 | 2 | } |
|
82 | |||
83 | 4 | return (object)$microformatItem; |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * Refine the item types |
||
88 | * |
||
89 | * @param array $types Types |
||
90 | * @return array Refined types |
||
91 | */ |
||
92 | 4 | protected static function createTypes(array $types) |
|
93 | { |
||
94 | 4 | return array_map( |
|
95 | function ($type) { |
||
96 | 4 | return (object)['profile' => self::MF2_PROFILE_URI, 'name' => $type]; |
|
97 | 4 | }, $types |
|
98 | 4 | ); |
|
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 | 4 | protected static function createProperties(array $properties, &$lang) |
|
109 | { |
||
110 | // Extract the language (if present) |
||
111 | 4 | $properties = self::createLanguage($properties, $lang); |
|
112 | |||
113 | 4 | $microformatProperties = []; |
|
114 | 4 | foreach ($properties as $propertyName => $propertyValues) { |
|
115 | // Process property values |
||
116 | 4 | if (is_array($propertyValues)) { |
|
117 | 4 | $microformatProperties[] = (object)[ |
|
118 | 4 | 'profile' => self::MF2_PROFILE_URI, |
|
119 | 4 | 'name' => $propertyName, |
|
120 | 4 | 'values' => self::createProperty($propertyValues) |
|
121 | 4 | ]; |
|
122 | 4 | } |
|
123 | 4 | } |
|
124 | 4 | 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 | 4 | protected static function createLanguage(array $values, &$lang) |
|
135 | { |
||
136 | // If this is an alternate values list |
||
137 | 4 | if (isset($values['html-lang'])) { |
|
138 | 1 | if (is_string($values['html-lang'])) { |
|
139 | 1 | $lang = trim($values['html-lang']) ?: null; |
|
140 | 1 | } |
|
141 | 1 | unset($values['html-lang']); |
|
142 | 1 | } |
|
143 | |||
144 | 4 | 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 | 2 | protected static function tagLanguage(array $values) |
|
154 | { |
||
155 | 2 | $lang = null; |
|
156 | 2 | $values = self::createLanguage($values, $lang); |
|
157 | return $lang ? array_map(function ($value) use ($lang) { |
||
158 | 1 | return (object)['value' => $value, 'lang' => $lang]; |
|
159 | 2 | }, $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 | 4 | protected static function createProperty(array $propertyValues) |
|
181 | |||
182 | /** |
||
183 | * Refine the item value |
||
184 | * |
||
185 | * @param string $value Value |
||
186 | * @return string Refined value |
||
187 | */ |
||
188 | 4 | protected static function createValue($value) |
|
192 | |||
193 | /** |
||
194 | * Refine and convert the Microformats parser result |
||
195 | * |
||
196 | * @param array $items Items |
||
197 | * @return array Refined items |
||
198 | */ |
||
199 | 4 | public static function createFromParserResult(array $items) |
|
203 | } |
||
204 |