1 | <?php |
||
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) |
|
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) |
|
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) |
|
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 | 6 | } |
|
117 | 6 | } |
|
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 | 6 | ) { |
|
136 | 5 | $property->values = $this->getPropertyValues($property->values); |
|
137 | 5 | if (count($property->values)) { |
|
138 | 5 | $properties[] = $property; |
|
139 | 5 | } |
|
140 | 5 | } |
|
141 | 6 | } 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) |
|
164 | } |
||
165 |