Skip to content

Commit c97b8f1

Browse files
Merge pull request #18 from TheDragonCode/1.x
Replace `rootItem` and `rootAttributes` methods with `root` method
2 parents 4360247 + 8857c6c commit c97b8f1

File tree

10 files changed

+74
-37
lines changed

10 files changed

+74
-37
lines changed

README.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ Each feed can be created in a certain folder of a certain storage.
7474
To indicate the storage, reduce the property of `$storage` in the feed class:
7575

7676
```php
77+
use DragonCode\LaravelFeed\Feeds\Feed;
78+
7779
class UserFeed extends Feed
7880
{
7981
protected string $storage = 'public';
@@ -85,6 +87,8 @@ By default, `public`.
8587
The path to the file inside the storage is indicated in the `filiname` method:
8688

8789
```php
90+
use DragonCode\LaravelFeed\Feeds\Feed;
91+
8892
class UserFeed extends Feed
8993
{
9094
public function filename(): string
@@ -176,25 +180,26 @@ According to this example, the XML file with the following contents will be gene
176180
#### Setting the root element
177181

178182
```php
183+
use DragonCode\LaravelFeed\Data\ElementData;
184+
use DragonCode\LaravelFeed\Feeds\Feed;
185+
179186
class UserFeed extends Feed
180187
{
181-
public function rootItem(): ?string
182-
{
183-
return 'users';
184-
}
185-
186-
public function rootAttributes(): array
188+
public function root(): ElementData
187189
{
188-
return [
189-
'foo' => 'some value',
190-
];
190+
return new ElementData(
191+
name: 'users',
192+
attributes: ['foo' => 'some value']
193+
);
191194
}
192195
}
193196
```
194197

195198
#### Adding attributes for the main section
196199

197200
```php
201+
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
202+
198203
class UserFeedItem extends FeedItem
199204
{
200205
public function attributes(): array
@@ -223,6 +228,8 @@ class UserFeedItem extends FeedItem
223228
> - `@mixed`
224229
225230
```php
231+
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
232+
226233
class UserFeedItem extends FeedItem
227234
{
228235
public function toArray(): array
@@ -264,6 +271,8 @@ class UserFeedItem extends FeedItem
264271
If it is necessary to change the file cap, override the `header` method in the feed class:
265272

266273
```php
274+
use DragonCode\LaravelFeed\Feeds\Feed;
275+
267276
class UserFeed extends Feed
268277
{
269278
public function header(): string

src/Concerns/InteractsWithName.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,22 @@
55
namespace DragonCode\LaravelFeed\Concerns;
66

77
use Illuminate\Support\Str;
8+
use Illuminate\Support\Stringable;
89

910
use function class_basename;
1011
use function str_replace;
1112

1213
/** @mixin \Illuminate\Console\GeneratorCommand */
1314
trait InteractsWithName
1415
{
16+
protected function getNameInput(): string
17+
{
18+
return Str::of(parent::getNameInput())
19+
->whenEndsWith('Feed', fn (Stringable $str) => $str->substr(0, -4))
20+
->whenEndsWith('FeedItem', fn (Stringable $str) => $str->substr(0, -8))
21+
->toString();
22+
}
23+
1524
protected function qualifyClass($name): string
1625
{
1726
return Str::finish(parent::qualifyClass($name), $this->type);

src/Data/ElementData.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\LaravelFeed\Data;
6+
7+
readonly class ElementData
8+
{
9+
public function __construct(
10+
public ?string $name = null,
11+
public array $attributes = []
12+
) {}
13+
}

src/Feeds/Feed.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace DragonCode\LaravelFeed\Feeds;
66

7+
use DragonCode\LaravelFeed\Data\ElementData;
78
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
89
use Illuminate\Contracts\Filesystem\Filesystem;
910
use Illuminate\Database\Eloquent\Builder;
@@ -41,14 +42,9 @@ public function footer(): string
4142
return '';
4243
}
4344

44-
public function rootItem(): ?string
45+
public function root(): ElementData
4546
{
46-
return null;
47-
}
48-
49-
public function rootAttributes(): array
50-
{
51-
return [];
47+
return new ElementData;
5248
}
5349

5450
public function filename(): string

src/Services/Generator.php

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

55
namespace DragonCode\LaravelFeed\Services;
66

7+
use DragonCode\LaravelFeed\Data\ElementData;
78
use DragonCode\LaravelFeed\Feeds\Feed;
89
use Illuminate\Database\Eloquent\Collection;
910
use Illuminate\Filesystem\Filesystem;
@@ -56,10 +57,10 @@ protected function performHeader($file, Feed $feed): void
5657
{
5758
$value = $feed->header();
5859

59-
if ($item = $feed->rootItem()) {
60-
$value .= $feed->rootAttributes()
61-
? sprintf("\n<%s %s>\n", $item, $this->makeRootAttributes($feed))
62-
: sprintf("\n<%s>\n", $item);
60+
if ($name = $feed->root()->name) {
61+
$value .= ! empty($feed->root()->attributes)
62+
? sprintf("\n<%s %s>\n", $name, $this->makeRootAttributes($feed->root()))
63+
: sprintf("\n<%s>\n", $name);
6364
}
6465

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

72-
if ($item = $feed->rootItem()) {
73-
$value .= "\n</$item>\n";
73+
if ($name = $feed->root()->name) {
74+
$value .= "\n</$name>\n";
7475
}
7576

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

79-
protected function makeRootAttributes(Feed $feed): string
80+
protected function makeRootAttributes(ElementData $item): string
8081
{
81-
return collect($feed->rootAttributes())
82+
return collect($item->attributes)
8283
->map(fn (mixed $value, int|string $key) => sprintf('%s="%s"', $key, $value))
8384
->implode(' ');
8485
}

stubs/feed.stub

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ declare(strict_types=1);
44

55
namespace DummyNamespace;
66

7+
use DragonCode\LaravelFeed\Data\ElementData;
78
use DragonCode\LaravelFeed\Feeds\Feed;
89
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
910
use Illuminate\Database\Eloquent\Builder;
@@ -17,9 +18,9 @@ class DummyClass extends Feed
1718
return DummyUser::query();
1819
}
1920

20-
public function rootItem(): ?string
21+
public function root(): ElementData
2122
{
22-
return 'users';
23+
return new ElementData('users');
2324
}
2425

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

3031
public function item(Model $model): FeedItem
3132
{
32-
return new Items\DummyUserFeedItem($model);
33+
return new Items\DummyClassItem($model);
3334
}
3435
}

tests/.pest/snapshots/Unit/Console/MakeTest/make_feed.snap

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ declare(strict_types=1);
44

55
namespace App\Feeds;
66

7+
use DragonCode\LaravelFeed\Data\ElementData;
78
use DragonCode\LaravelFeed\Feeds\Feed;
89
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
910
use Illuminate\Database\Eloquent\Builder;
@@ -17,9 +18,9 @@ class FooBarFeed extends Feed
1718
return User::query();
1819
}
1920

20-
public function rootItem(): ?string
21+
public function root(): ElementData
2122
{
22-
return 'users';
23+
return new ElementData('users');
2324
}
2425

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

3031
public function item(Model $model): FeedItem
3132
{
32-
return new Items\UserFeedItem($model);
33+
return new Items\FooBarFeedItem($model);
3334
}
3435
}

tests/.pest/snapshots/Unit/Console/MakeTest/make_with_item.snap

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ declare(strict_types=1);
44

55
namespace App\Feeds;
66

7+
use DragonCode\LaravelFeed\Data\ElementData;
78
use DragonCode\LaravelFeed\Feeds\Feed;
89
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
910
use Illuminate\Database\Eloquent\Builder;
@@ -17,9 +18,9 @@ class QweRtyFeed extends Feed
1718
return User::query();
1819
}
1920

20-
public function rootItem(): ?string
21+
public function root(): ElementData
2122
{
22-
return 'users';
23+
return new ElementData('users');
2324
}
2425

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

3031
public function item(Model $model): FeedItem
3132
{
32-
return new Items\UserFeedItem($model);
33+
return new Items\QweRtyFeedItem($model);
3334
}
3435
}

workbench/app/Feeds/EmptyFeed.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Workbench\App\Feeds;
66

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

20-
public function rootItem(): ?string
21+
public function root(): ElementData
2122
{
22-
return class_basename($this);
23+
return new ElementData(
24+
class_basename($this)
25+
);
2326
}
2427
}

workbench/app/Feeds/FilledFeed.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Workbench\App\Feeds;
66

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

24-
public function rootItem(): ?string
25+
public function root(): ElementData
2526
{
26-
return class_basename($this);
27+
return new ElementData(
28+
class_basename($this)
29+
);
2730
}
2831

2932
public function filename(): string

0 commit comments

Comments
 (0)