Skip to content

Commit

Permalink
Check many schemas when saving in relation reducer
Browse files Browse the repository at this point in the history
  • Loading branch information
dmongeau committed Oct 29, 2018
1 parent a6e2070 commit 3707828
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/Folklore/EloquentJsonSchema/Support/RelationReducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function set(HasJsonSchemaContract $model, Node $node, $state)
// @TODO add checks everywhere required
public function save(HasJsonSchemaContract $model, Node $node, $state)
{
if (!$this->shouldUseReducer($model, $node, $state)) {
if (!$this->shouldUseSaveReducer($model, $node, $state)) {
return $state;
}

Expand Down Expand Up @@ -124,6 +124,30 @@ protected function shouldUseReducer($model, $node, $state)
return true;
}

protected function shouldUseSaveReducer($model, $node, $state)
{
if (is_null($state)) {
return false;
}

// Only treat relations matching the associated schema class
$relationSchemaClass = $this->getRelationSchemaClass($model, $node, $state);
$relationSchemaManyClass = $this->getRelationSchemaManyClass($model, $node, $state);
if ((is_null($relationSchemaClass) || !($node->schema instanceof $relationSchemaClass))
&& (is_null($relationSchemaManyClass) || !($node->schema instanceof $relationSchemaManyClass))
) {
return false;
}

// Only treat single item nodes, not arrays
$type = $node->schema->getType();
if ($type !== 'object' && $type !== 'array') {
return false;
}

return true;
}

protected function mutateRelationIdToObject($model, $relationName, $id)
{
$method = 'mutate'.studly_case($relationName).'RelationIdToObject';
Expand Down

0 comments on commit 3707828

Please sign in to comment.