|
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
|
|
|
try { |
|
93
|
2 |
|
$expandedPropertyValues = $this->getPropertyValues($propertyValues); |
|
94
|
1 |
|
if (count($expandedPropertyValues)) { |
|
95
|
1 |
|
$properties [$propertyName] = $expandedPropertyValues; |
|
96
|
|
|
} |
|
97
|
1 |
|
} catch (InvalidArgumentException $e) { |
|
98
|
2 |
|
continue; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
3 |
|
return $properties; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Prepare item property values |
|
107
|
|
|
* |
|
108
|
|
|
* @param array $propertyValues Property values |
|
109
|
|
|
* @return array Expanded property values |
|
110
|
|
|
*/ |
|
111
|
2 |
|
protected function getPropertyValues($propertyValues) |
|
112
|
|
|
{ |
|
113
|
|
|
// If it's not a list of property values |
|
114
|
2 |
|
if (!is_array($propertyValues)) { |
|
115
|
1 |
|
throw new InvalidArgumentException( |
|
116
|
1 |
|
InvalidArgumentException::INVALID_PROPERTY_VALUES_STR, |
|
117
|
1 |
|
InvalidArgumentException::INVALID_PROPERTY_VALUES |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
1 |
|
return array_map( |
|
122
|
1 |
|
function($propertyValue) { |
|
|
|
|
|
|
123
|
1 |
|
return is_object($propertyValue) ? $this->__invoke($propertyValue) : $propertyValue; |
|
124
|
1 |
|
}, |
|
125
|
|
|
$propertyValues |
|
126
|
|
|
); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|