feat: Add #[UseResource] and #[UseResourceCollection] attributes
#330
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Ports Laravel's
#[UseResource]and#[UseResourceCollection]attributes to Hypervel, enabling declarative resource binding for models and collections.Summary
#[UseResource]attribute for declaratively binding a JsonResource to a model#[UseResourceCollection]attribute for declaratively binding a ResourceCollection to a modelTransformsToResourcetrait providingtoResource()method on modelsTransformsToResourceCollectiontrait providingtoResourceCollection()method on collectionsAnonymousResourceCollectioninheritance to properly extend Hypervel'sResourceCollectionUsage
Model to Resource
Collection to Resource Collection
Resolution Order
toResource()on Model:#[UseResource]attributeApp\Http\Resources\{ModelName}Resource)toResourceCollection()on Collection:#[UseResourceCollection]attribute (returns new instance)#[UseResource]attribute (uses::collection()method)Files Changed
New Files
src/core/src/Database/Eloquent/Attributes/UseResource.phpsrc/core/src/Database/Eloquent/Attributes/UseResourceCollection.phpsrc/core/src/Database/Eloquent/Concerns/TransformsToResource.phpsrc/support/src/Traits/TransformsToResourceCollection.phpModified Files
src/core/src/Database/Eloquent/Model.php- AddedTransformsToResourcetraitsrc/core/src/Database/Eloquent/Collection.php- AddedTransformsToResourceCollectiontraitsrc/support/src/Collection.php- AddedTransformsToResourceCollectiontraitsrc/http/src/Resources/Json/AnonymousResourceCollection.php- Fixed inheritance hierarchyTest Files
tests/Core/Database/Eloquent/Concerns/TransformsToResourceTest.phptests/Core/Database/Eloquent/Models/TransformsToResourceTestModelInModelsNamespace.phptests/Support/Traits/TransformsToResourceCollectionTest.phpAnonymousResourceCollection Fix
As part of this PR,
AnonymousResourceCollectionwas updated to properly extendHypervel\Http\Resources\Json\ResourceCollectioninstead ofHyperf\Resource\Json\AnonymousResourceCollection.Before: The class extended Hyperf's version, which meant it wasn't a subtype of Hypervel's
ResourceCollection.After: The class now extends Hypervel's
ResourceCollection, ensuring proper type hierarchy and allowingtoResourceCollection()to return the correct type.Test Plan
toResource()transforms model with explicit classtoResource()uses#[UseResource]attributetoResource()throws when resource cannot be foundguessResourceName()returns correct names for\Models\namespaceguessResourceName()returns empty for non-Models namespacetoResourceCollection()transforms collection with explicit classtoResourceCollection()returns empty collection for empty inputtoResourceCollection()uses#[UseResourceCollection]attributetoResourceCollection()uses#[UseResource]with::collection()#[UseResourceCollection]takes precedence over#[UseResource]Hypervel\Support\CollectionandHypervel\Database\Eloquent\CollectionguessResourceNamemethod