Completed
Push — v2 ( a2e732...ad2ba8 )
by Joschi
07:14
created

MicroformatsFactory::createItem()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6.0493

Importance

Changes 0
Metric Value
cc 6
eloc 9
nc 8
nop 1
dl 0
loc 21
ccs 8
cts 9
cp 0.8889
crap 6.0493
rs 8.7624
c 0
b 0
f 0
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 2
        $microformatItem = ['type' => self::createTypes($item['type'])];
64
65
        // Create the properties (if any)
66 2
        if (isset($item['properties']) && is_array($item['properties'])) {
67 2
            $microformatItem['properties'] = self::createProperties($item['properties']);
68
        }
69
70
        // Create the value (if any)
71 2
        if (isset($item['value'])) {
72 2
            $microformatItem['value'] = self::createValue($item['value']);
73
        }
74
75
        // Create the nested children (if any)
76 2
        if (isset($item['children']) && is_array($item['children'])) {
77
            $microformatItem['children'] = self::createFromParserResult($item['children']);
78
        }
79
80 2
        return (object)$microformatItem;
81
    }
82
83
    /**
84
     * Refine the item types
85
     *
86
     * @param array $types Types
87
     * @return array Refined types
88
     */
89 2
    protected static function createTypes(array $types)
90
    {
91 2
        return array_map(
92
            function ($type) {
93 2
                return self::MF2_PROFILE_URI.$type;
94 2
            }, $types
95
        );
96
    }
97
98
    /**
99
     * Refine the item properties
100
     *
101
     * @param array $properties Properties
102
     * @return array Refined properties
103
     */
104 2
    protected static function createProperties(array $properties)
105
    {
106 2
        $microformatProperties = [];
107 2
        foreach ($properties as $propertyName => $propertyValues) {
108 2
            $microformatProperties[self::MF2_PROFILE_URI.$propertyName] = self::createProperty($propertyValues);
109
        }
110 2
        return $microformatProperties;
111
    }
112
113
    /**
114
     * Refine the item property values
115
     *
116
     * @param array $propertyValues Property values
117
     * @return array Refined property values
118
     */
119 2
    protected static function createProperty(array $propertyValues)
120
    {
121 2
        return array_map(
122 2
            function ($propertyValue) {
123 2
                if (is_array($propertyValue)) {
124 2
                    return isset($propertyValue['type']) ? self::createItem($propertyValue) : $propertyValue;
125
                }
126 2
                return $propertyValue;
127 2
            }, $propertyValues
128
        );
129
    }
130
131
    /**
132
     * Refine the item value
133
     *
134
     * @param string $value Value
135
     * @return string Refined value
136
     */
137 2
    protected static function createValue($value)
138
    {
139 2
        return $value;
140
    }
141
142
    /**
143
     * Refine and convert the Microformats parser result
144
     *
145
     * @param array $items Items
146
     * @return array Refined items
147
     */
148 2
    public static function createFromParserResult(array $items)
149
    {
150 2
        return array_map([self::class, 'createItem'], $items);
151
    }
152
}
153