- Added some more method to filter properties

This commit is contained in:
Dave Mc Nicoll 2024-05-27 18:06:56 +00:00
parent 2a6ec3cd1b
commit 0cdc2dc3d8
5 changed files with 60 additions and 13 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -15,8 +15,4 @@ class ReflectedMethod extends Reflected implements ReflectedInterface
public array $parameters = [],
) {}
public function getAttributes(): array
{
return $this->attributes;
}
}

View File

@ -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
{

View File

@ -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