From fa4fe3227711f31e34fc9381ac8f6cb19812eef6 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Mon, 19 May 2025 17:31:33 +0000 Subject: [PATCH] - Small fix on reflection property name --- src/AttributeReader.php | 3 +- src/Common/Reflected.php | 1 - src/Common/Reflected.php~ | 82 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 src/Common/Reflected.php~ diff --git a/src/AttributeReader.php b/src/AttributeReader.php index acb0396..d9aa6bd 100644 --- a/src/AttributeReader.php +++ b/src/AttributeReader.php @@ -33,8 +33,9 @@ class AttributeReader { switch(true) { case $reflect instanceof ReflectionMethod : - case $reflect instanceof ReflectionProperty : return $reflect->class . "::" . $reflect->name; + case $reflect instanceof ReflectionProperty : + return $reflect->class . "::$" . $reflect->name; case $reflect instanceof ReflectionClass : return $reflect->name; diff --git a/src/Common/Reflected.php b/src/Common/Reflected.php index a55893a..333a202 100644 --- a/src/Common/Reflected.php +++ b/src/Common/Reflected.php @@ -21,7 +21,6 @@ abstract class Reflected $type = strtolower($type); foreach($this->getTypes() as $item) { - if ($type === "null") { if ($item->type === "null" || $item->nullable) { return true; diff --git a/src/Common/Reflected.php~ b/src/Common/Reflected.php~ new file mode 100644 index 0000000..9002648 --- /dev/null +++ b/src/Common/Reflected.php~ @@ -0,0 +1,82 @@ +attributes, fn($e) => $e->object instanceof Ignore); + } + + public function allowsNull() : bool + { + # dump($this); + return empty($this->type) || $this->expectType("null"); + } + + public function expectType(string $type) : bool + { + $type = strtolower($type); + + foreach($this->getTypes() as $item) { + if ($type === "null") { + if ($item->type === "null" || $item->nullable) { + return true; + } + } + elseif ($type === $item->type) { + return true; + } + } + + return false; + } + + public function getTypes() : array + { + return is_array($this->type) ? $this->type : [ $this->type ]; + } + + public function typeFromReflection(\ReflectionProperty|\ReflectionParameter $property) : void + { + if ( $property->hasType() ) { + $type = $property->getType(); + + if ($type instanceof \ReflectionUnionType ) { + foreach($type->getTypes() as $item) { + $this->type[] = new ReflectedPropertyType($item->getName(), $item->isBuiltIn(), $item->allowsNull()); + } + } + else { + $this->type = new ReflectedPropertyType($type->getName(), $type->isBuiltIn(), $type->allowsNull()); + } + } + } + + public function getAttributes(null|string|array $attributeType = null): array + { + if ($attributeType) { + $list = []; + + foreach($this->attributes as $attribute) { + foreach((array) $attributeType as $type) { + if ($attribute->object instanceof $type) { + $list[] = $attribute; + } + } + } + + return array_reverse($list); + } + + return array_reverse($this->attributes); + } + + public function getAttribute(string|array $attributeType): ?object + { + return $this->getAttributes($attributeType)[0] ?? null; + } +} \ No newline at end of file