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
|
6 |
|
public function __construct($format) |
67
|
|
|
{ |
68
|
6 |
|
$this->format = $format; |
69
|
6 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Prepare a single property value |
73
|
|
|
* |
74
|
|
|
* @param mixed $propertyValue Property Value |
75
|
|
|
* @return ValueInterface Value |
76
|
|
|
*/ |
77
|
4 |
|
protected function processPropertyValue($propertyValue) |
78
|
|
|
{ |
79
|
4 |
|
if (is_object($propertyValue)) { |
80
|
4 |
|
return $this->__invoke($propertyValue); |
81
|
|
|
} |
82
|
4 |
|
if (is_array($propertyValue)) { |
83
|
2 |
|
return new AlternateValues($propertyValue); |
84
|
|
|
} |
85
|
4 |
|
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
|
6 |
|
public function __invoke(\stdClass $item) |
95
|
|
|
{ |
96
|
6 |
|
$type = isset($item->type) ? $item->type : null; |
97
|
6 |
|
$itemId = isset($item->id) ? $item->id : null; |
98
|
6 |
|
$value = isset($item->value) ? $item->value : null; |
99
|
6 |
|
$children = isset($item->children) ? array_map([$this, __METHOD__], $item->children) : []; |
100
|
6 |
|
$properties = $this->getProperties($item); |
101
|
6 |
|
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
|
6 |
|
protected function getProperties(\stdClass $item) |
111
|
|
|
{ |
112
|
6 |
|
$properties = []; |
113
|
6 |
|
if (isset($item->properties) && is_array($item->properties)) { |
114
|
5 |
|
foreach ($item->properties as $propertyName => $propertyValues) { |
115
|
5 |
|
$this->processPropertyValues($properties, $propertyName, $propertyValues); |
116
|
|
|
} |
117
|
|
|
} |
118
|
6 |
|
return $properties; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Process the values of a property |
123
|
|
|
* |
124
|
|
|
* @param array $properties Properties |
125
|
|
|
* @param string $propertyName Property name |
126
|
|
|
* @param array $propertyValues Property values |
127
|
|
|
*/ |
128
|
5 |
|
protected function processPropertyValues(array &$properties, $propertyName, $propertyValues) |
129
|
|
|
{ |
130
|
|
|
try { |
131
|
5 |
|
$expandedPropertyValues = $this->getPropertyValues($propertyValues); |
132
|
4 |
|
if (count($expandedPropertyValues)) { |
133
|
4 |
|
$properties[$propertyName] = $expandedPropertyValues; |
134
|
|
|
} |
135
|
1 |
|
} catch (InvalidArgumentException $e) { |
136
|
|
|
// Skip this property |
137
|
|
|
} |
138
|
5 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Prepare item property values |
142
|
|
|
* |
143
|
|
|
* @param array $propertyValues Property values |
144
|
|
|
* @return array Expanded property values |
145
|
|
|
*/ |
146
|
5 |
|
protected function getPropertyValues($propertyValues) |
147
|
|
|
{ |
148
|
|
|
// If it's not a list of property values |
149
|
5 |
|
if (!is_array($propertyValues)) { |
150
|
1 |
|
throw new InvalidArgumentException( |
151
|
1 |
|
InvalidArgumentException::INVALID_PROPERTY_VALUES_STR, |
152
|
1 |
|
InvalidArgumentException::INVALID_PROPERTY_VALUES |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
4 |
|
return array_map([$this, 'processPropertyValue'], $propertyValues); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|