Skip to content

Instantly share code, notes, and snippets.

@BerezhniyDmitro
Created December 3, 2018 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BerezhniyDmitro/4ef6b59b5143cf10cb9c0ea471d34b36 to your computer and use it in GitHub Desktop.
Save BerezhniyDmitro/4ef6b59b5143cf10cb9c0ea471d34b36 to your computer and use it in GitHub Desktop.
<?php
namespace App\Models\CodeBlocks\Validator;
use App\Models\CodeBlocks\Decorator\CodeBlockTypesCIValidationContract;
use App\Models\Interfaces\ServiceValidable;
use App\ValueObject\Email;
use Components\Core\Container;
use App\Models\General\CI;
use Guzzle\Service\Description\ValidatorInterface;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Validation;
/**
* Class InputCodeBlockValidation
* @package App\Models\CodeBlocks\Validator
*/
class InputCodeBlockValidation implements ServiceValidable
{
/**
* @var array
*/
private $rawData;
/**
* @var \CI_Controller
*/
private $CI;
/**
* @var array
*/
private $errors = [];
/**
* Валидатор данных полученых с POST запроса
*
* @var null|\Symfony\Component\Validator\Validator\ValidatorInterface
*/
private $validator = null;
/**
* @var bool флаг валидности данных
*/
private $isValid;
/**
* InputCodeBlockValidation constructor.
* @throws \Exception
*/
public function __construct()
{
$this->CI = Container::instance()->container->get(CI::class)->getInstance();
$data = $this->CI->input->post();
$this->rawData = empty($data) ? [] : $data;
$this->validator = Validation::createValidator();
$this->isValid = true;
}
/**
* @return InputCodeBlockValidation
* @throws \Exception
*/
public static function instance()
{
return new self();
}
/**
* Валидирует список пришедших данных
*
* @return null
*/
public function validate()
{
if (empty($this->rawData['code_blocks'])) {
return;
}
$constraintMap = new CodeBlockConstraintMap();
foreach ($this->rawData['code_blocks'] as $codeBlock) {
$constraint = $constraintMap->getConstraintByKey($codeBlock['type']);
$errors = $this->validator->validate($codeBlock['params'], $constraint);
if ($errors->count() > 0) {
$this->addToStackErrors($errors);
}
}
}
/**
* Возвращает список найденных ошибок
*
* @return array
*/
public function getErrors()
{
return implode(' - ', $this->errors);
}
/**
* Метод возвращает успешность валидации
*
* @return bool
*/
public function isSuccess()
{
return $this->isValid;
}
/**
* Метод возвращает данные переданные постом
*
* @return array
*/
public function getPostData()
{
return $this->rawData;
}
/**
* Метод в случае ошибок наполняет массив ошибок
*
* @param ConstraintViolationListInterface $errors массив ошибок
*/
private function addToStackErrors($errors)
{
foreach ($errors as $error) {
$this->errors[] = $error->getMessage();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment