[2.0.1] Unexpected behavior in transformer #165
-
Imagine the following structure class SongData extends Data
{
public function __construct(
public string|Optional $title,
public string|Optional $some,
) {
}
} class AlbumData extends Data
{
public function __construct(
#[DataCollectionOf(SongData::class)]
public DataCollection $songs,
) {
}
} In class AlbumData extends Data
{
public function __construct(
#[DataCollectionOf(SongData::class)]
#[WithTransformer(DataCollectionTransformer::class, attributes: 'title')]
public DataCollection $songs,
) {
}
} And here is the transformer: class DataCollectionTransformer implements Transformer
{
public function __construct(protected array|string $attributes)
{
$this->attributes = Arr::wrap($this->attributes);
}
/**
* @param DataCollection $value
*/
public function transform(DataProperty $property, mixed $value): array
{
return $value
->map(fn(Data $data) => $data->only(...$this->attributes))
->toArray();
// ❌
}
} It does not work. But if the public function transform(DataProperty $property, mixed $value): array
{
return $value
->toCollection() // <-- just added this line
->map(fn(Data $data) => $data->only(...$this->attributes))
->toArray();
// ✅
} It would seem that this is the same thing, but no! Why is that? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Partials trees which power the only directive only get passed on top -> down. So the collection has no partial trees, and this gets passed on to the individual data objects. In the second example, no Collectable transformer is used an for each data object a partials tree is constructed which will include the only directive. This approach we once started when the package was first built, since you probably want to decide includes, excludes, only's and except's at the point where you're sending out the data. Maybe that approach should change although I'm not sure how performant it will be. As for your example, this seems a far better solution to me: public function transform(DataProperty $property, mixed $value): array
{
return $value
->only(...$this->attributes)
->toArray();
} |
Beta Was this translation helpful? Give feedback.
Partials trees which power the only directive only get passed on top -> down.
So the collection has no partial trees, and this gets passed on to the individual data objects.
In the second example, no Collectable transformer is used an for each data object a partials tree is constructed which will include the only directive.
This approach we once started when the package was first built, since you probably want to decide includes, excludes, only's and except's at the point where you're sending out the data. Maybe that approach should change although I'm not sure how performant it will be.
As for your example, this seems a far better solution to me: