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 | * Create an item instance |
||
102 | * |
||
103 | * The item object is expected to be layed out like this: |
||
104 | * |
||
105 | * { |
||
106 | * format: 2, // Parser formag |
||
107 | * type: 'type', // String / IRI object (see below) or list of strings / IRI objects |
||
108 | * properties: [...], // List of item property objects (see below) |
||
109 | * value: 'Item value', // Item value (optional) |
||
110 | * id: 'item-1', // Item ID (optional) |
||
111 | * children: [...] // Nested item objects (optional) |
||
112 | * } |
||
113 | * |
||
114 | * The item property objects are expected to be layed out like this: |
||
115 | * |
||
116 | * { |
||
117 | * name: 'name', // Property name |
||
118 | * profile: 'http://microformats.org/profile/', // Profile |
||
119 | * values: [...] // List of property values |
||
120 | * } |
||
121 | * |
||
122 | * Item property values may be either |
||
123 | * |
||
124 | * - a string: Interpreted as simple value |
||
125 | * - an array: Interpreted as alternate simple values |
||
126 | * - an object: Interpreted as an object property (recursively processed) |
||
127 | * |
||
128 | * IRI objects are expected to be layed out like this: |
||
129 | * |
||
130 | * { |
||
131 | * name: 'h-entry', |
||
132 | * profile: 'http://microformats.org/profile/', // Profile (optional) |
||
133 | * } |
||
134 | * |
||
135 | * @param \stdClass $item Raw item |
||
136 | * @return ItemInterface Item instance |
||
137 | */ |
||
138 | 17 | public function __invoke(\stdClass $item) |
|
139 | { |
||
140 | 17 | $type = $this->normalizeEmptyValue($item, 'type'); |
|
141 | 17 | $itemId = $this->normalizeEmptyValue($item, 'id'); |
|
142 | 17 | $itemLanguage = $this->normalizeEmptyValue($item, 'lang'); |
|
143 | 17 | $value = $this->normalizeEmptyValue($item, 'value'); |
|
144 | 17 | $children = isset($item->children) ? array_map([$this, __METHOD__], $item->children) : []; |
|
145 | 17 | $properties = $this->getProperties($item); |
|
146 | 16 | return new Item( |
|
147 | 16 | $this->format, |
|
148 | 16 | $this->propertyListFactory, |
|
149 | 16 | $type, |
|
150 | 16 | $properties, |
|
151 | 16 | $children, |
|
152 | 16 | $itemId, |
|
153 | 16 | $itemLanguage, |
|
154 | 16 | $value |
|
155 | ); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Normalize an empty item value |
||
160 | * |
||
161 | * @param \stdClass $item Item |
||
162 | * @param string $property Property name |
||
163 | * @return string|null Normalized property value |
||
164 | */ |
||
165 | 17 | protected function normalizeEmptyValue(\stdClass $item, $property) |
|
169 | |||
170 | /** |
||
171 | * Prepare item properties |
||
172 | * |
||
173 | * @param \stdClass $item Item |
||
174 | * @return array Properties |
||
175 | */ |
||
176 | 17 | protected function getProperties(\stdClass $item) |
|
186 | |||
187 | /** |
||
188 | * Process a property |
||
189 | * |
||
190 | * @param array $properties Properties |
||
191 | * @param \stdClass $property Property |
||
192 | */ |
||
193 | 16 | protected function processProperty(array &$properties, $property) |
|
206 | |||
207 | /** |
||
208 | * Validate if an object is a valid property |
||
209 | * |
||
210 | * @param \stdClass $property Property |
||
211 | * @return bool Is a valid property |
||
212 | */ |
||
213 | 16 | protected function validatePropertyStructure($property) |
|
217 | |||
218 | /** |
||
219 | * Prepare item property values |
||
220 | * |
||
221 | * @param array $propertyValues Property values |
||
222 | * @return array Expanded property values |
||
223 | * @throws InvalidArgumentException If it's not a list of property values |
||
224 | */ |
||
225 | 15 | protected function getPropertyValues($propertyValues) |
|
237 | |||
238 | /** |
||
239 | * Process a language tagged property value |
||
240 | * |
||
241 | * @param string|\stdClass $propertyValue Property value |
||
242 | * @return array Language and property value |
||
243 | * @throws RuntimeException If this is an invalid language tagged value |
||
244 | */ |
||
245 | 14 | protected function processLanguageTaggedPropertyValue($propertyValue) |
|
262 | } |
||
263 |