Skip to content

Commit

Permalink
Merge pull request #41 from spira/feature/fill-models
Browse files Browse the repository at this point in the history
fillModel fillModels
  • Loading branch information
zakhenry committed Feb 4, 2016
2 parents 5ab86aa + caa7389 commit 08b0e97
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 29 deletions.
21 changes: 13 additions & 8 deletions Controllers/AbstractRelatedEntityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ public function __construct(BaseModel $parentModel, TransformerInterface $transf
/**
* Function called before child models are synced with parent.
*
* @param $model
* @param $parent
* @param BaseModel $parent
* @param Collection $children
* @internal param $model
*/
protected function preSync(BaseModel $parent, Collection $children)
{
Expand All @@ -69,8 +70,9 @@ protected function preSync(BaseModel $parent, Collection $children)
/**
* Function called before response is sent after the model has been updated via sync.
*
* @param $model
* @param $parent
* @param BaseModel $parent
* @param Collection $children
* @internal param $model
*/
protected function postSync(BaseModel $parent, Collection $children)
{
Expand All @@ -86,17 +88,19 @@ protected function postSync(BaseModel $parent, Collection $children)
protected function getValidationRules($entityId = null, array $requestEntity = [])
{
$childRules = $this->getChildModel()->getValidationRules($entityId, $requestEntity);
$pivotRules = $this->getPivotValidationRules();
$pivotRules = $this->getPivotValidationRules($entityId, $requestEntity);

return array_merge($childRules, $pivotRules);
}

/**
* Override this method to provide custom validation rules.
*
* @param null $entityId
* @param array $requestEntity
* @return array
*/
protected function getPivotValidationRules()
protected function getPivotValidationRules($entityId = null, array $requestEntity = [])
{
return [];
}
Expand Down Expand Up @@ -172,8 +176,9 @@ protected function findOrFailChildEntity($id, BaseModel $parent)
}

/**
* @param $id
* @param BaseModel $parent
* @return BaseModel
* @throws ModelNotFoundException
*/
protected function fetchChildFromRelation($id, BaseModel $parent)
{
Expand Down Expand Up @@ -205,7 +210,7 @@ protected function findChildrenCollection($requestCollection, BaseModel $parent)
/**
* @param $requestCollection
* @param BaseModel $parent
* @return \Illuminate\Database\Eloquent\Collection
* @return Collection
*/
protected function findOrFailChildrenCollection($requestCollection, BaseModel $parent)
{
Expand Down
25 changes: 25 additions & 0 deletions Controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,29 @@ public function getKeyFromRequestEntity(BaseModel $entityModel, array $requestEn

return $requestEntity[$entityModel->getPrimaryKey()];
}

/**
* Override for custom functionality.
*
* @param BaseModel $model
* @param $requestEntity
* @return BaseModel
*/
protected function fillModel(BaseModel $model, $requestEntity)
{
return $model->fill($requestEntity);
}

/**
* Override for custom functionality.
*
* @param $baseModel $model
* @param Collection|BaseModel[] $existingModels
* @param Collection|array $requestCollection
* @return Collection|array
*/
protected function fillModels(BaseModel $baseModel, $existingModels, $requestCollection)
{
return $baseModel->hydrateRequestCollection($requestCollection, $existingModels);
}
}
19 changes: 9 additions & 10 deletions Controllers/ChildEntityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function postOne(Request $request, $id)
$requestEntity = $request->json()->all();
$this->validateRequest($requestEntity, $this->getValidationRules($id, $requestEntity));

$childModel->fill($request->json()->all());
$this->fillModel($childModel, $request->json()->all());

$this->checkPermission(static::class.'@postOne', ['model' => $parent, 'children' => $childModel]);

Expand All @@ -108,10 +108,9 @@ public function postMany(Request $request, $id)
$requestCollection = $request->json()->all();
$this->validateRequestCollection($requestCollection, $this->getChildModel());

$existingChildModels = $this->findChildrenCollection($requestCollection, $parent);
$existingModels = $this->findChildrenCollection($requestCollection, $parent);

$childModels = $this->getChildModel()
->hydrateRequestCollection($requestCollection, $existingChildModels);
$childModels = $this->fillModels($this->getChildModel(), $existingModels, $requestCollection);

$this->checkPermission(static::class.'@postMany', ['model' => $parent, 'children' => $childModels]);

Expand Down Expand Up @@ -151,7 +150,7 @@ public function putOne(Request $request, $id, $childId = false)
$requestEntity = $request->json()->all();
$this->validateRequest($requestEntity, $this->getValidationRules($childId, $requestEntity));

$childModel->fill($request->json()->all());
$this->fillModel($childModel, $request->json()->all());

$this->checkPermission(static::class.'@putOne', ['model' => $parent, 'children' => $childModel]);

Expand All @@ -178,9 +177,9 @@ public function putMany(Request $request, $id)
$requestCollection = $request->json()->all();
$this->validateRequestCollection($requestCollection, $this->getChildModel());

$existingChildModels = $this->findChildrenCollection($requestCollection, $parent);
$existingModels = $this->findChildrenCollection($requestCollection, $parent);

$childModels = $this->getChildModel()->hydrateRequestCollection($requestCollection, $existingChildModels);
$childModels = $this->fillModels($this->getChildModel(), $existingModels, $requestCollection);

$this->checkPermission(static::class.'@putMany', ['model' => $parent, 'children' => $childModels]);

Expand Down Expand Up @@ -227,7 +226,7 @@ public function patchOne(Request $request, $id, $childId = false)
$requestEntity = $request->json()->all();
$this->validateRequest($requestEntity, $this->getValidationRules($id, $requestEntity), $childModel);

$childModel->fill($request->json()->all());
$this->fillModel($childModel, $request->json()->all());

$this->checkPermission(static::class.'@patchOne', ['model' => $parent, 'children' => $childModel]);

Expand All @@ -250,9 +249,9 @@ public function patchMany(Request $request, $id)
$this->validateRequestCollection($requestCollection, $this->getChildModel(), true);

$parent = $this->findParentEntity($id);
$existingChildModels = $this->findOrFailChildrenCollection($requestCollection, $parent);
$existingModels = $this->findOrFailChildrenCollection($requestCollection, $parent);

$childModels = $this->getChildModel()->hydrateRequestCollection($requestCollection, $existingChildModels);
$childModels = $this->fillModels($this->getChildModel(), $existingModels, $requestCollection);

$this->checkPermission(static::class.'@patchMany', ['model' => $parent, 'children' => $childModels]);

Expand Down
15 changes: 7 additions & 8 deletions Controllers/EntityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function postOne(Request $request)
$requestEntity,
$this->getValidationRules($this->getKeyFromRequestEntity($this->getModel(), $requestEntity), $requestEntity)
);
$model->fill($requestEntity);
$this->fillModel($model, $requestEntity);
$this->checkPermission(static::class.'@postOne', ['model' => $model]);
$model->save();

Expand All @@ -163,7 +163,7 @@ public function putOne(Request $request, $id)
$requestEntity = $request->json()->all();
$this->validateRequest($requestEntity, $this->getValidationRules($id, $requestEntity));

$model->fill($request->json()->all());
$this->fillModel($model, $request->json()->all());
$this->checkPermission(static::class.'@putOne', ['model' => $model]);
$model->save();

Expand All @@ -186,8 +186,7 @@ public function putMany(Request $request)
$this->validateRequestCollection($requestCollection, $this->getModel());
$existingModels = $this->findCollection($requestCollection);

$modelCollection = $this->getModel()
->hydrateRequestCollection($requestCollection, $existingModels);
$modelCollection = $this->fillModels($this->getModel(), $existingModels, $requestCollection);

$this->checkPermission(static::class.'@putMany', ['model' => $modelCollection]);

Expand Down Expand Up @@ -217,7 +216,7 @@ public function patchOne(Request $request, $id)
$requestEntity = $request->json()->all();
$this->validateRequest($requestEntity, $this->getValidationRules($id, $requestEntity), $model);

$model->fill($request->json()->all());
$this->fillModel($model, $request->json()->all());
$this->checkPermission(static::class.'@patchOne', ['model' => $model]);
$model->save();

Expand All @@ -238,8 +237,7 @@ public function patchMany(Request $request)

$existingModels = $this->findOrFailCollection($requestCollection);

$modelsCollection = $this->getModel()
->hydrateRequestCollection($requestCollection, $existingModels);
$modelsCollection = $this->fillModels($this->getModel(), $existingModels, $requestCollection);

$this->checkPermission(static::class.'@patchMany', ['model' => $existingModels]);

Expand Down Expand Up @@ -328,6 +326,7 @@ protected function findOrFailCompoundEntity(array $routeParams)
}

/**
* @param array $routeParams
* @param null $limit
* @param null $offset
* @return Collection
Expand Down Expand Up @@ -522,7 +521,7 @@ protected function getModel()
}

/**
* @param null $entityId
* @param null $entityKey
* @param array $requestEntity
* @return array
*/
Expand Down
5 changes: 2 additions & 3 deletions Controllers/LinkedEntityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function attachOne(Request $request, $id, $childId)
if (! empty($requestEntity)) {
$childModel = $this->findOrNewChildEntity($childId, $parent);
$this->validateRequest($requestEntity, $this->getValidationRules($childId, $requestEntity), $childModel, true);
$childModel->fill($requestEntity)->save();
$this->fillModel($childModel, $requestEntity)->save();
} else {
$childModel = $this->findOrFailChildEntity($childId, $parent);
}
Expand Down Expand Up @@ -89,8 +89,7 @@ protected function processMany(Request $request, $id, $method)
$this->validateRequestCollection($requestCollection, $this->getChildModel(), true);

$existingChildren = $this->findChildrenCollection($requestCollection, $parent);
$childModels = $this->getChildModel()->hydrateRequestCollection($requestCollection, $existingChildren);

$childModels = $this->fillModels($this->getChildModel(), $existingChildren, $requestCollection);
$this->checkPermission(static::class.'@'.$method.'Many', ['model' => $parent, 'children' => $childModels]);

$this->preSync($parent, $childModels);
Expand Down

0 comments on commit 08b0e97

Please sign in to comment.