- Added some more method to filter properties

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

No files matched your search

+32 -2
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;
}
}