Destination Transformers

Destination Transformers (also sometimes referred to as simply, Transformers) are classes used in between the step of getting your data and saving your data. If you're familiar with Laravel, a transformer is kind of like an API resource.

By default, YumYum uses a really simple transformer, where everything from the feed's response is then saved.

3namespace DoubleThreeDigital\YumYum\Feeds\Transformers;
5use DoubleThreeDigital\YumYum\Contracts\Transformer as Contract;
7class Entry implements Contract
9 protected array $item;
11 public function __construct(array $item)
12 {
13 $this->item = $item;
14 }
16 public function toArray(): array
17 {
18 return $this->item;
19 }

The $item is passed in to your constructor and then your toArray method should return an array of data (this is what get's saved).

However, most of the time you'll probably want to use your own, custom transformer. To make it easy, run php please yumyum:transformer, followed by the name of your transformer, for example:

1php please yumyum:transformer WordPress

The comamnd will generate a WordPress transformer in your App\Transformers directory with some boilerplate code, enough for you to get started!

Mapping to fields

As you know, the toArray method should return an array of data, this is what is later saved as an Entry or Term, depending on what you have configured. Most of the time, you'll want to map the data you get back from the feed to your own fields, setup in your blueprint.

Let's take a feed for example and let's map it to our custom fields.

1public function toArray(): array
3 return [
4 'title' => $this->item['title'],
5 'embed_url' => str_replace('', '', $this->item['link']),
6 'show_notes' => $this->item['description'],
7 ];

🔥 Hot Tip: You can use array_merge to pull in existing attributes as well as your custom ones.