Skip to content

Commit

Permalink
Custom typeinfo serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
uuf6429 committed Oct 28, 2023
1 parent 6c1ed84 commit f2e4b89
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/TypeInfo/TypeInfoBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
11 changes: 8 additions & 3 deletions src/TypeInfo/TypeInfoClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace uuf6429\Rune\TypeInfo;

use JetBrains\PhpStorm\ArrayShape;

class TypeInfoClass extends TypeInfoBase
{
/**
Expand Down Expand Up @@ -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;
}
}
11 changes: 8 additions & 3 deletions src/TypeInfo/TypeInfoMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace uuf6429\Rune\TypeInfo;

use JetBrains\PhpStorm\ArrayShape;

class TypeInfoMethod extends TypeInfoBase
{
/**
Expand Down Expand Up @@ -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;
}
}
12 changes: 12 additions & 0 deletions src/Util/ArrayableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types=1);

namespace uuf6429\Rune\Util;

interface ArrayableInterface
{
/**
* @param null|callable(self, array<string, mixed>): array $serializer
* @return array<string, mixed>
*/
public function toArray(?callable $serializer = null): array;
}

0 comments on commit f2e4b89

Please sign in to comment.