Skip to content

Commit ab9d8fe

Browse files
Merge pull request #129 from TheDragonCode/1.x
Added `Conditionable` and `Macroable` to `Feed`, `FeedItem`, and `FeedInfo`
2 parents fbef9b7 + dc21789 commit ab9d8fe

11 files changed

+230
-0
lines changed

docs/laravel-feeds.tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<toc-element topic="location.topic" />
2727
<toc-element topic="presets.topic" />
2828
<toc-element topic="events.topic" />
29+
<toc-element topic="extending-functionality.topic" />
2930
</toc-element>
3031
<toc-element toc-title="Receipts">
3132
<toc-element topic="receipt-sitemap.topic" />
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use DragonCode\LaravelFeed\Feeds\Feed;
6+
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
7+
use Illuminate\Database\Eloquent\Model;
8+
9+
class ProductFeed extends Feed
10+
{
11+
public function item(Model $model): FeedItem
12+
{
13+
return (new ProductFeedItem($model))
14+
->when(
15+
value : $model->category,
16+
callback: function (ProductFeedItem $item) {
17+
$item->title .= ' (first)';
18+
},
19+
default : function (ProductFeedItem $item) {
20+
$item->title .= ' (second)';
21+
},
22+
);
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use DragonCode\LaravelFeed\Feeds\Feed;
6+
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
7+
use Illuminate\Database\Eloquent\Model;
8+
9+
class ProductFeed extends Feed
10+
{
11+
public function item(Model $model): FeedItem
12+
{
13+
return (new ProductFeedItem($model))
14+
->unless(
15+
value : $model->category,
16+
callback: function (ProductFeedItem $item) {
17+
$item->title .= ' (second)';
18+
},
19+
default : function (ProductFeedItem $item) {
20+
$item->title .= ' (first)';
21+
},
22+
);
23+
}
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use DragonCode\LaravelFeed\Feeds\Feed;
6+
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
7+
use Illuminate\Database\Eloquent\Model;
8+
9+
class ProductFeed extends Feed
10+
{
11+
public function item(Model $model): FeedItem
12+
{
13+
return (new ProductFeedItem($model))
14+
->when($model->category, function (ProductFeedItem $item) {
15+
$item->title .= ' (first)';
16+
});
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use DragonCode\LaravelFeed\Feeds\Feed;
6+
use Illuminate\Support\ServiceProvider;
7+
8+
class FeedServiceProvider extends ServiceProvider
9+
{
10+
public function boot(): void
11+
{
12+
Feed::macro('customFilename', function (Feed $feed, string $name) {
13+
$this->filename = $name;
14+
15+
return $this;
16+
});
17+
}
18+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use DragonCode\LaravelFeed\Feeds\Info\FeedInfo;
6+
use Illuminate\Support\ServiceProvider;
7+
8+
class FeedServiceProvider extends ServiceProvider
9+
{
10+
public function boot(): void
11+
{
12+
FeedInfo::macro('titleWithPrefix', function () {
13+
return sprintf('[%s]: %s', date('Y'), $this->title);
14+
});
15+
}
16+
}
17+
18+
class ProductFeedInfo extends FeedInfo
19+
{
20+
public function __construct(
21+
protected string $title,
22+
) {}
23+
24+
public function toArray(): array
25+
{
26+
return [
27+
'title' => $this->titleWithPrefix(),
28+
];
29+
}
30+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
6+
use Illuminate\Support\ServiceProvider;
7+
8+
class FeedServiceProvider extends ServiceProvider
9+
{
10+
public function boot(): void
11+
{
12+
FeedItem::macro('titleWithPrefix', function () {
13+
return sprintf('[%s]: %s]', $this->model->getKey(), $this->model->title);
14+
});
15+
}
16+
}
17+
18+
class ProductFeedItem extends FeedItem
19+
{
20+
public function toArray(): array
21+
{
22+
return [
23+
'title' => $this->titleWithPrefix(),
24+
];
25+
}
26+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE topic
3+
SYSTEM "https://resources.jetbrains.com/writerside/1.0/xhtml-entities.dtd">
4+
<topic
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:noNamespaceSchemaLocation="https://resources.jetbrains.com/writerside/1.0/topic.v2.xsd"
7+
title="Extending functionality" id="extending-functionality">
8+
9+
<link-summary>Extend feeds using conditional clauses and macros for Feed, FeedItem, and FeedInfo.</link-summary>
10+
<card-summary>Extend feeds using conditional clauses and macros for Feed, FeedItem, and FeedInfo.</card-summary>
11+
<web-summary>Extend feeds using conditional clauses and macros for Feed, FeedItem, and FeedInfo.</web-summary>
12+
13+
<show-structure depth="3" />
14+
15+
<chapter title="Conditional clauses" id="conditional_clauses">
16+
<note>
17+
Conditional clauses are available for <code>Feed</code>, <code>FeedInfo</code> and
18+
<code>FeedItem</code> classes.
19+
</note>
20+
21+
<p>
22+
Sometimes, you may want certain feed clauses to apply to a feed based on another condition.
23+
For instance, you may only want to apply a where statement if a given input value is present or another attribute has a value.
24+
You can accomplish this using the <code>when</code> method:
25+
</p>
26+
27+
<code-block lang="php" src="advanced-extends-conditional.php" include-lines="5-" />
28+
29+
<p>
30+
The <code>when</code> method only executes the given closure when the first argument is <code>true</code>.
31+
If the first argument is <code>false</code>, the closure will not be executed.
32+
So, in the example above, the closure given to the <code>when</code> method will only be invoked if the
33+
<code>category</code> field is present on the model and evaluates to <code>true</code>.
34+
</p>
35+
36+
<p>
37+
You may pass another closure as the third argument to the <code>when</code> method.
38+
This closure will only execute if the first argument evaluates to <code>false</code>.
39+
To illustrate how this feature may be used, we will use it to configure the model title suffix:
40+
</p>
41+
42+
<code-block lang="php" src="advanced-extends-conditional-both.php" include-lines="5-" />
43+
44+
<p>
45+
Additionally, the inverse method <code>unless</code> is available.
46+
In this case, the processing logic is the exact opposite.
47+
</p>
48+
49+
<code-block lang="php" src="advanced-extends-conditional-unless.php" include-lines="5-" />
50+
</chapter>
51+
52+
<chapter title="Macroable" id="macroable">
53+
<p>
54+
<code>Feed</code>, <code>FeedItem</code>, and <code>FeedInfo</code> are "macroable",
55+
which allows you to add additional methods to them at runtime.
56+
The <code>macro</code> method accepts a closure that will be executed when your macro is called.
57+
The macro closure may access the feed's other methods via <code>$this</code>,
58+
just as if it were a real method of the feed classes.
59+
</p>
60+
61+
<p>
62+
For example, the following code adds a <code>customFilename</code> method to the <code>Feed</code> class:
63+
</p>
64+
65+
<code-block lang="php" src="advanced-extends-macro-feed.php" include-lines="5-" />
66+
67+
<p>
68+
Additionally, here are examples for <code>FeedInfo</code> and <code>FeedItem</code>:
69+
</p>
70+
71+
<code-block lang="php" src="advanced-extends-macro-info.php" include-lines="5-" />
72+
<code-block lang="php" src="advanced-extends-macro-item.php" include-lines="5-" />
73+
</chapter>
74+
</topic>

src/Feeds/Feed.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@
1414
use Illuminate\Foundation\Application;
1515
use Illuminate\Support\Facades\Storage;
1616
use Illuminate\Support\Str;
17+
use Illuminate\Support\Traits\Conditionable;
18+
use Illuminate\Support\Traits\Macroable;
1719

1820
use function class_basename;
1921

2022
abstract class Feed
2123
{
24+
use Conditionable;
25+
use Macroable;
26+
2227
protected FeedFormatEnum $format = FeedFormatEnum::Xml;
2328

2429
protected string $storage = 'public';

src/Feeds/Info/FeedInfo.php

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

77
use Illuminate\Contracts\Support\Arrayable;
8+
use Illuminate\Support\Traits\Conditionable;
9+
use Illuminate\Support\Traits\Macroable;
810

911
class FeedInfo implements Arrayable
1012
{
13+
use Conditionable;
14+
use Macroable;
15+
1116
public function toArray(): array
1217
{
1318
return [];

0 commit comments

Comments
 (0)