Skip to content

Commit 5503902

Browse files
Merge pull request #26 from TheDragonCode/1.x
Improved work with an array of elements
2 parents 84ba20f + bf34af4 commit 5503902

File tree

4 files changed

+73
-6
lines changed

4 files changed

+73
-6
lines changed

README.md

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,15 +268,37 @@ class UserFeedItem extends FeedItem
268268

269269
#### Adding an array of elements
270270

271-
In some cases, you need to place an array of elements with the same names. For example:
271+
In some cases, you need to place an array of elements with the same names.
272+
273+
To do this, add a symbol of `@` to the beginning of the key name:
274+
275+
```php
276+
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
277+
278+
class UserFeedItem extends FeedItem
279+
{
280+
public function toArray(): array
281+
{
282+
return [
283+
'@picture' => $this->model->images,
284+
];
285+
}
286+
}
287+
```
288+
289+
Result:
272290

273291
```xml
292+
274293
<picture>https://via.placeholder.com/640x480.png/009966?text=beatae</picture>
275294
<picture>https://via.placeholder.com/640x480.png/000011?text=deleniti</picture>
276295
<picture>https://via.placeholder.com/640x480.png/009999?text=voluptates</picture>
277296
```
278297

279-
To do this, add a symbol of `@` to the beginning of the key name:
298+
You can also use values for such elements.
299+
To insert them, use the reserved word `@value`.
300+
301+
For example:
280302

281303
```php
282304
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
@@ -286,12 +308,29 @@ class UserFeedItem extends FeedItem
286308
public function toArray(): array
287309
{
288310
return [
289-
'@picture' => $this->model->images,
311+
'@param' => [
312+
[
313+
'@attributes' => ['name' => 'Article'],
314+
'@value' => $this->model->article,
315+
],
316+
[
317+
'@attributes' => ['name' => 'Brand'],
318+
'@value' => $this->model->brand,
319+
],
320+
],
290321
];
291322
}
292323
}
293324
```
294325

326+
Result:
327+
328+
```xml
329+
330+
<param name="Article">GD-PRDCT-1</param>
331+
<param name="Brand">The Best</param>
332+
```
333+
295334
#### Header information
296335

297336
If it is necessary to change the file cap, override the `header` method in the feed class:

src/Services/ConvertToXml.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ protected function performItem(DOMElement $parent, array $items): void
5757
$this->isAttributes($key) => $this->setAttributes($parent, $value),
5858
$this->isCData($key) => $this->setCData($parent, $value),
5959
$this->isMixed($key) => $this->setMixed($parent, $value),
60-
$this->isArray($key) => $this->setItemsArray($parent, $value, $key),
60+
$this->isValue($key) => $this->setRaw($parent, $value),
61+
$this->isPrefixed($key) => $this->setItemsArray($parent, $value, $key),
6162
default => $this->setItems($parent, $key, $value),
6263
};
6364
}
@@ -78,7 +79,12 @@ protected function isMixed(string $key): bool
7879
return $key === '@mixed';
7980
}
8081

81-
protected function isArray(string $key): bool
82+
protected function isValue(string $key): bool
83+
{
84+
return $key === '@value';
85+
}
86+
87+
protected function isPrefixed(string $key): bool
8288
{
8389
return str_starts_with($key, '@');
8490
}
@@ -110,7 +116,7 @@ protected function setMixed(DOMElement $element, string $value): void
110116
$element->appendChild($fragment);
111117
}
112118

113-
protected function setItemsArray(DOMElement $parent, mixed $value, string $key): void
119+
protected function setItemsArray(DOMElement $parent, $value, string $key): void
114120
{
115121
$key = Str::substr($key, 1);
116122

@@ -130,6 +136,11 @@ protected function setItems(DOMElement $parent, string $key, mixed $value): void
130136
$parent->appendChild($element);
131137
}
132138

139+
protected function setRaw(DOMElement $parent, mixed $value): void
140+
{
141+
$parent->nodeValue = $this->convertValue($value);
142+
}
143+
133144
protected function toXml(DOMElement $item): string
134145
{
135146
return $this->document->saveXML($item);

tests/.pest/snapshots/Feature/YandexTest/export.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
<currencyId>RUR</currencyId>
2626
<vendor>The Best</vendor>
2727
<picture>https://via.placeholder.com/640x480.png/008877?text=repudiandae</picture>
28+
<param name="Article">GD-PRDCT-1</param>
29+
<param name="Brand">The Best</param>
2830
</offer>
2931
<offer id="2" available="true" type="vendor.model">
3032
<url>http://localhost/products/GD-PRDCT-2</url>
@@ -38,6 +40,8 @@
3840
<picture>https://via.placeholder.com/640x480.png/009966?text=beatae</picture>
3941
<picture>https://via.placeholder.com/640x480.png/000011?text=deleniti</picture>
4042
<picture>https://via.placeholder.com/640x480.png/009999?text=voluptates</picture>
43+
<param name="Article">GD-PRDCT-2</param>
44+
<param name="Brand">The Best</param>
4145
</offer>
4246
<offer id="3" available="false" type="vendor.model">
4347
<url>http://localhost/products/GD-PRDCT-3</url>
@@ -50,6 +54,8 @@
5054
<vendor>The Best</vendor>
5155
<picture>https://via.placeholder.com/640x480.png/000044?text=asperiores</picture>
5256
<picture>https://via.placeholder.com/640x480.png/0055ff?text=expedita</picture>
57+
<param name="Article">GD-PRDCT-3</param>
58+
<param name="Brand">The Best</param>
5359
</offer>
5460
</offers>
5561

workbench/app/Feeds/Items/YandexFeedItem.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ public function toArray(): array
4141
'vendor' => $this->model->brand,
4242

4343
'@picture' => $this->model->images,
44+
45+
'@param' => [
46+
[
47+
'@attributes' => ['name' => 'Article'],
48+
'@value' => $this->model->article,
49+
],
50+
[
51+
'@attributes' => ['name' => 'Brand'],
52+
'@value' => $this->model->brand,
53+
],
54+
],
4455
];
4556
}
4657
}

0 commit comments

Comments
 (0)