Completed
Push — v2 ( a3e6f8...094005 )
by Joschi
07:34
created

MicroformatsFactory::createProperties()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.1158

Importance

Changes 0
Metric Value
dl 0
loc 21
c 0
b 0
f 0
ccs 10
cts 12
cp 0.8333
rs 8.7624
cc 5
eloc 12
nc 4
nop 2
crap 5.1158
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 2
        $microformatProperties = [];
111 2
        foreach ($properties as $propertyName => $propertyValues) {
112
            // If this is the item language
113 2
            if (($propertyName == 'html-lang') && is_string($propertyValues)) {
114
                $lang = $propertyValues;
115
                continue;
116
            }
117
118
            // Process property values
119 2
            if (is_array($propertyValues)) {
120 2
                $microformatProperties[] = (object)[
121 2
                    'profile' => self::MF2_PROFILE_URI,
122 2
                    'name' => $propertyName,
123 2
                    'values' => self::createProperty($propertyValues)
124
                ];
125
            }
126
        }
127 2
        return $microformatProperties;
128
    }
129
130
    /**
131
     * Refine the item property values
132
     *
133
     * @param array $propertyValues Property values
134
     * @return array Refined property values
135
     */
136 2
    protected static function createProperty(array $propertyValues)
137
    {
138 2
        return array_map(
139 2
            function ($propertyValue) {
140 2
                if (is_array($propertyValue)) {
141 2
                    return isset($propertyValue['type']) ? self::createItem($propertyValue) : $propertyValue;
142
                }
143 2
                return $propertyValue;
144 2
            }, $propertyValues
145
        );
146
    }
147
148
    /**
149
     * Refine the item value
150
     *
151
     * @param string $value Value
152
     * @return string Refined value
153
     */
154 2
    protected static function createValue($value)
155
    {
156 2
        return $value;
157
    }
158
159
    /**
160
     * Refine and convert the Microformats parser result
161
     *
162
     * @param array $items Items
163
     * @return array Refined items
164
     */
165 2
    public static function createFromParserResult(array $items)
166
    {
167 2
        return array_map([self::class, 'createItem'], $items);
168
    }
169
}
170