-
-
Notifications
You must be signed in to change notification settings - Fork 14
feat: Add #[CollectedBy] attribute
#331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
binaryfire
wants to merge
4
commits into
hypervel:main
Choose a base branch
from
binaryfire:feature/collected-by-attribute
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
771060a
Add CollectedBy attribute
binaryfire 7d4fadd
Merge remote-tracking branch 'origin/main' into feature/collected-by-…
binaryfire 9059ed7
Add $collectionClass property support for custom collections
binaryfire 61b4a91
Add HasCollection support to Pivot and MorphPivot
binaryfire File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Hypervel\Database\Eloquent\Attributes; | ||
|
|
||
| use Attribute; | ||
|
|
||
| /** | ||
| * Declare the collection class for a model using an attribute. | ||
| * | ||
| * When placed on a model class, the model will use the specified collection | ||
| * class when creating new collection instances via newCollection(). | ||
| * | ||
| * @example | ||
| * ```php | ||
| * #[CollectedBy(CustomCollection::class)] | ||
| * class User extends Model {} | ||
| * ``` | ||
| */ | ||
| #[Attribute(Attribute::TARGET_CLASS)] | ||
| class CollectedBy | ||
| { | ||
| /** | ||
| * Create a new attribute instance. | ||
| * | ||
| * @param class-string<\Hypervel\Database\Eloquent\Collection<array-key, \Hypervel\Database\Eloquent\Model>> $collectionClass | ||
| */ | ||
| public function __construct( | ||
| public string $collectionClass, | ||
| ) { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Hypervel\Database\Eloquent\Concerns; | ||
|
|
||
| use Hypervel\Database\Eloquent\Attributes\CollectedBy; | ||
| use Hypervel\Database\Eloquent\Collection; | ||
| use ReflectionClass; | ||
|
|
||
| /** | ||
| * Provides support for custom collection classes on models. | ||
| * | ||
| * Models can specify their collection class in two ways: | ||
| * 1. Using the #[CollectedBy] attribute (takes precedence) | ||
| * 2. Overriding the static $collectionClass property | ||
| * | ||
| * The fallback chain is: #[CollectedBy] attribute → $collectionClass property. | ||
| */ | ||
| trait HasCollection | ||
| { | ||
| /** | ||
| * The resolved collection class names by model. | ||
| * | ||
| * @var array<class-string, class-string<Collection>> | ||
| */ | ||
| protected static array $resolvedCollectionClasses = []; | ||
|
|
||
| /** | ||
| * Create a new Eloquent Collection instance. | ||
| * | ||
| * @param array<array-key, static> $models | ||
| * @return \Hypervel\Database\Eloquent\Collection<array-key, static> | ||
| */ | ||
| public function newCollection(array $models = []): Collection | ||
| { | ||
| static::$resolvedCollectionClasses[static::class] ??= ($this->resolveCollectionFromAttribute() ?? static::$collectionClass); | ||
|
|
||
| return new static::$resolvedCollectionClasses[static::class]($models); | ||
| } | ||
|
|
||
| /** | ||
| * Resolve the collection class name from the CollectedBy attribute. | ||
| * | ||
| * @return null|class-string<Collection> | ||
| */ | ||
| protected function resolveCollectionFromAttribute(): ?string | ||
| { | ||
| $reflectionClass = new ReflectionClass(static::class); | ||
|
|
||
| $attributes = $reflectionClass->getAttributes(CollectedBy::class); | ||
|
|
||
| if (! isset($attributes[0])) { | ||
| return null; | ||
| } | ||
|
|
||
| // @phpstan-ignore return.type (attribute stores generic Model type, but we know it's compatible with static) | ||
| return $attributes[0]->newInstance()->collectionClass; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.