Skip to content

Commit

Permalink
Merge branch 'master' of github.com:spira/spira into feature/app-pagi…
Browse files Browse the repository at this point in the history
…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
zakhenry committed Aug 7, 2015
2 parents af81779 + 2d4570d commit 572c22f
Show file tree
Hide file tree
Showing 46 changed files with 2,212 additions and 621 deletions.
112 changes: 112 additions & 0 deletions api/app/Extensions/Controller/RequestValidationTrait.php
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());
}
}
}
}
Loading

0 comments on commit 572c22f

Please sign in to comment.