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
27 changes: 18 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ Each feed can be created in a certain folder of a certain storage.
To indicate the storage, reduce the property of `$storage` in the feed class:

```php
use DragonCode\LaravelFeed\Feeds\Feed;

class UserFeed extends Feed
{
protected string $storage = 'public';
Expand All @@ -85,6 +87,8 @@ By default, `public`.
The path to the file inside the storage is indicated in the `filiname` method:

```php
use DragonCode\LaravelFeed\Feeds\Feed;

class UserFeed extends Feed
{
public function filename(): string
Expand Down Expand Up @@ -176,25 +180,26 @@ According to this example, the XML file with the following contents will be gene
#### Setting the root element

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

class UserFeed extends Feed
{
public function rootItem(): ?string
{
return 'users';
}

public function rootAttributes(): array
public function root(): ElementData
{
return [
'foo' => 'some value',
];
return new ElementData(
name: 'users',
attributes: ['foo' => 'some value']
);
}
}
```

#### Adding attributes for the main section

```php
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;

class UserFeedItem extends FeedItem
{
public function attributes(): array
Expand Down Expand Up @@ -223,6 +228,8 @@ class UserFeedItem extends FeedItem
> - `@mixed`

```php
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;

class UserFeedItem extends FeedItem
{
public function toArray(): array
Expand Down Expand Up @@ -264,6 +271,8 @@ class UserFeedItem extends FeedItem
If it is necessary to change the file cap, override the `header` method in the feed class:

```php
use DragonCode\LaravelFeed\Feeds\Feed;

class UserFeed extends Feed
{
public function header(): string
Expand Down
9 changes: 9 additions & 0 deletions src/Concerns/InteractsWithName.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@
namespace DragonCode\LaravelFeed\Concerns;

use Illuminate\Support\Str;
use Illuminate\Support\Stringable;

use function class_basename;
use function str_replace;

/** @mixin \Illuminate\Console\GeneratorCommand */
trait InteractsWithName
{
protected function getNameInput(): string
{
return Str::of(parent::getNameInput())
->whenEndsWith('Feed', fn (Stringable $str) => $str->substr(0, -4))
->whenEndsWith('FeedItem', fn (Stringable $str) => $str->substr(0, -8))
->toString();
}

protected function qualifyClass($name): string
{
return Str::finish(parent::qualifyClass($name), $this->type);
Expand Down
13 changes: 13 additions & 0 deletions src/Data/ElementData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace DragonCode\LaravelFeed\Data;

readonly class ElementData
{
public function __construct(
public ?string $name = null,
public array $attributes = []
) {}
}
10 changes: 3 additions & 7 deletions src/Feeds/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace DragonCode\LaravelFeed\Feeds;

use DragonCode\LaravelFeed\Data\ElementData;
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Database\Eloquent\Builder;
Expand Down Expand Up @@ -41,14 +42,9 @@ public function footer(): string
return '';
}

public function rootItem(): ?string
public function root(): ElementData
{
return null;
}

public function rootAttributes(): array
{
return [];
return new ElementData;
}

public function filename(): string
Expand Down
17 changes: 9 additions & 8 deletions src/Services/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace DragonCode\LaravelFeed\Services;

use DragonCode\LaravelFeed\Data\ElementData;
use DragonCode\LaravelFeed\Feeds\Feed;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Filesystem\Filesystem;
Expand Down Expand Up @@ -56,10 +57,10 @@ protected function performHeader($file, Feed $feed): void
{
$value = $feed->header();

if ($item = $feed->rootItem()) {
$value .= $feed->rootAttributes()
? sprintf("\n<%s %s>\n", $item, $this->makeRootAttributes($feed))
: sprintf("\n<%s>\n", $item);
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);
}

$this->append($file, $value);
Expand All @@ -69,16 +70,16 @@ protected function performFooter($file, Feed $feed): void
{
$value = $feed->footer();

if ($item = $feed->rootItem()) {
$value .= "\n</$item>\n";
if ($name = $feed->root()->name) {
$value .= "\n</$name>\n";
}

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

protected function makeRootAttributes(Feed $feed): string
protected function makeRootAttributes(ElementData $item): string
{
return collect($feed->rootAttributes())
return collect($item->attributes)
->map(fn (mixed $value, int|string $key) => sprintf('%s="%s"', $key, $value))
->implode(' ');
}
Expand Down
7 changes: 4 additions & 3 deletions stubs/feed.stub
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ declare(strict_types=1);

namespace DummyNamespace;

use DragonCode\LaravelFeed\Data\ElementData;
use DragonCode\LaravelFeed\Feeds\Feed;
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
use Illuminate\Database\Eloquent\Builder;
Expand All @@ -17,9 +18,9 @@ class DummyClass extends Feed
return DummyUser::query();
}

public function rootItem(): ?string
public function root(): ElementData
{
return 'users';
return new ElementData('users');
}

public function filename(): string
Expand All @@ -29,6 +30,6 @@ class DummyClass extends Feed

public function item(Model $model): FeedItem
{
return new Items\DummyUserFeedItem($model);
return new Items\DummyClassItem($model);
}
}
7 changes: 4 additions & 3 deletions tests/.pest/snapshots/Unit/Console/MakeTest/make_feed.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ declare(strict_types=1);

namespace App\Feeds;

use DragonCode\LaravelFeed\Data\ElementData;
use DragonCode\LaravelFeed\Feeds\Feed;
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
use Illuminate\Database\Eloquent\Builder;
Expand All @@ -17,9 +18,9 @@ class FooBarFeed extends Feed
return User::query();
}

public function rootItem(): ?string
public function root(): ElementData
{
return 'users';
return new ElementData('users');
}

public function filename(): string
Expand All @@ -29,6 +30,6 @@ class FooBarFeed extends Feed

public function item(Model $model): FeedItem
{
return new Items\UserFeedItem($model);
return new Items\FooBarFeedItem($model);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ declare(strict_types=1);

namespace App\Feeds;

use DragonCode\LaravelFeed\Data\ElementData;
use DragonCode\LaravelFeed\Feeds\Feed;
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
use Illuminate\Database\Eloquent\Builder;
Expand All @@ -17,9 +18,9 @@ class QweRtyFeed extends Feed
return User::query();
}

public function rootItem(): ?string
public function root(): ElementData
{
return 'users';
return new ElementData('users');
}

public function filename(): string
Expand All @@ -29,6 +30,6 @@ class QweRtyFeed extends Feed

public function item(Model $model): FeedItem
{
return new Items\UserFeedItem($model);
return new Items\QweRtyFeedItem($model);
}
}
7 changes: 5 additions & 2 deletions workbench/app/Feeds/EmptyFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Workbench\App\Feeds;

use DragonCode\LaravelFeed\Data\ElementData;
use DragonCode\LaravelFeed\Feeds\Feed;
use Illuminate\Database\Eloquent\Builder;
use Workbench\App\Models\News;
Expand All @@ -17,8 +18,10 @@ public function builder(): Builder
return News::query()->where('id', '<', 0);
}

public function rootItem(): ?string
public function root(): ElementData
{
return class_basename($this);
return new ElementData(
class_basename($this)
);
}
}
7 changes: 5 additions & 2 deletions workbench/app/Feeds/FilledFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Workbench\App\Feeds;

use DragonCode\LaravelFeed\Data\ElementData;
use DragonCode\LaravelFeed\Feeds\Feed;
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
use Illuminate\Database\Eloquent\Builder;
Expand All @@ -21,9 +22,11 @@ public function builder(): Builder
return News::query()->where('updated_at', '>', now()->subDay());
}

public function rootItem(): ?string
public function root(): ElementData
{
return class_basename($this);
return new ElementData(
class_basename($this)
);
}

public function filename(): string
Expand Down