generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
118 additions
and
68 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,79 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Support; | ||
|
||
use Illuminate\Support\Collection; | ||
use ReflectionAttribute; | ||
|
||
class AttributeCollection extends Collection | ||
{ | ||
private array $groups; | ||
|
||
public static function makeFromReflectionAttributes(array $attributes): static | ||
{ | ||
return static::make( | ||
Check failure on line 14 in src/Support/AttributeCollection.php GitHub Actions / phpstan
|
||
array_map( | ||
fn (ReflectionAttribute $attribute) => $attribute->newInstance(), | ||
array_filter($attributes, fn (ReflectionAttribute $attribute) => class_exists($attribute->getName())) | ||
) | ||
); | ||
} | ||
|
||
public function add($item): static | ||
{ | ||
unset($this->groups); | ||
return parent::add($item); | ||
} | ||
|
||
public function offsetSet($key, $value): void | ||
{ | ||
unset($this->groups); | ||
parent::offsetSet($key, $value); | ||
} | ||
|
||
private function maybeProcessItemsIntoGroups(): void | ||
{ | ||
if (!isset($this->groups)) { | ||
foreach ($this->items as $item) { | ||
$implements = class_implements($item); | ||
$parents = class_parents($item); | ||
foreach (array_merge([get_class($item)], $implements, $parents) as $parent) { | ||
$this->groups[$parent][] = $item; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param class-string $attributeClass | ||
*/ | ||
public function hasAttribute(string $attributeClass): bool | ||
{ | ||
$this->maybeProcessItemsIntoGroups(); | ||
return !empty($this->groups[$attributeClass]); | ||
} | ||
|
||
/** | ||
* @template T | ||
* @param class-string<T> $attributeClass | ||
* | ||
* @return Collection<T> | ||
*/ | ||
public function getAttributes(string $attributeClass): Collection | ||
{ | ||
$this->maybeProcessItemsIntoGroups(); | ||
return collect($this->groups[$attributeClass] ?? []); | ||
} | ||
|
||
/** | ||
* @template T | ||
* @param class-string<T> $attributeClass | ||
* | ||
* @return ?T | ||
*/ | ||
public function getAttribute(string $attributeClass): ?object | ||
Check failure on line 74 in src/Support/AttributeCollection.php GitHub Actions / phpstan
|
||
{ | ||
$this->maybeProcessItemsIntoGroups(); | ||
return current($this->groups[$attributeClass] ?? []) ?: null; | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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 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