Completed
Push — v2 ( dad8a3...5e546d )
by Joschi
06:58
created

MicroformatsFactory::createFromParserResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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