-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
6 changed files
with
190 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper\Util; | ||
|
||
use Rekalogika\Mapper\Attribute\MapperAttributeInterface; | ||
|
||
class AttributeUtil | ||
{ | ||
/** | ||
* @var array<class-string,array<int,\ReflectionAttribute<MapperAttributeInterface>>> | ||
*/ | ||
private static array $attributesIncludingParentsCache = []; | ||
|
||
/** | ||
* @param \ReflectionClass<object> $class | ||
* @return array<int,\ReflectionAttribute<MapperAttributeInterface>> | ||
*/ | ||
public static function getAttributesIncludingParents(\ReflectionClass $class): array | ||
{ | ||
$className = $class->getName(); | ||
|
||
if (isset(self::$attributesIncludingParentsCache[$className])) { | ||
return self::$attributesIncludingParentsCache[$className]; | ||
} | ||
|
||
$attributes = []; | ||
|
||
while ($class !== false) { | ||
$attributes = array_merge( | ||
$attributes, | ||
self::getAttributes($class) | ||
); | ||
|
||
$class = $class->getParentClass(); | ||
} | ||
|
||
foreach (class_implements($className) as $interface) { | ||
$interface = new \ReflectionClass($interface); | ||
|
||
$attributes = array_merge( | ||
$attributes, | ||
self::getAttributes($interface) | ||
); | ||
} | ||
|
||
return self::$attributesIncludingParentsCache[$className] = $attributes; | ||
} | ||
|
||
/** | ||
* @var array<class-string,array<int,\ReflectionAttribute<MapperAttributeInterface>>> | ||
*/ | ||
private static array $attributes = []; | ||
|
||
/** | ||
* @param \ReflectionClass<object> $class | ||
* @return array<int,\ReflectionAttribute<MapperAttributeInterface>> | ||
*/ | ||
public static function getAttributes(\ReflectionClass $class): array | ||
{ | ||
$className = $class->getName(); | ||
|
||
if (isset(self::$attributes[$className])) { | ||
return self::$attributes[$className]; | ||
} | ||
|
||
$attributes = []; | ||
|
||
$attributes = $class->getAttributes( | ||
MapperAttributeInterface::class, | ||
\ReflectionAttribute::IS_INSTANCEOF | ||
); | ||
|
||
return self::$attributes[$className] = $attributes; | ||
} | ||
} |
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