Completed
Push — master ( 95e445...ca4763 )
by Joschi
03:42 queued 01:06
created

MicroformatsFactory::createFromParserResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 © 2018 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 © 2018 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
     *
60
     * @return \stdClass Refined Microformats item
61
     */
62 4
    protected static function createItem(array $item)
63
    {
64 4
        $microformatItem = ['type' => self::createTypes($item['type']), 'lang' => null];
65 4
        self::processProperties($item, $microformatItem);
66 4
        self::processValue($item, $microformatItem);
67 4
        self::processChildren($item, $microformatItem);
68
69 4
        return (object)$microformatItem;
70
    }
71
72
    /**
73
     * Refine the item types
74
     *
75
     * @param array $types Types
76
     *
77
     * @return array Refined types
78
     */
79 4
    protected static function createTypes(array $types)
80
    {
81 4
        return array_map(
82
            function ($type) {
83 4
                return (object)['profile' => self::MF2_PROFILE_URI, 'name' => $type];
84 4
            },
85 4
            $types
86
        );
87
    }
88
89
    /**
90
     * Process the item properties
91
     *
92
     * @param array $item            Item
93
     * @param array $microformatItem Refined Microformats item
94
     */
95 4
    protected static function processProperties(array $item, array &$microformatItem)
96
    {
97
        // Create the properties (if any)
98 4
        if (isset($item['properties']) && is_array($item['properties'])) {
99 4
            $microformatItem['properties'] = self::createProperties($item['properties'], $microformatItem['lang']);
100
        }
101 4
    }
102
103
    /**
104
     * Refine the item properties
105
     *
106
     * @param array $properties Properties
107
     * @param string $lang      Item language
108
     *
109
     * @return array Refined properties
110
     */
111 4
    protected static function createProperties(array $properties, &$lang)
112
    {
113
        // Extract the language (if present)
114 4
        $properties = self::createLanguage($properties, $lang);
115
116 4
        $mfProperties = [];
117 4
        foreach ($properties as $propertyName => $propertyValues) {
118
            // Process property values
119 4
            if (is_array($propertyValues)) {
120 4
                $mfProperties[] = (object)[
121 4
                    'profile' => self::MF2_PROFILE_URI,
122 4
                    'name'    => $propertyName,
123 4
                    'values'  => self::createProperty($propertyValues)
124
                ];
125
            }
126
        }
127
128 4
        return $mfProperties;
129
    }
130
131
    /**
132
     * Extract a language value from a value list
133
     *
134
     * @param array $values Value list
135
     * @param string $lang  Language
136
     *
137
     * @return array Remaining values
138
     */
139 4
    protected static function createLanguage(array $values, &$lang)
140
    {
141
        // If this is an alternate values list
142 4
        if (isset($values['lang'])) {
143 1
            if (is_string($values['lang'])) {
144 1
                $lang = trim($values['lang']) ?: null;
145
            }
146 1
            unset($values['lang']);
147
        }
148
149 4
        return $values;
150
    }
151
152
    /**
153
     * Refine the item property values
154
     *
155
     * @param array $propertyValues Property values
156
     *
157
     * @return array Refined property values
158
     */
159 4
    protected static function createProperty(array $propertyValues)
160
    {
161 4
        return array_map(
162
            function ($propertyValue) {
163 4
                if (is_array($propertyValue)) {
164 4
                    return isset($propertyValue['type']) ?
165 4
                        self::createItem($propertyValue) : self::tagLanguage($propertyValue);
166
                }
167
168 4
                return $propertyValue;
169 4
            },
170 4
            $propertyValues
171
        );
172
    }
173
174
    /**
175
     * Tag values with a language (if possible)
176
     *
177
     * @param array $values Values
178
     *
179
     * @return array Language tagged values
180
     */
181 2
    protected static function tagLanguage(array $values)
182
    {
183 2
        $lang   = null;
184 2
        $values = self::createLanguage($values, $lang);
185
186
        return $lang ? array_map(function ($value) use ($lang) {
187 1
            return (object)['value' => $value, 'lang' => $lang];
188 2
        }, $values) : $values;
189
    }
190
191
    /**
192
     * Process the item value
193
     *
194
     * @param array $item            Item
195
     * @param array $microformatItem Refined Microformats item
196
     */
197 4
    protected static function processValue(array $item, array &$microformatItem)
198
    {
199 4
        if (isset($item['value'])) {
200 4
            $microformatItem['value'] = self::createValue($item['value']);
201
        }
202 4
    }
203
204
    /**
205
     * Refine the item value
206
     *
207
     * @param string $value Value
208
     *
209
     * @return string Refined value
210
     */
211 4
    protected static function createValue($value)
212
    {
213 4
        return $value;
214
    }
215
216
    /**
217
     * Process the nested item children
218
     *
219
     * @param array $item            Item
220
     * @param array $microformatItem Refined Microformats item
221
     */
222 4
    protected static function processChildren(array $item, array &$microformatItem)
223
    {
224 4
        if (isset($item['children']) && is_array($item['children'])) {
225 2
            $microformatItem['children'] = self::createFromParserResult($item['children']);
226
        }
227 4
    }
228
229
    /**
230
     * Refine and convert the Microformats parser result
231
     *
232
     * @param array $items Items
233
     *
234
     * @return array Refined items
235
     */
236 4
    public static function createFromParserResult(array $items)
237
    {
238 4
        return array_map([self::class, 'createItem'], $items);
239
    }
240
}
241