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
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,39 @@ class UserFeed extends Feed
}
```

#### Setting the head info

In some cases, you need to add various information to the beginning of the file.
To do this, use the `info` method:

```php
use DragonCode\LaravelFeed\Data\ElementData;
use DragonCode\LaravelFeed\Feeds\Feed;
use DragonCode\LaravelFeed\Feeds\Info\FeedInfo;

class UserFeed extends Feed
{
public function info(): FeedInfo
{
return new FeedInfo();
}
}
```

```php
use DragonCode\LaravelFeed\Feeds\Info\FeedInfo;

class UserFeedInfo extends FeedInfo
{
public function toArray(): array
{
return [
// ...
];
}
}
```

#### Adding attributes for the main section

```php
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"require": {
"php": "^8.2",
"ext-dom": "*",
"ext-libxml": ">=2.6.21",
"illuminate/database": "^11.0 || ^12.0",
"illuminate/filesystem": "^11.0 || ^12.0",
"illuminate/support": "^11.0 || ^12.0",
Expand Down
21 changes: 21 additions & 0 deletions ide.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,27 @@
}
}
]
},
{
"id": "dragon-code.xml-feeds.info",
"name": "Create XML Feed Info",
"classSuffix": "FeedInfo",
"regex": ".+",
"files": [
{
"appNamespace": "Feeds\\Info",
"name": "${INPUT_CLASS|replace: ,_|className|upperCamelCase}.php",
"template": {
"type": "stub",
"path": "/stubs/feed_info.stub",
"fallbackPath": "stubs/feed_info.stub",
"parameters": {
"DummyNamespace": "${INPUT_FQN|namespace}",
"DummyClass": "${INPUT_CLASS|replace: ,_|className|upperCamelCase}"
}
}
}
]
}
]
}
35 changes: 35 additions & 0 deletions src/Console/Commands/FeedInfoMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace DragonCode\LaravelFeed\Console\Commands;

use DragonCode\LaravelFeed\Concerns\InteractsWithName;
use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputOption;

#[AsCommand('make:feed-info', 'Create a new feed info')]
class FeedInfoMakeCommand extends GeneratorCommand
{
use InteractsWithName;

protected $type = 'FeedInfo';

protected function getStub(): string
{
return __DIR__ . '/../../../stubs/feed_info.php';
}

protected function getDefaultNamespace($rootNamespace): string
{
return $rootNamespace . '\Feeds\Info';
}

protected function getOptions(): array
{
return [
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the feed already exists'],
];
}
}
6 changes: 6 additions & 0 deletions src/Feeds/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace DragonCode\LaravelFeed\Feeds;

use DragonCode\LaravelFeed\Data\ElementData;
use DragonCode\LaravelFeed\Feeds\Info\FeedInfo;
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Database\Eloquent\Builder;
Expand Down Expand Up @@ -47,6 +48,11 @@ public function root(): ElementData
return new ElementData;
}

public function info(): FeedInfo
{
return new FeedInfo;
}

public function filename(): string
{
return $this->filename ??= Str::kebab(class_basename($this)) . '.xml';
Expand Down
15 changes: 15 additions & 0 deletions src/Feeds/Info/FeedInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace DragonCode\LaravelFeed\Feeds\Info;

use Illuminate\Contracts\Support\Arrayable;

class FeedInfo implements Arrayable
{
public function toArray(): array
{
return [];
}
}
4 changes: 3 additions & 1 deletion src/LaravelFeedServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace DragonCode\LaravelFeed;

use DragonCode\LaravelFeed\Console\Commands\FeedGenerateCommand;
use DragonCode\LaravelFeed\Console\Commands\FeedInfoMakeCommand;
use DragonCode\LaravelFeed\Console\Commands\FeedItemMakeCommand;
use DragonCode\LaravelFeed\Console\Commands\FeedMakeCommand;
use Illuminate\Support\ServiceProvider;
Expand Down Expand Up @@ -37,8 +38,9 @@ protected function registerCommands(): void
{
$this->commands([
FeedGenerateCommand::class,
FeedMakeCommand::class,
FeedInfoMakeCommand::class,
FeedItemMakeCommand::class,
FeedMakeCommand::class,
]);
}
}
35 changes: 22 additions & 13 deletions src/Services/ConvertToXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace DragonCode\LaravelFeed\Services;

use DOMDocument;
use DOMElement;
use DOMNode;
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
use Illuminate\Container\Attributes\Config;
use Illuminate\Support\Str;
Expand All @@ -31,7 +31,7 @@ public function __construct(
$this->document->preserveWhiteSpace = ! $pretty;
}

public function convert(FeedItem $item): string
public function convertItem(FeedItem $item): string
{
$box = $this->performBox($item);

Expand All @@ -40,7 +40,16 @@ public function convert(FeedItem $item): string
return $this->toXml($box);
}

protected function performBox(FeedItem $item): DOMElement
public function convertInfo(array $info): string
{
$box = $this->document->createDocumentFragment();

$this->performItem($box, $info);

return $this->toXml($box);
}

protected function performBox(FeedItem $item): DOMNode
{
$element = $this->createElement($item->name());

Expand All @@ -51,7 +60,7 @@ protected function performBox(FeedItem $item): DOMElement
return $element;
}

protected function performItem(DOMElement $parent, array $items): void
protected function performItem(DOMNode $parent, array $items): void
{
foreach ($items as $key => $value) {
$key = $this->convertKey($key);
Expand Down Expand Up @@ -92,34 +101,34 @@ protected function isPrefixed(string $key): bool
return str_starts_with($key, '@');
}

protected function createElement(string $name, bool|float|int|string|null $value = ''): DOMElement
protected function createElement(string $name, bool|float|int|string|null $value = ''): DOMNode
{
return $this->document->createElement($name, $this->convertValue($value));
}

protected function setAttributes(DOMElement $element, array $attributes): void
protected function setAttributes(DOMNode $element, array $attributes): void
{
foreach ($attributes as $key => $value) {
$element->setAttribute($key, $this->convertValue($value));
}
}

protected function setCData(DOMElement $element, string $value): void
protected function setCData(DOMNode $element, string $value): void
{
$element->appendChild(
$this->document->createCDATASection($value)
);
}

protected function setMixed(DOMElement $element, string $value): void
protected function setMixed(DOMNode $element, string $value): void
{
$fragment = $this->document->createDocumentFragment();
$fragment->appendXML($value);

$element->appendChild($fragment);
}

protected function setItemsArray(DOMElement $parent, $value, string $key): void
protected function setItemsArray(DOMNode $parent, $value, string $key): void
{
$key = Str::substr($key, 1);

Expand All @@ -128,7 +137,7 @@ protected function setItemsArray(DOMElement $parent, $value, string $key): void
}
}

protected function setItems(DOMElement $parent, string $key, mixed $value): void
protected function setItems(DOMNode $parent, string $key, mixed $value): void
{
$element = $this->createElement($key, is_array($value) ? '' : $this->convertValue($value));

Expand All @@ -139,14 +148,14 @@ protected function setItems(DOMElement $parent, string $key, mixed $value): void
$parent->appendChild($element);
}

protected function setRaw(DOMElement $parent, mixed $value): void
protected function setRaw(DOMNode $parent, mixed $value): void
{
$parent->nodeValue = $this->convertValue($value);
}

protected function toXml(DOMElement $item): string
protected function toXml(DOMNode $item): string
{
return $this->document->saveXML($item);
return $this->document->saveXML($item, LIBXML_COMPACT);
}

protected function convertKey(int|string $key): string
Expand Down
31 changes: 25 additions & 6 deletions src/Services/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use DragonCode\LaravelFeed\Feeds\Feed;
use Illuminate\Database\Eloquent\Collection;

use function blank;
use function collect;
use function implode;
use function sprintf;
Expand All @@ -26,6 +27,8 @@ public function feed(Feed $feed): void
);

$this->performHeader($file, $feed);
$this->performInfo($file, $feed);
$this->performRoot($file, $feed);
$this->performItem($file, $feed);
$this->performFooter($file, $feed);

Expand All @@ -38,7 +41,7 @@ protected function performItem($file, Feed $feed): void
$content = [];

foreach ($models as $model) {
$content[] = $this->converter->convert(
$content[] = $this->converter->convertItem(
$feed->item($model)
);
}
Expand All @@ -49,14 +52,30 @@ protected function performItem($file, Feed $feed): void

protected function performHeader($file, Feed $feed): void
{
$value = $feed->header();
$this->append($file, $feed->header());
}

if ($name = $feed->root()->name) {
$value .= ! empty($feed->root()->attributes)
? sprintf("\n<%s %s>\n", $name, $this->makeRootAttributes($feed->root()))
: sprintf("\n<%s>\n", $name);
protected function performInfo($file, Feed $feed): void
{
if (blank($info = $feed->info()->toArray())) {
return;
}

$value = $this->converter->convertInfo($info);

$this->append($file, PHP_EOL . $value);
}

protected function performRoot($file, Feed $feed): void
{
if (! $name = $feed->root()->name) {
return;
}

$value = ! empty($feed->root()->attributes)
? sprintf("\n<%s %s>\n", $name, $this->makeRootAttributes($feed->root()))
: sprintf("\n<%s>\n", $name);

$this->append($file, $value);
}

Expand Down
17 changes: 17 additions & 0 deletions stubs/feed_info.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace DummyNamespace;

use DragonCode\LaravelFeed\Feeds\Info\FeedInfo;

class DummyClass extends FeedInfo
{
public function toArray(): array
{
return [
//
];
}
}
27 changes: 14 additions & 13 deletions tests/.pest/snapshots/Feature/YandexTest/export.snap
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
<!DOCTYPE yml_catalog SYSTEM "shops.dtd">
<yml_catalog date="2025-08-30T21:14:49+00:00">
<shop>
<name>Laravel</name>
<company>Laravel</company>
<url>http://localhost</url>
<platform>Laravel</platform>
<email>test@example.com</email>
<currencies>
<currency id="RUR" rate="1"/>
</currencies>
<categories>
<category id="41">Домашние майки</category>
<category id="539">Велосипедки</category>
<category id="44">Ремни</category>
</categories>
<name>Laravel</name>
<company>Laravel</company>
<platform>Laravel</platform>
<url>http://localhost</url>
<email>test@example.com</email>
<currencies>
<currency id="RUR" rate="1"/>
</currencies>
<categories>
<category id="41">Домашние майки</category>
<category id="539">Велосипедки</category>
<category id="44">Ремни</category>
</categories>

<offers>
<offer id="1" available="true" type="vendor.model">
<url>http://localhost/products/GD-PRDCT-1</url>
Expand Down
Loading