Skip to content

Commit

Permalink
Merge pull request #16 from LinioIT/feature/schema-builder
Browse files Browse the repository at this point in the history
Added schema builder
  • Loading branch information
klaussilveira authored Apr 6, 2017
2 parents 13238df + 7c83e92 commit 60cc44d
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Node/BaseNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class BaseNode
*/
protected $type = 'array';

/**
* @var string
*/
protected $typeAlias = 'array';

/**
* @var bool
*/
Expand Down Expand Up @@ -88,6 +93,16 @@ public function setType(string $type)
$this->type = $type;
}

public function setTypeAlias(string $typeAlias)
{
$this->typeAlias = $typeAlias;
}

public function getTypeAlias(): string
{
return $this->typeAlias;
}

public function setRequired(bool $required)
{
$this->required = $required;
Expand Down
34 changes: 34 additions & 0 deletions src/SchemaBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);

namespace Linio\Component\Input;

use Linio\Component\Input\Node\BaseNode;

class SchemaBuilder
{
public function build(InputHandler $inputHandler): array
{
$inputHandler->define();
$schema = $this->walk($inputHandler->getRoot());

return $schema;
}

protected function walk(BaseNode $node): array
{
if (!$node->hasChildren()) {
return [];
}

foreach ($node->getChildren() as $field => $childNode) {
$schema[$field]['type'] = $childNode->getTypeAlias();
$schema[$field]['required'] = $childNode->isRequired();
$schema[$field]['default'] = $childNode->getDefault();
$schema[$field]['nullable'] = $childNode->allowNull();
$schema[$field]['children'] = $this->walk($childNode);
}

return $schema;
}
}
4 changes: 4 additions & 0 deletions src/TypeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function getType(string $name): BaseNode
{
if (isset($this->types[$name])) {
$type = new $this->types[$name]();
$type->setTypeAlias($name);
$type->setTypeHandler($this);

return $type;
Expand All @@ -62,6 +63,7 @@ public function getType(string $name): BaseNode
if ($this->isScalarCollectionType($name)) {
$type = new ScalarCollectionNode();
$type->setType($this->getCollectionType($name));
$type->setTypeAlias($name);
$type->setTypeHandler($this);

return $type;
Expand All @@ -70,6 +72,7 @@ public function getType(string $name): BaseNode
if ($this->isClassType($name)) {
$type = new ObjectNode();
$type->setType($name);
$type->setTypeAlias('object');
$type->setTypeHandler($this);
$type->setInstantiator($this->defaultInstantiator);

Expand All @@ -79,6 +82,7 @@ public function getType(string $name): BaseNode
if ($this->isCollectionType($name)) {
$type = new CollectionNode();
$type->setType($this->getCollectionType($name));
$type->setTypeAlias('object[]');
$type->setTypeHandler($this);
$type->setInstantiator($this->defaultInstantiator);

Expand Down
151 changes: 151 additions & 0 deletions tests/SchemaBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php
declare(strict_types=1);

namespace Linio\Component\Input;

class SchemaTestInputHandler extends InputHandler
{
public function define()
{
$this->add('title', 'string');
$this->add('size', 'int');
$this->add('dimensions', 'int[]');
$this->add('date', 'datetime');
$this->add('metadata', 'array');

$simple = $this->add('simple', 'array');
$simple->add('title', 'string', ['default' => 'Barfoo']);
$simple->add('size', 'int', ['required' => false, 'default' => 15]);
$simple->add('date', 'datetime');

$author = $this->add('author', 'Linio\Component\Input\TestUser');
$author->add('name', 'string');
$author->add('age', 'int');
$author->add('is_active', 'bool', ['required' => false]);

$authors = $this->add('authors', 'Linio\Component\Input\TestUser[]');
$authors->add('name', 'string');
}
}

class SchemaBuilderTest extends \PHPUnit_Framework_TestCase
{
public function testIsBuildingSchema()
{
$expectedSchema = [
'title' => [
'type' => 'string',
'required' => true,
'default' => null,
'nullable' => false,
'children' => [],
],
'size' => [
'type' => 'int',
'required' => true,
'default' => null,
'nullable' => false,
'children' => [],
],
'dimensions' => [
'type' => 'int[]',
'required' => true,
'default' => null,
'nullable' => false,
'children' => [],
],
'date' => [
'type' => 'datetime',
'required' => true,
'default' => null,
'nullable' => false,
'children' => [],
],
'metadata' => [
'type' => 'array',
'required' => true,
'default' => null,
'nullable' => false,
'children' => [],
],
'simple' => [
'type' => 'array',
'required' => true,
'default' => null,
'nullable' => false,
'children' => [
'title' => [
'type' => 'string',
'required' => false,
'default' => 'Barfoo',
'nullable' => false,
'children' => [],
],
'size' => [
'type' => 'int',
'required' => false,
'default' => 15,
'nullable' => false,
'children' => [],
],
'date' => [
'type' => 'datetime',
'required' => true,
'default' => null,
'nullable' => false,
'children' => [],
],
],
],
'author' => [
'type' => 'object',
'required' => true,
'default' => null,
'nullable' => false,
'children' => [
'name' => [
'type' => 'string',
'required' => true,
'default' => null,
'nullable' => false,
'children' => [],
],
'age' => [
'type' => 'int',
'required' => true,
'default' => null,
'nullable' => false,
'children' => [],
],
'is_active' => [
'type' => 'bool',
'required' => false,
'default' => null,
'nullable' => false,
'children' => [],
],
],
],
'authors' => [
'type' => 'object[]',
'required' => true,
'default' => null,
'nullable' => false,
'children' => [
'name' => [
'type' => 'string',
'required' => true,
'default' => null,
'nullable' => false,
'children' => [],
],
],
],
];

$schemaBuilder = new SchemaBuilder();
$schema = $schemaBuilder->build(new SchemaTestInputHandler());

$this->assertEquals($expectedSchema, $schema);
}
}

0 comments on commit 60cc44d

Please sign in to comment.