diff --git a/src/Common/Reflected.php b/src/Common/Reflected.php index 080aa0d..d776602 100644 --- a/src/Common/Reflected.php +++ b/src/Common/Reflected.php @@ -56,4 +56,31 @@ abstract class Reflected } } + public function getAttributes(?string $attributeType = null): array + { + if ($attributeType) { + $list = []; + + foreach($this->attributes as $attribute) { + if ($attribute->object instanceof $attributeType) { + $list[] = $attribute; + } + } + + return $list; + } + + return $this->attributes; + } + + public function getAttribute(string $attributeType): ?object + { + foreach($this->getAttributes($attributeType) as $attribute) { + if ($attribute->object instanceof $attributeType) { + return $attribute; + } + } + + return null; + } } \ No newline at end of file diff --git a/src/Common/ReflectedClass.php b/src/Common/ReflectedClass.php index cde1608..05a7842 100644 --- a/src/Common/ReflectedClass.php +++ b/src/Common/ReflectedClass.php @@ -14,6 +14,11 @@ class ReflectedClass implements ReflectedInterface public array $traits = [], ) {} + public function getClassName() : string + { + return end(explode('\\', $this->name)); + } + public function getProperties(bool $deep = true) : array { return $deep ? array_replace( @@ -35,14 +40,28 @@ class ReflectedClass implements ReflectedInterface ) : $this->methods; } - public function getAttributes(bool $deep = true) : array + public function getAttributes(bool $deep = true, ?string $attributeType = null) : array { - return $deep ? array_merge( + $attributes = $deep ? array_merge( $this->parent ? $this->parent->getAttributes(true) : [], array_merge(...array_map(fn($e) => $e->getAttributes(true), $this->interfaces)), array_merge(...array_map(fn($e) => $e->getAttributes(true), $this->traits)), $this->attributes ) : $this->attributes; + + if ($attributeType) { + $list = []; + + foreach($attributes as $attribute) { + if ($attribute->object instanceof $attributeType) { + $list[] = $attribute; + } + } + + return $list; + } + + return $attributes; } # Get Attributes based on Native attributes's target @@ -146,4 +165,15 @@ class ReflectedClass implements ReflectedInterface return $list; } + + public function getAttribute(string $attributeType): ?object + { + foreach($this->getAttributes($attributeType) as $attribute) { + if ($attribute->object instanceof $attributeType) { + return $attribute; + } + } + + return null; + } } \ No newline at end of file diff --git a/src/Common/ReflectedMethod.php b/src/Common/ReflectedMethod.php index 53d3919..5ba50ba 100644 --- a/src/Common/ReflectedMethod.php +++ b/src/Common/ReflectedMethod.php @@ -15,8 +15,4 @@ class ReflectedMethod extends Reflected implements ReflectedInterface public array $parameters = [], ) {} - public function getAttributes(): array - { - return $this->attributes; - } } \ No newline at end of file diff --git a/src/Common/ReflectedProperty.php b/src/Common/ReflectedProperty.php index be5b70a..9baf564 100644 --- a/src/Common/ReflectedProperty.php +++ b/src/Common/ReflectedProperty.php @@ -14,12 +14,6 @@ class ReflectedProperty extends Reflected implements ReflectedInterface, \ArrayA public array $attributes = [], ) {} - public function getAttributes(): array - { - return $this->attributes; - } - - ## BACKWARD COMPATIBILITY ONLY To be removed really soon ! public function offsetExists(mixed $offset): bool { diff --git a/src/ObjectReflection.php b/src/ObjectReflection.php index a871886..cb53edb 100644 --- a/src/ObjectReflection.php +++ b/src/ObjectReflection.php @@ -31,7 +31,7 @@ class ObjectReflection { public static function fromClass(ReflectionClass|string $class, ? CacheInterface $cache = null) : self { - return new static($class, null, $cache); + return new static($class, $cache); } public function reflectClass() : ReflectedClass