Skip to content

Commit 84ba20f

Browse files
Merge pull request #25 from TheDragonCode/1.x
Added generation of arrays of values without nesting
2 parents 8c15854 + c38d92b commit 84ba20f

File tree

7 files changed

+76
-22
lines changed

7 files changed

+76
-22
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,32 @@ class UserFeedItem extends FeedItem
266266
}
267267
```
268268

269+
#### Adding an array of elements
270+
271+
In some cases, you need to place an array of elements with the same names. For example:
272+
273+
```xml
274+
<picture>https://via.placeholder.com/640x480.png/009966?text=beatae</picture>
275+
<picture>https://via.placeholder.com/640x480.png/000011?text=deleniti</picture>
276+
<picture>https://via.placeholder.com/640x480.png/009999?text=voluptates</picture>
277+
```
278+
279+
To do this, add a symbol of `@` to the beginning of the key name:
280+
281+
```php
282+
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
283+
284+
class UserFeedItem extends FeedItem
285+
{
286+
public function toArray(): array
287+
{
288+
return [
289+
'@picture' => $this->model->images,
290+
];
291+
}
292+
}
293+
```
294+
269295
#### Header information
270296

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

src/Services/ConvertToXml.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
use DOMElement;
99
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
1010
use Illuminate\Container\Attributes\Config;
11+
use Illuminate\Support\Str;
1112

1213
use function htmlspecialchars;
1314
use function is_array;
15+
use function is_bool;
1416
use function preg_replace;
1517
use function str_replace;
18+
use function str_starts_with;
1619

1720
class ConvertToXml
1821
{
@@ -54,6 +57,7 @@ protected function performItem(DOMElement $parent, array $items): void
5457
$this->isAttributes($key) => $this->setAttributes($parent, $value),
5558
$this->isCData($key) => $this->setCData($parent, $value),
5659
$this->isMixed($key) => $this->setMixed($parent, $value),
60+
$this->isArray($key) => $this->setItemsArray($parent, $value, $key),
5761
default => $this->setItems($parent, $key, $value),
5862
};
5963
}
@@ -74,6 +78,11 @@ protected function isMixed(string $key): bool
7478
return $key === '@mixed';
7579
}
7680

81+
protected function isArray(string $key): bool
82+
{
83+
return str_starts_with($key, '@');
84+
}
85+
7786
protected function createElement(string $name, bool|float|int|string|null $value = ''): DOMElement
7887
{
7988
return $this->document->createElement($name, $this->convertValue($value));
@@ -101,6 +110,15 @@ protected function setMixed(DOMElement $element, string $value): void
101110
$element->appendChild($fragment);
102111
}
103112

113+
protected function setItemsArray(DOMElement $parent, mixed $value, string $key): void
114+
{
115+
$key = Str::substr($key, 1);
116+
117+
foreach ($value as $item) {
118+
$this->setItems($parent, $key, $item);
119+
}
120+
}
121+
104122
protected function setItems(DOMElement $parent, string $key, mixed $value): void
105123
{
106124
$element = $this->createElement($key, is_array($value) ? '' : $this->convertValue($value));
@@ -117,13 +135,17 @@ protected function toXml(DOMElement $item): string
117135
return $this->document->saveXML($item);
118136
}
119137

120-
protected function convertKey(string $key): string
138+
protected function convertKey(int|string $key): string
121139
{
122-
return str_replace(' ', '_', $key);
140+
return str_replace(' ', '_', (string) $key);
123141
}
124142

125143
protected function convertValue(bool|float|int|string|null $value): string
126144
{
145+
if (is_bool($value)) {
146+
return $value ? 'true' : 'false';
147+
}
148+
127149
return $this->removeControlCharacters(
128150
htmlspecialchars((string) $value)
129151
);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<price>100</price>
2525
<currencyId>RUR</currencyId>
2626
<vendor>The Best</vendor>
27+
<picture>https://via.placeholder.com/640x480.png/008877?text=repudiandae</picture>
2728
</offer>
2829
<offer id="2" available="true" type="vendor.model">
2930
<url>http://localhost/products/GD-PRDCT-2</url>
@@ -34,6 +35,9 @@
3435
<price>250</price>
3536
<currencyId>RUR</currencyId>
3637
<vendor>The Best</vendor>
38+
<picture>https://via.placeholder.com/640x480.png/009966?text=beatae</picture>
39+
<picture>https://via.placeholder.com/640x480.png/000011?text=deleniti</picture>
40+
<picture>https://via.placeholder.com/640x480.png/009999?text=voluptates</picture>
3741
</offer>
3842
<offer id="3" available="false" type="vendor.model">
3943
<url>http://localhost/products/GD-PRDCT-3</url>
@@ -44,6 +48,8 @@
4448
<price>400</price>
4549
<currencyId>RUR</currencyId>
4650
<vendor>The Best</vendor>
51+
<picture>https://via.placeholder.com/640x480.png/000044?text=asperiores</picture>
52+
<picture>https://via.placeholder.com/640x480.png/0055ff?text=expedita</picture>
4753
</offer>
4854
</offers>
4955

workbench/app/Data/ProductFakeData.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace Workbench\App\Data;
66

7-
use function json_encode;
8-
97
class ProductFakeData
108
{
119
public static function toArray(): array
@@ -22,9 +20,9 @@ public static function toArray(): array
2220

2321
'brand' => 'The Best',
2422

25-
'images' => json_encode([
26-
'https://via.placeholder.com/640x480.png/00ff55?text=s1',
27-
]),
23+
'images' => [
24+
'https://via.placeholder.com/640x480.png/008877?text=repudiandae',
25+
],
2826

2927
'created_at' => '2025-08-31 00:00:00',
3028
'updated_at' => '2025-08-31 20:00:00',
@@ -40,11 +38,11 @@ public static function toArray(): array
4038

4139
'brand' => 'The Best',
4240

43-
'images' => json_encode([
44-
'https://via.placeholder.com/640x480.png/00ff55?text=s20',
45-
'https://via.placeholder.com/640x480.png/00ff55?text=s21',
46-
'https://via.placeholder.com/640x480.png/00ff55?text=s22',
47-
]),
41+
'images' => [
42+
'https://via.placeholder.com/640x480.png/009966?text=beatae',
43+
'https://via.placeholder.com/640x480.png/000011?text=deleniti',
44+
'https://via.placeholder.com/640x480.png/009999?text=voluptates',
45+
],
4846

4947
'created_at' => '2025-08-30 00:00:00',
5048
'updated_at' => '2025-08-30 19:00:00',
@@ -60,10 +58,10 @@ public static function toArray(): array
6058

6159
'brand' => 'The Best',
6260

63-
'images' => json_encode([
64-
'https://via.placeholder.com/640x480.png/00ff55?text=s30',
65-
'https://via.placeholder.com/640x480.png/00ff55?text=s31',
66-
]),
61+
'images' => [
62+
'https://via.placeholder.com/640x480.png/000044?text=asperiores',
63+
'https://via.placeholder.com/640x480.png/0055ff?text=expedita',
64+
],
6765

6866
'created_at' => '2025-08-29 00:00:00',
6967
'updated_at' => '2025-08-29 18:00:00',

workbench/app/Feeds/Items/YandexFeedItem.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function attributes(): array
1919
return [
2020
'id' => $this->model->id,
2121

22-
'available' => ! empty($this->model->quantity) ? 'true' : 'false',
22+
'available' => ! empty($this->model->quantity),
2323

2424
'type' => 'vendor.model',
2525
];
@@ -40,7 +40,7 @@ public function toArray(): array
4040
'currencyId' => 'RUR',
4141
'vendor' => $this->model->brand,
4242

43-
// 'picture' => $this->model->images,
43+
'@picture' => $this->model->images,
4444
];
4545
}
4646
}

workbench/app/Models/Product.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ class Product extends Model
2727

2828
'images',
2929
];
30+
31+
protected $casts = [
32+
'images' => 'array',
33+
];
3034
}

workbench/database/factories/ProductFactory.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
use Illuminate\Database\Eloquent\Factories\Factory;
88
use Illuminate\Support\Str;
99

10-
use function json_encode;
11-
1210
class ProductFactory extends Factory
1311
{
1412
public function definition(): array
@@ -24,11 +22,11 @@ public function definition(): array
2422
'quantity' => fake()->numberBetween(0, 10),
2523
'currency' => fake()->currencyCode(),
2624

27-
'images' => json_encode([
25+
'images' => [
2826
fake()->imageUrl(),
2927
fake()->imageUrl(),
3028
fake()->imageUrl(),
31-
]),
29+
],
3230
];
3331
}
3432
}

0 commit comments

Comments
 (0)