← Previous · Back to README · Next →
Directives
Formats: XML
The use of directives is available only when generating feeds in the XML format.
@attributes
Indicates an array of attributes.
You can use a key named @attributes to add attributes to a node:
namespace App\Feeds;
use App\Feeds\Info\AttributesDirectiveFeedInfo;
use App\Feeds\Items\AttributesDirectiveFeedItem;
use App\Models\User;
use DragonCode\LaravelFeed\Feeds\Feed;
use DragonCode\LaravelFeed\Feeds\Info\FeedInfo;
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class AttributesDirectiveFeed extends Feed
{
public function builder(): Builder
{
return User::query();
}
public function info(): FeedInfo
{
return new AttributesDirectiveFeedInfo;
}
public function item(Model $model): FeedItem
{
return new AttributesDirectiveFeedItem($model);
}
}
namespace App\Feeds\Info;
use DragonCode\LaravelFeed\Feeds\Info\FeedInfo;
use function fake;
class AttributesDirectiveFeedInfo extends FeedInfo
{
public function toArray(): array
{
return [
'company' => [
'@attributes' => ['since' => fake()->year],
],
'url' => config('app.url'),
];
}
}
namespace App\Feeds\Items;
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
class AttributesDirectiveFeedItem extends FeedItem
{
public function toArray(): array
{
return [
'name' => $this->model->name,
'contact' => [
'@attributes' => [
'email' => $this->model->email,
'phone' => '555-000-' . $this->model->id,
],
],
];
}
}
This code will result in:
<?xml version="1.0" encoding="UTF-8"?>
<attributes_directive>
<company since="1994"/>
<url>https://example.com</url>
<user>
<name>User 1</name>
</user>
<user>
<name>User 2</name>
</user>
</attributes_directive>
@value
Indicates the applicable value “as it is”.
Note that the value of the @value field must be a string.
You can use a key named @value to add value to a node:
namespace App\Feeds\Items;
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
class ValueDirectiveFeedItem extends FeedItem
{
public function toArray(): array
{
return [
'name' => [
'@value' => $this->model->name,
],
'contact' => [
'@attributes' => ['type' => 'email'],
'@value' => $this->model->email,
],
];
}
}
This code will result in:
<?xml version="1.0" encoding="UTF-8"?>
<value_directive>
<user>
<name>User 1</name>
</user>
<user>
<name>User 2</name>
</user>
</value_directive>
@cdata
Allows the use of XML markup without transformation.
It is also possible to wrap the value of a node into a CDATA section. This allows you to use reserved characters.
You can use a key named @cdata to add CDATA to a node:
namespace App\Feeds\Items;
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
use function sprintf;
class CdataDirectiveFeedItem extends FeedItem
{
public function toArray(): array
{
return [
'name' => [
'@cdata' => sprintf('<h1>%s</h1>', $this->model->name),
],
'email' => $this->model->email,
];
}
}
This code will result in:
<?xml version="1.0" encoding="UTF-8"?>
<cdata_directive>
<user>
<name><![CDATA[<h1>User 1</h1>]]></name>
</user>
<user>
<name><![CDATA[<h1>User 2</h1>]]></name>
</user>
</cdata_directive>
@mixed
Allows you to use XML markup directly within the structure of the document.
To insert XML fragments “as is”, use the @mixed directive:
namespace App\Feeds\Items;
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
class MixedDirectiveFeedItem extends FeedItem
{
public function toArray(): array
{
return [
'name' => $this->model->name,
'@mixed' => <<<XML
<some>
<first>Foo</first>
<second>{$this->model->email}</second>
</some>
XML,
];
}
}
This code will result in:
<?xml version="1.0" encoding="UTF-8"?>
<mixed_directive>
<user>
<name>User 1</name>
<some>
<first>Foo</first>
</some>
</user>
<user>
<name>User 2</name>
<some>
<first>Foo</first>
</some>
</user>
</mixed_directive>
@custom
Describes the possibility of listing an array of elements with the same keys.
In some cases, you need to place an array of elements with the same names.
To do this, add a @ symbol to the start of the key name:
namespace App\Feeds\Items;
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
use function fake;
class ArrayDirectiveFeedItem extends FeedItem
{
public function toArray(): array
{
return [
'name' => $this->model->name,
'@avatar' => [
fake()->imageUrl(),
fake()->imageUrl(),
],
'@images' => [
[
'@attributes' => ['name' => fake()->words(asText: true)],
'@value' => fake()->imageUrl(),
],
[
'@attributes' => ['name' => fake()->words(asText: true)],
'@value' => fake()->imageUrl(),
],
],
];
}
}
This code will result in:
<?xml version="1.0" encoding="UTF-8"?>
<array_directive>
<user>
<name>User 1</name>
<avatar>https://via.placeholder.com/640x480.png/00ffff?text=aut</avatar>
<avatar>https://via.placeholder.com/640x480.png/0022dd?text=et</avatar>
<images name="doloremque distinctio facere">https://via.placeholder.com/640x480.png/0099dd?text=ea</images>
<images name="fuga odio expedita">https://via.placeholder.com/640x480.png/00cc77?text=sint</images>
</user>
<user>
<name>User 2</name>
<avatar>https://via.placeholder.com/640x480.png/00ff99?text=est</avatar>
<avatar>https://via.placeholder.com/640x480.png/003300?text=non</avatar>
<images name="distinctio et aliquam">https://via.placeholder.com/640x480.png/0099aa?text=accusantium</images>
<images name="consequatur soluta nemo">https://via.placeholder.com/640x480.png/0099cc?text=ab</images>
</user>
</array_directive>