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 | 18 | public function __construct($format) |
|
78 | |||
79 | /** |
||
80 | * Prepare a single property value |
||
81 | * |
||
82 | * @param mixed $propertyValue Property Value |
||
83 | * @return ValueInterface Value |
||
84 | */ |
||
85 | 14 | protected function processPropertyValue($propertyValue) |
|
99 | |||
100 | /** |
||
101 | * Process a language tagged property value |
||
102 | * |
||
103 | * @param string|\stdClass $propertyValue Property value |
||
104 | * @return array Language and property value |
||
105 | * @throws RuntimeException If this is an invalid language tagged value |
||
106 | */ |
||
107 | 14 | protected function processLanguageTaggedPropertyValue($propertyValue) |
|
108 | { |
||
109 | 14 | $language = null; |
|
110 | 14 | if (is_object($propertyValue)) { |
|
111 | // If this is an invalid language tagged object |
||
112 | 3 | if (!isset($propertyValue->lang) || !isset($propertyValue->value)) { |
|
113 | 1 | throw new RuntimeException( |
|
114 | 1 | RuntimeException::INVALID_LANGUAGE_TAGGED_VALUE_STR, |
|
115 | RuntimeException::INVALID_LANGUAGE_TAGGED_VALUE |
||
116 | 1 | ); |
|
117 | } |
||
118 | |||
119 | 2 | $language = $propertyValue->lang; |
|
120 | 2 | $propertyValue = $propertyValue->value; |
|
121 | 2 | } |
|
122 | 13 | return [$propertyValue, $language]; |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * Create an item instance |
||
127 | * |
||
128 | * The item object is expected to be layed out like this: |
||
129 | * |
||
130 | * { |
||
131 | * format: 2, // Parser formag |
||
132 | * type: 'type', // String / IRI object (see below) or list of strings / IRI objects |
||
133 | * properties: [...], // List of item property objects (see below) |
||
134 | * value: 'Item value', // Item value (optional) |
||
135 | * id: 'item-1', // Item ID (optional) |
||
136 | * children: [...] // Nested item objects (optional) |
||
137 | * } |
||
138 | * |
||
139 | * The item property objects are expected to be layed out like this: |
||
140 | * |
||
141 | * { |
||
142 | * name: 'name', // Property name |
||
143 | * profile: 'http://microformats.org/profile/', // Profile |
||
144 | * values: [...] // List of property values |
||
145 | * } |
||
146 | * |
||
147 | * Item property values may be either |
||
148 | * |
||
149 | * - a string: Interpreted as simple value |
||
150 | * - an array: Interpreted as alternate simple values |
||
151 | * - an object: Interpreted as an object property (recursively processed) |
||
152 | * |
||
153 | * IRI objects are expected to be layed out like this: |
||
154 | * |
||
155 | * { |
||
156 | * name: 'h-entry', |
||
157 | * profile: 'http://microformats.org/profile/', // Profile (optional) |
||
158 | * } |
||
159 | * |
||
160 | * @param \stdClass $item Raw item |
||
161 | * @return ItemInterface Item instance |
||
162 | */ |
||
163 | 17 | public function __invoke(\stdClass $item) |
|
174 | |||
175 | /** |
||
176 | * Prepare item properties |
||
177 | * |
||
178 | * @param \stdClass $item Item |
||
179 | * @return array Properties |
||
180 | */ |
||
181 | 17 | protected function getProperties(\stdClass $item) |
|
182 | { |
||
183 | 17 | $properties = []; |
|
184 | 17 | if (isset($item->properties) && is_array($item->properties)) { |
|
185 | 16 | foreach ($item->properties as $property) { |
|
186 | 16 | $this->processProperty($properties, $property); |
|
187 | 15 | } |
|
188 | 15 | } |
|
189 | 16 | return $properties; |
|
190 | } |
||
191 | |||
192 | |||
193 | /** |
||
194 | * Process a property |
||
195 | * |
||
196 | * @param array $properties Properties |
||
197 | * @param \stdClass $property Property |
||
198 | */ |
||
199 | 16 | protected function processProperty(array &$properties, $property) |
|
200 | { |
||
201 | try { |
||
202 | 16 | if (is_object($property) |
|
203 | 16 | && isset($property->profile) |
|
204 | 16 | && isset($property->name) |
|
205 | 16 | && isset($property->values) |
|
206 | 16 | ) { |
|
207 | 15 | $property->values = $this->getPropertyValues($property->values); |
|
208 | 13 | if (count($property->values)) { |
|
209 | 13 | $properties[] = $property; |
|
210 | 13 | } |
|
211 | 13 | } |
|
212 | 16 | } catch (InvalidArgumentException $e) { |
|
213 | // Skip this property |
||
214 | } |
||
215 | 15 | } |
|
216 | |||
217 | /** |
||
218 | * Prepare item property values |
||
219 | * |
||
220 | * @param array $propertyValues Property values |
||
221 | * @return array Expanded property values |
||
222 | * @throws InvalidArgumentException If it's not a list of property values |
||
223 | */ |
||
224 | 15 | protected function getPropertyValues($propertyValues) |
|
236 | } |
||
237 |