laravel-data v2 #84
Replies: 7 comments 16 replies
-
To some degree we are getting into normalizing data when we talk about the creation method code and the unified pipeline for resolving Data objects. I am wondering if some sort of normalizer that can be tapped into might be worth adding. For example: Normalization provider Interface: interface NormalizerInterface
{
public function supports(mixed $value): bool;
public function normalize(mixed $value): array;
} Used like so: // Register some default providers.
Normalizer::register(new RequestNormalizer);
Normalizer::register(new ModelNormalizer);
// The data is normalized and then passed through the pipeline.
// If no provider supports the type, an exception is thrown.
class Resolver
{
public static function from(mixed $from)
{
return new static(
Normalizer::normalize($from)
);
}
public function __construct(array $data)
{}
} |
Beta Was this translation helpful? Give feedback.
-
I would like an option to easily convert one DataCollection or Array of DataObjects to another DataCollection. Right now the ensureAllItemsAreData function only checks if it is a data class, not which one. See: protected function ensureAllItemsAreData()
{
$closure = fn ($item) => $item instanceof Data ? $item : $this->dataClass::from($item);
$this->items = $this->isPaginated()
? $this->items->through($closure)
: array_map($closure, $this->items);
} The problem is de following: when you put a // @var DataCollection<BarData> $barDataCollectiom
FooData::collection($barDataCollection);
// or
FooData::collection($barDataCollection->all()); I would like this to be extended to check the actual data class. Maybe the function can be changed to something like: protected function ensureAllItemsAreData()
{
$closure = fn ($item) => $item instanceof Data && $this->dataClass === $item::class ? $item : $this->dataClass::from($item);
$this->items = $this->isPaginated()
? $this->items->through($closure)
: array_map($closure, $this->items);
} Maybe this could be done optionally or configurable or with a new function. I think it would be nice if this conversion would be done automatically. Right now it seems I have to use a helper function in my own Data class to make sure the Data classes are converted correctly. use Spatie\LaravelData\Data as LaravelData;
use Spatie\LaravelData\DataCollection;
abstract class Data extends LaravelData
{
/**
* @param DataCollection<Data>|array<int|mixed, Data> $collection
*
* @return DataCollection<static>
*/
public static function dataCollection(DataCollection|array $collection): DataCollection
{
return static::collection(array_map(fn (Data $item) => static::from($item), [...$collection]));
}
} |
Beta Was this translation helpful? Give feedback.
-
Is there any release plans for V2, and is it compatible with V1? |
Beta Was this translation helpful? Give feedback.
-
Thanks for this package and excited to see the V2 being in the works! I have two feature requests that I think will make the package even better. Easier custom collectionsI'm a great fan of custom collections. Currently I implement them like this: class Collection extends DataCollection
{
// Custom methods
} class RichText extends Data
{
public static function collection(Paginator|Enumerable|array|AbstractCursorPaginator|DataCollection|AbstractPaginator $items): DataCollection
{
return new Collection(static::class, $items);
}
} Is it possible to add easier support for them, without having to overwrite the "collection" method? For example like this: class RichText extends Data
{
protected static string $collection = CustomCollectionClass::class;
} Implementation would be fairly easy: class Data
{
protected static string $collection = DataCollection::class;
public static getCollection(): string
{
return static::$collection;
}
public static function collection(Paginator|Enumerable|array|AbstractCursorPaginator|DataCollection|AbstractPaginator $items): DataCollection
{
return new (static::getCollection())(static::class, $items);
}
} DataCollection extends Illuminate\Support\CollectionI'm not sure whether this is technically feasible (though I hope so), but it would be great of the DataCollection could extend the Illuminate Collection. The reason I'm asking is that there are quite some methods missing from the DataCollection class, that are present in the Illuminate Collection. For me, this makes the DataCollection class much less usable. For example: If this is not possible, would it be possible to at least implement the Thank you! |
Beta Was this translation helpful? Give feedback.
-
We have created an early implementation of an OpenAPI specification generator that relies on Laravel Routes + Data request and data response objects (using reflection). Are there others with interest in this? Would this be something that could be integrated in the main package? Or could this be a separate package? |
Beta Was this translation helpful? Give feedback.
-
While prematurely converting to v2, I noticed quite a bit of errors showing up in both Telescope and Ray, while I'd also like thank you for this excellent package. |
Beta Was this translation helpful? Give feedback.
-
We've officially released |
Beta Was this translation helpful? Give feedback.
-
So it is time to start thinking about what version 2 of spatie/laravel-data will look like. This is what's in the pipeline at the moment:
Will certainly be added:
Undefined
properties (add sometimes rule & undefined concept #79)DataCollections
in adata
key to be more like JSON API (Wrap collections in a key #43) -> we should revisit this PR, actually only thetoResponse
method of data objects and collections should wrap the thing in a data keyMapFrom
andMapTo
when creating or transforming a data object (@phh is working on this)authorized
methodDataFromSomethingResolver
line 67DataCollection::from($items)
to also cast already existing data objects into the correct data objects of the collectionDataCollection
Lazy
in subclasses likeDefaultLazy
,ConditionLazy
,RelationAwareLazy
DataClass
andDataProperty
with support for cachingSnakeToCamelCasePropertyNameMapper
. We could use these classes together with theMapFrom
andMapTo
attributes.from
method. We could just loop over allfrom*
methods, check if the object can be constructed and if not catch the exception and try the nextfrom*
method.DataCollection
intoDataCollection
andPaginatedDataCollection
, this makes working with data collections a lot more clear. Also paginated data collections can only be used on the top level so this would make a lot of code a bit more readable.errorBag
,stopOnFirstFailure
andredirect
for validation.$context
array to a cast to provide values arround the casted property (Dynamic casting based on passed data #102)only
andexcept
methods on data objectsIf you have any ideas, please let them know down bellow!
Beta Was this translation helpful? Give feedback.
All reactions