Completed
Push — v2 ( ad2ba8...88b419 )
by Joschi
04:59
created

MicroformatsFactory::createProperties()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

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