Skip to content

Commit 65efc13

Browse files
Merge pull request #15 from TheDragonCode/1.x
Added `rootAttributes` method
2 parents 2704a8a + cf08b04 commit 65efc13

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ According to this example, the XML file with the following contents will be gene
169169

170170
### Objects, attributes and more
171171

172-
#### Setting the name of the root element
172+
#### Setting the root element
173173

174174
```php
175175
class UserFeed extends Feed
@@ -178,6 +178,13 @@ class UserFeed extends Feed
178178
{
179179
return 'users';
180180
}
181+
182+
public function rootAttributes(): array
183+
{
184+
return [
185+
'foo' => 'some value',
186+
];
187+
}
181188
}
182189
```
183190

src/Feeds/Feed.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public function rootItem(): ?string
4646
return null;
4747
}
4848

49+
public function rootAttributes(): array
50+
{
51+
return [];
52+
}
53+
4954
public function filename(): string
5055
{
5156
return $this->filename ??= Str::kebab(class_basename($this)) . '.xml';

src/Feeds/Sitemaps/PostFeed.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\LaravelFeed\Feeds\Sitemaps;
6+
7+
use App\Models\Post;
8+
use DragonCode\LaravelFeed\Feeds\Feed;
9+
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
10+
use Illuminate\Database\Eloquent\Builder;
11+
use Illuminate\Database\Eloquent\Model;
12+
13+
class PostFeed extends Feed
14+
{
15+
public function builder(): Builder
16+
{
17+
return Post::query()
18+
->withCount('comments')
19+
->with('author')
20+
->where('created_at', '>', now()->subYear());
21+
}
22+
23+
public function rootItem(): ?string
24+
{
25+
return 'urlset';
26+
}
27+
28+
public function filename(): string
29+
{
30+
return 'sitemaps/posts.xml';
31+
}
32+
33+
public function item(Model $model): FeedItem
34+
{
35+
return new Items\Sitemaps\PostFeedItem($model);
36+
}
37+
}

src/Services/Generator.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
use Illuminate\Database\Eloquent\Collection;
99
use Illuminate\Filesystem\Filesystem;
1010

11+
use function collect;
1112
use function dirname;
1213
use function fclose;
1314
use function fopen;
1415
use function fwrite;
1516
use function implode;
17+
use function sprintf;
1618

1719
class Generator
1820
{
@@ -55,7 +57,9 @@ protected function performHeader($file, Feed $feed): void
5557
$value = $feed->header();
5658

5759
if ($item = $feed->rootItem()) {
58-
$value .= "\n<$item>\n";
60+
$value .= $feed->rootAttributes()
61+
? sprintf("\n<%s %s>\n", $item, $this->makeRootAttributes($feed))
62+
: sprintf("\n<%s>\n", $item);
5963
}
6064

6165
$this->append($file, $value);
@@ -72,6 +76,13 @@ protected function performFooter($file, Feed $feed): void
7276
$this->append($file, $value);
7377
}
7478

79+
protected function makeRootAttributes(Feed $feed): string
80+
{
81+
return collect($feed->rootAttributes())
82+
->map(fn (mixed $value, int|string $key) => sprintf('%s="%s"', $key, $value))
83+
->implode(' ');
84+
}
85+
7586
protected function append($file, string $content): void
7687
{
7788
if (! empty($content)) {

0 commit comments

Comments
 (0)