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