diff --git a/docs/laravel-feeds.tree b/docs/laravel-feeds.tree index 20fb58b..4c8afcb 100644 --- a/docs/laravel-feeds.tree +++ b/docs/laravel-feeds.tree @@ -26,6 +26,7 @@ + diff --git a/docs/snippets/advanced-extends-conditional-both.php b/docs/snippets/advanced-extends-conditional-both.php new file mode 100644 index 0000000..ba9c1e6 --- /dev/null +++ b/docs/snippets/advanced-extends-conditional-both.php @@ -0,0 +1,24 @@ +when( + value : $model->category, + callback: function (ProductFeedItem $item) { + $item->title .= ' (first)'; + }, + default : function (ProductFeedItem $item) { + $item->title .= ' (second)'; + }, + ); + } +} diff --git a/docs/snippets/advanced-extends-conditional-unless.php b/docs/snippets/advanced-extends-conditional-unless.php new file mode 100644 index 0000000..74dcaa3 --- /dev/null +++ b/docs/snippets/advanced-extends-conditional-unless.php @@ -0,0 +1,24 @@ +unless( + value : $model->category, + callback: function (ProductFeedItem $item) { + $item->title .= ' (second)'; + }, + default : function (ProductFeedItem $item) { + $item->title .= ' (first)'; + }, + ); + } +} diff --git a/docs/snippets/advanced-extends-conditional.php b/docs/snippets/advanced-extends-conditional.php new file mode 100644 index 0000000..5fb4f87 --- /dev/null +++ b/docs/snippets/advanced-extends-conditional.php @@ -0,0 +1,18 @@ +when($model->category, function (ProductFeedItem $item) { + $item->title .= ' (first)'; + }); + } +} diff --git a/docs/snippets/advanced-extends-macro-feed.php b/docs/snippets/advanced-extends-macro-feed.php new file mode 100644 index 0000000..d63eb17 --- /dev/null +++ b/docs/snippets/advanced-extends-macro-feed.php @@ -0,0 +1,18 @@ +filename = $name; + + return $this; + }); + } +} diff --git a/docs/snippets/advanced-extends-macro-info.php b/docs/snippets/advanced-extends-macro-info.php new file mode 100644 index 0000000..c636d12 --- /dev/null +++ b/docs/snippets/advanced-extends-macro-info.php @@ -0,0 +1,30 @@ +title); + }); + } +} + +class ProductFeedInfo extends FeedInfo +{ + public function __construct( + protected string $title, + ) {} + + public function toArray(): array + { + return [ + 'title' => $this->titleWithPrefix(), + ]; + } +} diff --git a/docs/snippets/advanced-extends-macro-item.php b/docs/snippets/advanced-extends-macro-item.php new file mode 100644 index 0000000..e6a1cd3 --- /dev/null +++ b/docs/snippets/advanced-extends-macro-item.php @@ -0,0 +1,26 @@ +model->getKey(), $this->model->title); + }); + } +} + +class ProductFeedItem extends FeedItem +{ + public function toArray(): array + { + return [ + 'title' => $this->titleWithPrefix(), + ]; + } +} diff --git a/docs/topics/extending-functionality.topic b/docs/topics/extending-functionality.topic new file mode 100644 index 0000000..6dc0413 --- /dev/null +++ b/docs/topics/extending-functionality.topic @@ -0,0 +1,74 @@ + + + + + Extend feeds using conditional clauses and macros for Feed, FeedItem, and FeedInfo. + Extend feeds using conditional clauses and macros for Feed, FeedItem, and FeedInfo. + Extend feeds using conditional clauses and macros for Feed, FeedItem, and FeedInfo. + + + + + + Conditional clauses are available for Feed, FeedInfo and + FeedItem classes. + + +

+ Sometimes, you may want certain feed clauses to apply to a feed based on another condition. + For instance, you may only want to apply a where statement if a given input value is present or another attribute has a value. + You can accomplish this using the when method: +

+ + + +

+ The when method only executes the given closure when the first argument is true. + If the first argument is false, the closure will not be executed. + So, in the example above, the closure given to the when method will only be invoked if the + category field is present on the model and evaluates to true. +

+ +

+ You may pass another closure as the third argument to the when method. + This closure will only execute if the first argument evaluates to false. + To illustrate how this feature may be used, we will use it to configure the model title suffix: +

+ + + +

+ Additionally, the inverse method unless is available. + In this case, the processing logic is the exact opposite. +

+ + +
+ + +

+ Feed, FeedItem, and FeedInfo are "macroable", + which allows you to add additional methods to them at runtime. + The macro method accepts a closure that will be executed when your macro is called. + The macro closure may access the feed's other methods via $this, + just as if it were a real method of the feed classes. +

+ +

+ For example, the following code adds a customFilename method to the Feed class: +

+ + + +

+ Additionally, here are examples for FeedInfo and FeedItem: +

+ + + +
+
diff --git a/src/Feeds/Feed.php b/src/Feeds/Feed.php index 2b1dfac..7a0e465 100644 --- a/src/Feeds/Feed.php +++ b/src/Feeds/Feed.php @@ -14,11 +14,16 @@ use Illuminate\Foundation\Application; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; +use Illuminate\Support\Traits\Conditionable; +use Illuminate\Support\Traits\Macroable; use function class_basename; abstract class Feed { + use Conditionable; + use Macroable; + protected FeedFormatEnum $format = FeedFormatEnum::Xml; protected string $storage = 'public'; diff --git a/src/Feeds/Info/FeedInfo.php b/src/Feeds/Info/FeedInfo.php index 14aa3fe..708f1a1 100644 --- a/src/Feeds/Info/FeedInfo.php +++ b/src/Feeds/Info/FeedInfo.php @@ -5,9 +5,14 @@ namespace DragonCode\LaravelFeed\Feeds\Info; use Illuminate\Contracts\Support\Arrayable; +use Illuminate\Support\Traits\Conditionable; +use Illuminate\Support\Traits\Macroable; class FeedInfo implements Arrayable { + use Conditionable; + use Macroable; + public function toArray(): array { return []; diff --git a/src/Feeds/Items/FeedItem.php b/src/Feeds/Items/FeedItem.php index d7d5900..cab2d45 100644 --- a/src/Feeds/Items/FeedItem.php +++ b/src/Feeds/Items/FeedItem.php @@ -7,11 +7,16 @@ use Illuminate\Contracts\Support\Arrayable; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Str; +use Illuminate\Support\Traits\Conditionable; +use Illuminate\Support\Traits\Macroable; use function class_basename; class FeedItem implements Arrayable { + use Conditionable; + use Macroable; + protected ?string $name = null; public function __construct(