-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:spira/spira into feature/app-pagi…
…nation-error-interception * 'master' of github.com:spira/spira: (21 commits) Applying style patch. Implemented getProfile for User. Updated tests and documentation. Applying style patch. Updated unit tests for User to include UserProfile. Updated put and patch User methods to include profile. Updated API documentation. comment on save documentation bits style patch tests abd fixes Applying style patch. Refactoring user database models. Adding UserProfile. relation validation child entity tests fixes validation pulled to upper level articles permalinks and meta separation validation fixes, wrapping up EntityController, ChildEntityController separation meta documentation style patch test default url decode article meta crud, seed, tests article meta factory and seeder test get all meta article crucial fix - get by permalink, also with test ...
- Loading branch information
Showing
46 changed files
with
2,212 additions
and
621 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
api/app/Extensions/Controller/RequestValidationTrait.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: redjik | ||
* Date: 04.08.15 | ||
* Time: 21:09 | ||
*/ | ||
|
||
namespace App\Extensions\Controller; | ||
|
||
use Laravel\Lumen\Routing\ValidatesRequests; | ||
use Spira\Repository\Collection\Collection; | ||
use Spira\Repository\Validation\ValidationException; | ||
use Spira\Repository\Validation\ValidationExceptionCollection; | ||
|
||
trait RequestValidationTrait | ||
{ | ||
use ValidatesRequests; | ||
|
||
/** | ||
* @param $entityCollection | ||
* @param string $keyName | ||
* @param string|null $rule | ||
* @return array | ||
* @throws ValidationExceptionCollection | ||
*/ | ||
protected function getIds($entityCollection, $keyName, $rule = null) | ||
{ | ||
$ids = []; | ||
$errors = []; | ||
$error = false; | ||
foreach ($entityCollection as $requestEntity) { | ||
if (isset($requestEntity[$keyName]) && $requestEntity[$keyName]) { | ||
try { | ||
$id = $requestEntity[$keyName]; | ||
$this->validateId($id, $keyName, $rule); | ||
$ids[] = $id; | ||
$errors[] = null; | ||
} catch (ValidationException $e) { | ||
$error = true; | ||
$errors[] = $e; | ||
} | ||
} else { | ||
$errors[] = null; | ||
} | ||
} | ||
if ($error) { | ||
throw new ValidationExceptionCollection($errors); | ||
} | ||
|
||
return $ids; | ||
} | ||
|
||
|
||
/** | ||
* Build notFoundException | ||
* @param string $keyName | ||
* @return ValidationException | ||
*/ | ||
protected function notFoundException($keyName = '') | ||
{ | ||
$validation = $this->getValidationFactory()->make([$keyName=>$keyName], [$keyName=>'notFound']); | ||
if (!$validation->fails()) { | ||
// @codeCoverageIgnoreStart | ||
throw new \LogicException("Validator should have failed"); | ||
// @codeCoverageIgnoreEnd | ||
} | ||
|
||
throw new ValidationException($validation->getMessageBag()); | ||
} | ||
|
||
/** | ||
* Get notFoundManyException | ||
* @param $ids | ||
* @param Collection $models | ||
* @param string $keyName | ||
* @return ValidationExceptionCollection | ||
*/ | ||
protected function notFoundManyException($ids, $models, $keyName = '') | ||
{ | ||
$errors = []; | ||
foreach ($ids as $id) { | ||
if ($models->get($id)) { | ||
$errors[] = null; | ||
} else { | ||
try { | ||
throw $this->notFoundException($keyName); | ||
} catch (ValidationException $e) { | ||
$errors[] = $e; | ||
} | ||
} | ||
} | ||
|
||
throw new ValidationExceptionCollection($errors); | ||
} | ||
|
||
/** | ||
* @param $id | ||
* @param string $keyName | ||
* @param string|null $rule | ||
* @throw ValidationException | ||
*/ | ||
protected function validateId($id, $keyName, $rule = null) | ||
{ | ||
if (!is_null($rule)) { | ||
$validation = $this->getValidationFactory()->make([$keyName=>$id], [$keyName=>$rule]); | ||
if ($validation->fails()) { | ||
throw new ValidationException($validation->getMessageBag()); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.