This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the Bukashk0zzzYmlGenerator |
||
5 | * |
||
6 | * (c) Denis Golubovskiy <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Bukashk0zzz\YmlGenerator; |
||
13 | |||
14 | use Bukashk0zzz\YmlGenerator\Model\Category; |
||
15 | use Bukashk0zzz\YmlGenerator\Model\Currency; |
||
16 | use Bukashk0zzz\YmlGenerator\Model\Delivery; |
||
17 | use Bukashk0zzz\YmlGenerator\Model\Offer\OfferCondition; |
||
18 | use Bukashk0zzz\YmlGenerator\Model\Offer\OfferGroupAwareInterface; |
||
19 | use Bukashk0zzz\YmlGenerator\Model\Offer\OfferInterface; |
||
20 | use Bukashk0zzz\YmlGenerator\Model\Offer\OfferParam; |
||
21 | use Bukashk0zzz\YmlGenerator\Model\ShopInfo; |
||
22 | |||
23 | /** |
||
24 | * Class Generator |
||
25 | */ |
||
26 | class Generator |
||
27 | { |
||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $tmpFile; |
||
32 | |||
33 | /** |
||
34 | * @var \XMLWriter |
||
35 | */ |
||
36 | protected $writer; |
||
37 | |||
38 | /** |
||
39 | * @var Settings |
||
40 | */ |
||
41 | protected $settings; |
||
42 | |||
43 | /** |
||
44 | * Generator constructor. |
||
45 | * |
||
46 | * @param Settings $settings |
||
47 | */ |
||
48 | public function __construct($settings = null) |
||
49 | { |
||
50 | $this->settings = $settings instanceof Settings ? $settings : new Settings(); |
||
51 | $this->writer = new \XMLWriter(); |
||
52 | |||
53 | if ($this->settings->getOutputFile() !== null && $this->settings->getReturnResultYMLString()) { |
||
54 | throw new \LogicException('Only one destination need to be used ReturnResultYMLString or OutputFile'); |
||
55 | } |
||
56 | |||
57 | if ($this->settings->getReturnResultYMLString()) { |
||
58 | $this->writer->openMemory(); |
||
59 | } else { |
||
60 | $this->tmpFile = $this->settings->getOutputFile() !== null ? \tempnam(\sys_get_temp_dir(), 'YMLGenerator') : 'php://output'; |
||
61 | $this->writer->openURI($this->tmpFile); |
||
62 | } |
||
63 | |||
64 | if ($this->settings->getIndentString()) { |
||
65 | $this->writer->setIndentString($this->settings->getIndentString()); |
||
66 | $this->writer->setIndent(true); |
||
67 | } |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @param ShopInfo $shopInfo |
||
72 | * @param array $currencies |
||
73 | * @param array $categories |
||
74 | * @param array $offers |
||
75 | * @param array $deliveries |
||
76 | * |
||
77 | * @return bool |
||
78 | */ |
||
79 | public function generate(ShopInfo $shopInfo, array $currencies, array $categories, array $offers, array $deliveries = []) |
||
80 | { |
||
81 | try { |
||
82 | $this->addHeader(); |
||
83 | |||
84 | $this->addShopInfo($shopInfo); |
||
85 | $this->addCurrencies($currencies); |
||
86 | $this->addCategories($categories); |
||
87 | |||
88 | if (\count($deliveries) !== 0) { |
||
89 | $this->addDeliveries($deliveries); |
||
90 | } |
||
91 | |||
92 | $this->addOffers($offers); |
||
93 | $this->addFooter(); |
||
94 | |||
95 | if ($this->settings->getReturnResultYMLString()) { |
||
96 | return $this->writer->flush(); |
||
97 | } |
||
98 | |||
99 | if (null !== $this->settings->getOutputFile()) { |
||
100 | \copy($this->tmpFile, $this->settings->getOutputFile()); |
||
101 | @\unlink($this->tmpFile); |
||
0 ignored issues
–
show
|
|||
102 | } |
||
103 | |||
104 | return true; |
||
105 | } catch (\Exception $exception) { |
||
106 | throw new \RuntimeException(\sprintf('Problem with generating YML file: %s', $exception->getMessage()), 0, $exception); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Add document header |
||
112 | */ |
||
113 | protected function addHeader() |
||
114 | { |
||
115 | $this->writer->startDocument('1.0', $this->settings->getEncoding()); |
||
116 | $this->writer->startDTD('yml_catalog', null, 'shops.dtd'); |
||
117 | $this->writer->endDTD(); |
||
118 | $this->writer->startElement('yml_catalog'); |
||
119 | $this->writer->writeAttribute('date', \date('Y-m-d H:i')); |
||
120 | $this->writer->startElement('shop'); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Add document footer |
||
125 | */ |
||
126 | protected function addFooter() |
||
127 | { |
||
128 | $this->writer->fullEndElement(); |
||
129 | $this->writer->fullEndElement(); |
||
130 | $this->writer->endDocument(); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Adds shop element data. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#shop) |
||
135 | * |
||
136 | * @param ShopInfo $shopInfo |
||
137 | */ |
||
138 | protected function addShopInfo(ShopInfo $shopInfo) |
||
139 | { |
||
140 | foreach ($shopInfo->toArray() as $name => $value) { |
||
141 | if ($value !== null) { |
||
142 | $this->writer->writeElement($name, $value); |
||
143 | } |
||
144 | } |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @param Currency $currency |
||
149 | */ |
||
150 | protected function addCurrency(Currency $currency) |
||
151 | { |
||
152 | $this->writer->startElement('currency'); |
||
153 | $this->writer->writeAttribute('id', $currency->getId()); |
||
154 | $this->writer->writeAttribute('rate', $currency->getRate()); |
||
155 | $this->writer->endElement(); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @param Category $category |
||
160 | */ |
||
161 | protected function addCategory(Category $category) |
||
162 | { |
||
163 | $this->writer->startElement('category'); |
||
164 | $this->writer->writeAttribute('id', $category->getId()); |
||
165 | |||
166 | if ($category->getParentId() !== null) { |
||
167 | $this->writer->writeAttribute('parentId', $category->getParentId()); |
||
168 | } |
||
169 | |||
170 | if (!empty($category->getAttributes())) { |
||
171 | foreach ($category->getAttributes() as $attribute) { |
||
172 | $this->writer->writeAttribute($attribute['name'], $attribute['value']); |
||
173 | } |
||
174 | } |
||
175 | |||
176 | $this->writer->text($category->getName()); |
||
177 | $this->writer->fullEndElement(); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * @param Delivery $delivery |
||
182 | */ |
||
183 | protected function addDelivery(Delivery $delivery) |
||
184 | { |
||
185 | $this->writer->startElement('option'); |
||
186 | $this->writer->writeAttribute('cost', $delivery->getCost()); |
||
187 | $this->writer->writeAttribute('days', $delivery->getDays()); |
||
188 | if ($delivery->getOrderBefore() !== null) { |
||
189 | $this->writer->writeAttribute('order-before', $delivery->getOrderBefore()); |
||
190 | } |
||
191 | $this->writer->endElement(); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @param OfferInterface $offer |
||
196 | */ |
||
197 | protected function addOffer(OfferInterface $offer) |
||
198 | { |
||
199 | $this->writer->startElement('offer'); |
||
200 | $this->writer->writeAttribute('id', $offer->getId()); |
||
201 | $this->writer->writeAttribute('available', $offer->isAvailable() ? 'true' : 'false'); |
||
202 | |||
203 | if ($offer->getType() !== null) { |
||
204 | $this->writer->writeAttribute('type', $offer->getType()); |
||
205 | } |
||
206 | |||
207 | if ($offer instanceof OfferGroupAwareInterface && $offer->getGroupId() !== null) { |
||
208 | $this->writer->writeAttribute('group_id', $offer->getGroupId()); |
||
209 | } |
||
210 | |||
211 | foreach ($offer->toArray() as $name => $value) { |
||
212 | if (\is_array($value)) { |
||
213 | foreach ($value as $itemValue) { |
||
214 | $this->addOfferElement($name, $itemValue); |
||
215 | } |
||
216 | } else { |
||
217 | $this->addOfferElement($name, $value); |
||
218 | } |
||
219 | } |
||
220 | $this->addOfferParams($offer); |
||
221 | $this->addOfferDeliveryOptions($offer); |
||
222 | $this->addOfferCondition($offer); |
||
223 | |||
224 | $this->writer->fullEndElement(); |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Adds <currencies> element. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#currencies) |
||
229 | * |
||
230 | * @param array $currencies |
||
231 | */ |
||
232 | private function addCurrencies(array $currencies) |
||
233 | { |
||
234 | $this->writer->startElement('currencies'); |
||
235 | |||
236 | /** @var Currency $currency */ |
||
237 | foreach ($currencies as $currency) { |
||
238 | if ($currency instanceof Currency) { |
||
239 | $this->addCurrency($currency); |
||
240 | } |
||
241 | } |
||
242 | |||
243 | $this->writer->fullEndElement(); |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Adds <categories> element. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#categories) |
||
248 | * |
||
249 | * @param array $categories |
||
250 | */ |
||
251 | private function addCategories(array $categories) |
||
252 | { |
||
253 | $this->writer->startElement('categories'); |
||
254 | |||
255 | /** @var Category $category */ |
||
256 | foreach ($categories as $category) { |
||
257 | if ($category instanceof Category) { |
||
258 | $this->addCategory($category); |
||
259 | } |
||
260 | } |
||
261 | |||
262 | $this->writer->fullEndElement(); |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * Adds <delivery-option> element. (See https://yandex.ru/support/partnermarket/elements/delivery-options.xml) |
||
267 | * |
||
268 | * @param array $deliveries |
||
269 | */ |
||
270 | private function addDeliveries(array $deliveries) |
||
271 | { |
||
272 | $this->writer->startElement('delivery-options'); |
||
273 | |||
274 | /** @var Delivery $delivery */ |
||
275 | foreach ($deliveries as $delivery) { |
||
276 | if ($delivery instanceof Delivery) { |
||
277 | $this->addDelivery($delivery); |
||
278 | } |
||
279 | } |
||
280 | |||
281 | $this->writer->fullEndElement(); |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Adds <offers> element. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#offers) |
||
286 | * |
||
287 | * @param array $offers |
||
288 | */ |
||
289 | private function addOffers(array $offers) |
||
290 | { |
||
291 | $this->writer->startElement('offers'); |
||
292 | |||
293 | /** @var OfferInterface $offer */ |
||
294 | foreach ($offers as $offer) { |
||
295 | if ($offer instanceof OfferInterface) { |
||
296 | $this->addOffer($offer); |
||
297 | } |
||
298 | } |
||
299 | |||
300 | $this->writer->fullEndElement(); |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * @param OfferInterface $offer |
||
305 | */ |
||
306 | private function addOfferDeliveryOptions(OfferInterface $offer) |
||
307 | { |
||
308 | $options = $offer->getDeliveryOptions(); |
||
309 | if (!empty($options)) { |
||
310 | $this->addDeliveries($options); |
||
311 | } |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * @param OfferInterface $offer |
||
316 | */ |
||
317 | private function addOfferParams(OfferInterface $offer) |
||
318 | { |
||
319 | /** @var OfferParam $param */ |
||
320 | foreach ($offer->getParams() as $param) { |
||
321 | if ($param instanceof OfferParam) { |
||
322 | $this->writer->startElement('param'); |
||
323 | |||
324 | $this->writer->writeAttribute('name', $param->getName()); |
||
325 | if ($param->getUnit()) { |
||
326 | $this->writer->writeAttribute('unit', $param->getUnit()); |
||
327 | } |
||
328 | $this->writer->text($param->getValue()); |
||
329 | |||
330 | $this->writer->endElement(); |
||
331 | } |
||
332 | } |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * @param OfferInterface $offer |
||
337 | */ |
||
338 | private function addOfferCondition(OfferInterface $offer) |
||
339 | { |
||
340 | $params = $offer->getCondition(); |
||
341 | if ($params instanceof OfferCondition) { |
||
342 | $this->writer->startElement('condition'); |
||
343 | $this->writer->writeAttribute('type', $params->getType()); |
||
344 | $this->writer->writeElement('reason', $params->getReasonText()); |
||
345 | $this->writer->endElement(); |
||
346 | } |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * @param string $name |
||
351 | * @param mixed $value |
||
352 | * |
||
353 | * @return bool |
||
354 | */ |
||
355 | private function addOfferElement($name, $value) |
||
356 | { |
||
357 | if ($value === null) { |
||
358 | return false; |
||
359 | } |
||
360 | |||
361 | if ($value instanceof Cdata) { |
||
362 | $this->writer->startElement($name); |
||
363 | $this->writer->writeCdata((string) $value); |
||
364 | $this->writer->endElement(); |
||
365 | |||
366 | return true; |
||
367 | } |
||
368 | |||
369 | if (\is_bool($value)) { |
||
370 | $value = $value ? 'true' : 'false'; |
||
371 | } |
||
372 | $this->writer->writeElement($name, $value); |
||
373 | |||
374 | return true; |
||
375 | } |
||
376 | } |
||
377 |
If you suppress an error, we recommend checking for the error condition explicitly: