diff --git a/README.md b/README.md index 6a5ce94..a20d54b 100644 --- a/README.md +++ b/README.md @@ -268,15 +268,37 @@ class UserFeedItem extends FeedItem #### Adding an array of elements -In some cases, you need to place an array of elements with the same names. For example: +In some cases, you need to place an array of elements with the same names. + +To do this, add a symbol of `@` to the beginning of the key name: + +```php +use DragonCode\LaravelFeed\Feeds\Items\FeedItem; + +class UserFeedItem extends FeedItem +{ + public function toArray(): array + { + return [ + '@picture' => $this->model->images, + ]; + } +} +``` + +Result: ```xml + https://via.placeholder.com/640x480.png/009966?text=beatae https://via.placeholder.com/640x480.png/000011?text=deleniti https://via.placeholder.com/640x480.png/009999?text=voluptates ``` -To do this, add a symbol of `@` to the beginning of the key name: +You can also use values for such elements. +To insert them, use the reserved word `@value`. + +For example: ```php use DragonCode\LaravelFeed\Feeds\Items\FeedItem; @@ -286,12 +308,29 @@ class UserFeedItem extends FeedItem public function toArray(): array { return [ - '@picture' => $this->model->images, + '@param' => [ + [ + '@attributes' => ['name' => 'Article'], + '@value' => $this->model->article, + ], + [ + '@attributes' => ['name' => 'Brand'], + '@value' => $this->model->brand, + ], + ], ]; } } ``` +Result: + +```xml + +GD-PRDCT-1 +The Best +``` + #### Header information If it is necessary to change the file cap, override the `header` method in the feed class: diff --git a/src/Services/ConvertToXml.php b/src/Services/ConvertToXml.php index 75f6d9c..d16a2f2 100644 --- a/src/Services/ConvertToXml.php +++ b/src/Services/ConvertToXml.php @@ -57,7 +57,8 @@ protected function performItem(DOMElement $parent, array $items): void $this->isAttributes($key) => $this->setAttributes($parent, $value), $this->isCData($key) => $this->setCData($parent, $value), $this->isMixed($key) => $this->setMixed($parent, $value), - $this->isArray($key) => $this->setItemsArray($parent, $value, $key), + $this->isValue($key) => $this->setRaw($parent, $value), + $this->isPrefixed($key) => $this->setItemsArray($parent, $value, $key), default => $this->setItems($parent, $key, $value), }; } @@ -78,7 +79,12 @@ protected function isMixed(string $key): bool return $key === '@mixed'; } - protected function isArray(string $key): bool + protected function isValue(string $key): bool + { + return $key === '@value'; + } + + protected function isPrefixed(string $key): bool { return str_starts_with($key, '@'); } @@ -110,7 +116,7 @@ protected function setMixed(DOMElement $element, string $value): void $element->appendChild($fragment); } - protected function setItemsArray(DOMElement $parent, mixed $value, string $key): void + protected function setItemsArray(DOMElement $parent, $value, string $key): void { $key = Str::substr($key, 1); @@ -130,6 +136,11 @@ protected function setItems(DOMElement $parent, string $key, mixed $value): void $parent->appendChild($element); } + protected function setRaw(DOMElement $parent, mixed $value): void + { + $parent->nodeValue = $this->convertValue($value); + } + protected function toXml(DOMElement $item): string { return $this->document->saveXML($item); diff --git a/tests/.pest/snapshots/Feature/YandexTest/export.snap b/tests/.pest/snapshots/Feature/YandexTest/export.snap index 77983d6..62d9649 100644 --- a/tests/.pest/snapshots/Feature/YandexTest/export.snap +++ b/tests/.pest/snapshots/Feature/YandexTest/export.snap @@ -25,6 +25,8 @@ RUR The Best https://via.placeholder.com/640x480.png/008877?text=repudiandae + GD-PRDCT-1 + The Best http://localhost/products/GD-PRDCT-2 @@ -38,6 +40,8 @@ https://via.placeholder.com/640x480.png/009966?text=beatae https://via.placeholder.com/640x480.png/000011?text=deleniti https://via.placeholder.com/640x480.png/009999?text=voluptates + GD-PRDCT-2 + The Best http://localhost/products/GD-PRDCT-3 @@ -50,6 +54,8 @@ The Best https://via.placeholder.com/640x480.png/000044?text=asperiores https://via.placeholder.com/640x480.png/0055ff?text=expedita + GD-PRDCT-3 + The Best diff --git a/workbench/app/Feeds/Items/YandexFeedItem.php b/workbench/app/Feeds/Items/YandexFeedItem.php index 54bcc81..1a5981e 100644 --- a/workbench/app/Feeds/Items/YandexFeedItem.php +++ b/workbench/app/Feeds/Items/YandexFeedItem.php @@ -41,6 +41,17 @@ public function toArray(): array 'vendor' => $this->model->brand, '@picture' => $this->model->images, + + '@param' => [ + [ + '@attributes' => ['name' => 'Article'], + '@value' => $this->model->article, + ], + [ + '@attributes' => ['name' => 'Brand'], + '@value' => $this->model->brand, + ], + ], ]; } }