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