Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for object datatype. #21

Merged
merged 3 commits into from
Oct 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Map/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,19 @@ public function nested($field, Closure $callback)
return $this->addField('nested', $field, ['callback' => $callback]);
}

/**
* Add a object map.
*
* @param $field
* @param Closure $callback
*
* @return Fluent
*/
public function object($field, Closure $callback)
{
return $this->addField('object', $field, ['callback' => $callback]);
}

/**
* Add a new field to the blueprint.
*
Expand Down
23 changes: 23 additions & 0 deletions src/Map/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,29 @@ public function compileNested(Fluent $fluent)
];
}

/**
* Compile a object map.
*
* @param Fluent $fluent
*
* @return array
*/
public function compileObject(Fluent $fluent)
{
$blueprint = new Blueprint($fluent->type);

/* @var \Closure $callback */
$callback = $fluent->callback;

if (is_callable($callback)) {
$callback($blueprint);
}

return [
'properties' => $this->compileFields($blueprint->getFields()),
];
}

/**
* Format the map array for submission.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/Map/MapGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,21 @@ public function it_adds_a_nested_map()
$statement);
}

/**
* @test
*/
public function it_adds_a_object_map()
{
$blueprint = new Blueprint('post');
$blueprint->create();
$blueprint->object('tags', function ($blueprint) {
$blueprint->string('name');
});
$statement = $blueprint->toDSL($this->getGrammar());
$this->assertEquals(['tags' => ['properties' => ['name' => ['type' => 'string']]]],
$statement);
}

protected function getGrammar()
{
return new \Sleimanx2\Plastic\Map\Grammar();
Expand Down