Completed
Push — v2 ( 384bdd...1892b9 )
by Joschi
07:49
created

ItemFactory::getProperties()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 2
nop 1
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * micrometa
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\Micrometa
8
 * @subpackage Jkphl\Micrometa\Application
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\Application\Factory;
38
39
use Jkphl\Micrometa\Application\Exceptions\InvalidArgumentException;
40
use Jkphl\Micrometa\Application\Item\Item;
41
42
/**
43
 * Item factory
44
 *
45
 * @package Jkphl\Micrometa
46
 * @subpackage Jkphl\Micrometa\Application
47
 */
48
class ItemFactory
49
{
50
    /**
51
     * Parser format
52
     *
53
     * @var int
54
     */
55
    protected $format;
56
57
    /**
58
     * Item factory constructor
59
     *
60
     * @param int $format Parser format
61
     */
62 4
    public function __construct($format)
63
    {
64 4
        $this->format = $format;
65 4
    }
66
67
    /**
68
     * Create an item instance
69
     *
70
     * @param \stdClass $item Raw item
71
     * @return Item Item instance
72
     */
73 3
    public function __invoke(\stdClass $item)
74
    {
75 3
        $type = isset($item->type) ? $item->type : null;
76 3
        $itemId = isset($item->id) ? $item->id : null;
77 3
        $properties = $this->getProperties($item);
78 3
        return new Item($this->format, $type, $properties, $itemId);
79
    }
80
81
    /**
82
     * Prepare item properties
83
     *
84
     * @param \stdClass $item Item
85
     * @return array Properties
86
     */
87 3
    protected function getProperties(\stdClass $item)
88
    {
89 3
        $properties = [];
90 3
        if (isset($item->properties) && is_array($item->properties)) {
91 2
            foreach ($item->properties as $propertyName => $propertyValues) {
92
                $this->processPropertyValues($properties, $propertyName, $propertyValues);
93 2
            }
94 1
        }
95 1
        return $properties;
96
    }
97 1
98 2
    /**
99
     * Process the values of a property
100
     *
101
     * @param array $properties Properties
102 3
     * @param string $propertyName Property name
103
     * @param array $propertyValues Property values
104
     */
105
    protected function processPropertyValues(array &$properties, $propertyName, $propertyValues)
106
    {
107
        try {
108
            $expandedPropertyValues = $this->getPropertyValues($propertyValues);
109
            if (count($expandedPropertyValues)) {
110
                $properties[$propertyName] = $expandedPropertyValues;
111 2
            }
112
        } catch (InvalidArgumentException $e) {
113
            // Skip this property
114 2
        }
115 1
    }
116 1
117 1
    /**
118
     * Prepare item property values
119
     *
120
     * @param array $propertyValues Property values
121 1
     * @return array Expanded property values
122 1
     */
123 1
    protected function getPropertyValues($propertyValues)
124 1
    {
125
        // If it's not a list of property values
126
        if (!is_array($propertyValues)) {
127
            throw new InvalidArgumentException(
128
                InvalidArgumentException::INVALID_PROPERTY_VALUES_STR,
129
                InvalidArgumentException::INVALID_PROPERTY_VALUES
130
            );
131
        }
132
133
        return array_map(
134
            function ($propertyValue) {
135
                return is_object($propertyValue) ? $this->__invoke($propertyValue) : $propertyValue;
136
            },
137
            $propertyValues
138
        );
139
    }
140
}
141