Skip to content
This repository has been archived by the owner on Sep 17, 2023. It is now read-only.

Commit

Permalink
Improve json serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
knesmeyanov committed Aug 9, 2018
1 parent b92a261 commit 5e06d12
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
47 changes: 43 additions & 4 deletions src/Common/Jsonable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Railt\Reflection\Common;

use Railt\Io\Readable;
use Railt\Reflection\Contracts\Definition;
use Railt\Reflection\Contracts\Document;

/**
Expand All @@ -25,17 +26,55 @@ public function jsonSerialize(): array
$result = [];

foreach ($this->getObjectFields() as $field => $value) {
if ($value instanceof Readable || $value instanceof Document) {
continue;
foreach ($this->jsonNonRenderableTypes() as $type) {
if ($value instanceof $type) {
continue 2;
}
}

if ($field === 'parent' || $field === 'dictionary') {
continue;
foreach ($this->jsonNonRenderableFields() as $name) {
if ($field === $name) {
continue 2;
}
}

$result[$field] = $value;
}

// Additional information about type
if ($this instanceof Definition) {
$result['__typename'] = $this::getType()->getName();
}

return $result;
}

/**
* @return iterable|string[]
*/
private function jsonNonRenderableTypes(): iterable
{
yield Readable::class;
yield Document::class;
}

/**
* @return iterable|string[]
*/
private function jsonNonRenderableFields(): iterable
{
// Recursive relation exclusion
yield 'parent';

// Dictionary relation exclusion
yield 'dictionary';

// File information exclusion
yield 'offset';
yield 'line';
yield 'column';

// Reverse superfluous inheritance information
yield 'extendedBy';
}
}
1 change: 0 additions & 1 deletion tests/InstanceInheritance/InstanceInheritanceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public function testInheritanceValidation(Def $original, Def $extends, bool $val
* @param Def $extends
* @param bool $valid
* @throws \PHPUnit\Framework\SkippedTestError
* @throws TypeConflictException
* @throws \PHPUnit\Framework\AssertionFailedError
*/
public function testInheritanceInstanceOf(Def $original, Def $extends, bool $valid): void
Expand Down

0 comments on commit 5e06d12

Please sign in to comment.