From f2e4b8966f435ab6a9d0c75df87b95a0921a8e0f Mon Sep 17 00:00:00 2001 From: Christian Sciberras Date: Sat, 28 Oct 2023 19:52:07 +0200 Subject: [PATCH] Custom typeinfo serializer --- src/TypeInfo/TypeInfoBase.php | 9 ++++++--- src/TypeInfo/TypeInfoClass.php | 11 ++++++++--- src/TypeInfo/TypeInfoMethod.php | 11 ++++++++--- src/Util/ArrayableInterface.php | 12 ++++++++++++ 4 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 src/Util/ArrayableInterface.php diff --git a/src/TypeInfo/TypeInfoBase.php b/src/TypeInfo/TypeInfoBase.php index 795cdab..520b471 100644 --- a/src/TypeInfo/TypeInfoBase.php +++ b/src/TypeInfo/TypeInfoBase.php @@ -5,8 +5,9 @@ use ArrayAccess; use Closure; use JetBrains\PhpStorm\ArrayShape; +use uuf6429\Rune\Util\ArrayableInterface; -abstract class TypeInfoBase +abstract class TypeInfoBase implements ArrayableInterface { protected string $name; @@ -79,13 +80,15 @@ public function isInvokable(): bool } #[ArrayShape(['name' => 'string', 'hint' => 'null|string', 'link' => 'null|string'])] - public function toArray(): array + public function toArray(?callable $serializer = null): array { - return [ + $result = [ 'name' => $this->getName(), 'hint' => $this->getHint(), 'link' => $this->getLink(), 'types' => $this->getTypes(), ]; + + return $serializer ? $serializer($this, $result) : $result; } } diff --git a/src/TypeInfo/TypeInfoClass.php b/src/TypeInfo/TypeInfoClass.php index 3856a85..332264f 100644 --- a/src/TypeInfo/TypeInfoClass.php +++ b/src/TypeInfo/TypeInfoClass.php @@ -2,6 +2,8 @@ namespace uuf6429\Rune\TypeInfo; +use JetBrains\PhpStorm\ArrayShape; + class TypeInfoClass extends TypeInfoBase { /** @@ -39,13 +41,16 @@ public function getMembers(): array return $this->members; } - public function toArray(): array + #[ArrayShape(['name' => 'string', 'hint' => 'null|string', 'link' => 'null|string', 'members' => 'array'])] + public function toArray(?callable $serializer = null): array { - return array_merge(parent::toArray(), [ + $result = array_merge(parent::toArray($serializer), [ 'members' => array_map( - static fn ($member) => $member->toArray(), + static fn ($member) => $member->toArray($serializer), $this->getMembers() ), ]); + + return $serializer ? $serializer($this, $result) : $result; } } diff --git a/src/TypeInfo/TypeInfoMethod.php b/src/TypeInfo/TypeInfoMethod.php index f0f6974..883aa7b 100644 --- a/src/TypeInfo/TypeInfoMethod.php +++ b/src/TypeInfo/TypeInfoMethod.php @@ -2,6 +2,8 @@ namespace uuf6429\Rune\TypeInfo; +use JetBrains\PhpStorm\ArrayShape; + class TypeInfoMethod extends TypeInfoBase { /** @@ -42,14 +44,17 @@ public function getReturnTypes(): array return $this->return; } - public function toArray(): array + #[ArrayShape(['name' => 'string', 'hint' => 'null|string', 'link' => 'null|string', 'params' => 'array', 'return' => 'array'])] + public function toArray(?callable $serializer = null): array { - return array_merge(parent::toArray(), [ + $result = array_merge(parent::toArray($serializer), [ 'params' => array_map( - static fn (TypeInfoParameter $param) => $param->toArray(), + static fn (TypeInfoParameter $param) => $param->toArray($serializer), $this->getParameters() ), 'return' => $this->getReturnTypes(), ]); + + return $serializer ? $serializer($this, $result) : $result; } } diff --git a/src/Util/ArrayableInterface.php b/src/Util/ArrayableInterface.php new file mode 100644 index 0000000..33e5a06 --- /dev/null +++ b/src/Util/ArrayableInterface.php @@ -0,0 +1,12 @@ +): array $serializer + * @return array + */ + public function toArray(?callable $serializer = null): array; +}