1 | <?php |
||
53 | class ItemFactory |
||
54 | { |
||
55 | /** |
||
56 | * Parser format |
||
57 | * |
||
58 | * @var int |
||
59 | */ |
||
60 | protected $format; |
||
61 | /** |
||
62 | * Property lit factory |
||
63 | * |
||
64 | * @var PropertyListFactoryInterface |
||
65 | */ |
||
66 | protected $propertyListFactory; |
||
67 | |||
68 | /** |
||
69 | * Item factory constructor |
||
70 | * |
||
71 | * @param int $format Parser format |
||
72 | */ |
||
73 | 20 | public function __construct($format) |
|
78 | |||
79 | /** |
||
80 | * Prepare a single property value |
||
81 | * |
||
82 | * @param mixed $propertyValue Property Value |
||
83 | * |
||
84 | * @return ValueInterface Value |
||
85 | */ |
||
86 | 15 | protected function processPropertyValue($propertyValue) |
|
101 | |||
102 | /** |
||
103 | * Create an item instance |
||
104 | * |
||
105 | * The item object is expected to be layed out like this: |
||
106 | * |
||
107 | * { |
||
108 | * format: 2, // Parser formag |
||
109 | * type: 'type', // String / IRI object (see below) or list of strings / IRI objects |
||
110 | * properties: [...], // List of item property objects (see below) |
||
111 | * value: 'Item value', // Item value (optional) |
||
112 | * id: 'item-1', // Item ID (optional) |
||
113 | * children: [...] // Nested item objects (optional) |
||
114 | * } |
||
115 | * |
||
116 | * The item property objects are expected to be layed out like this: |
||
117 | * |
||
118 | * { |
||
119 | * name: 'name', // Property name |
||
120 | * profile: 'http://microformats.org/profile/', // Profile |
||
121 | * values: [...] // List of property values |
||
122 | * } |
||
123 | * |
||
124 | * Item property values may be either |
||
125 | * |
||
126 | * - a string: Interpreted as simple value |
||
127 | * - an array: Interpreted as alternate simple values |
||
128 | * - an object: Interpreted as an object property (recursively processed) |
||
129 | * |
||
130 | * IRI objects are expected to be layed out like this: |
||
131 | * |
||
132 | * { |
||
133 | * name: 'h-entry', |
||
134 | * profile: 'http://microformats.org/profile/', // Profile (optional) |
||
135 | * } |
||
136 | * |
||
137 | * @param \stdClass $item Raw item |
||
138 | * |
||
139 | * @return ItemInterface Item instance |
||
140 | */ |
||
141 | 19 | public function __invoke(\stdClass $item) |
|
142 | { |
||
143 | 19 | $type = $this->normalizeEmptyValue($item, 'type'); |
|
144 | 19 | $itemId = $this->normalizeEmptyValue($item, 'id'); |
|
145 | 19 | $itemLanguage = $this->normalizeEmptyValue($item, 'lang'); |
|
146 | 19 | $value = $this->normalizeEmptyValue($item, 'value'); |
|
147 | 19 | $children = isset($item->children) ? array_map([$this, __METHOD__], $item->children) : []; |
|
148 | 19 | $properties = $this->getProperties($item); |
|
149 | |||
150 | 18 | return new Item( |
|
151 | 18 | $this->format, |
|
152 | 18 | $this->propertyListFactory, |
|
153 | 18 | $type, |
|
154 | 18 | $properties, |
|
155 | 18 | $children, |
|
156 | 18 | $itemId, |
|
157 | 18 | $itemLanguage, |
|
158 | 18 | $value |
|
159 | ); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Normalize an empty item value |
||
164 | * |
||
165 | * @param \stdClass $item Item |
||
166 | * @param string $property Property name |
||
167 | * |
||
168 | * @return string|null Normalized property value |
||
169 | */ |
||
170 | 19 | protected function normalizeEmptyValue(\stdClass $item, $property) |
|
174 | |||
175 | /** |
||
176 | * Prepare item properties |
||
177 | * |
||
178 | * @param \stdClass $item Item |
||
179 | * |
||
180 | * @return array Properties |
||
181 | */ |
||
182 | 19 | protected function getProperties(\stdClass $item) |
|
193 | |||
194 | /** |
||
195 | * Process a property |
||
196 | * |
||
197 | * @param array $properties Properties |
||
198 | * @param \stdClass $property Property |
||
199 | */ |
||
200 | 17 | protected function processProperty(array &$properties, $property) |
|
213 | |||
214 | /** |
||
215 | * Validate if an object is a valid property |
||
216 | * |
||
217 | * @param \stdClass $property Property |
||
218 | * |
||
219 | * @return bool Is a valid property |
||
220 | */ |
||
221 | 17 | protected function validatePropertyStructure($property) |
|
225 | |||
226 | /** |
||
227 | * Prepare item property values |
||
228 | * |
||
229 | * @param array $propertyValues Property values |
||
230 | * |
||
231 | * @return array Expanded property values |
||
232 | * @throws InvalidArgumentException If it's not a list of property values |
||
233 | */ |
||
234 | 16 | protected function getPropertyValues($propertyValues) |
|
246 | |||
247 | /** |
||
248 | * Process a language tagged property value |
||
249 | * |
||
250 | * @param string|\stdClass $propertyValue Property value |
||
251 | * |
||
252 | * @return array Language and property value |
||
253 | * @throws RuntimeException If this is an invalid language tagged value |
||
254 | */ |
||
255 | 15 | protected function processLanguageTaggedPropertyValue($propertyValue) |
|
273 | } |
||
274 |