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

ItemFactory::processPropertyValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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