Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<picture>https://via.placeholder.com/640x480.png/009966?text=beatae</picture>
<picture>https://via.placeholder.com/640x480.png/000011?text=deleniti</picture>
<picture>https://via.placeholder.com/640x480.png/009999?text=voluptates</picture>
```

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;
Expand All @@ -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

<param name="Article">GD-PRDCT-1</param>
<param name="Brand">The Best</param>
```

#### Header information

If it is necessary to change the file cap, override the `header` method in the feed class:
Expand Down
17 changes: 14 additions & 3 deletions src/Services/ConvertToXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
}
Expand All @@ -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, '@');
}
Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions tests/.pest/snapshots/Feature/YandexTest/export.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
<currencyId>RUR</currencyId>
<vendor>The Best</vendor>
<picture>https://via.placeholder.com/640x480.png/008877?text=repudiandae</picture>
<param name="Article">GD-PRDCT-1</param>
<param name="Brand">The Best</param>
</offer>
<offer id="2" available="true" type="vendor.model">
<url>http://localhost/products/GD-PRDCT-2</url>
Expand All @@ -38,6 +40,8 @@
<picture>https://via.placeholder.com/640x480.png/009966?text=beatae</picture>
<picture>https://via.placeholder.com/640x480.png/000011?text=deleniti</picture>
<picture>https://via.placeholder.com/640x480.png/009999?text=voluptates</picture>
<param name="Article">GD-PRDCT-2</param>
<param name="Brand">The Best</param>
</offer>
<offer id="3" available="false" type="vendor.model">
<url>http://localhost/products/GD-PRDCT-3</url>
Expand All @@ -50,6 +54,8 @@
<vendor>The Best</vendor>
<picture>https://via.placeholder.com/640x480.png/000044?text=asperiores</picture>
<picture>https://via.placeholder.com/640x480.png/0055ff?text=expedita</picture>
<param name="Article">GD-PRDCT-3</param>
<param name="Brand">The Best</param>
</offer>
</offers>

Expand Down
11 changes: 11 additions & 0 deletions workbench/app/Feeds/Items/YandexFeedItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
],
];
}
}