Skip to content

Instantly share code, notes, and snippets.

@dadamssg
Created October 17, 2013 00:33
Show Gist options
  • Save dadamssg/7017396 to your computer and use it in GitHub Desktop.
Save dadamssg/7017396 to your computer and use it in GitHub Desktop.
<?php namespace MyApp\Validators;
use ProgrammingAreHard\Data\Validators\UserValidation;
use ProgrammingAreHard\Data\Validators\ValidationService;
use ProgrammingAreHard\Data\Validators\ValidatorInterface;
class RegistrationValidator implements ValidatorInterface {
/**
* @var \ProgrammingAreHard\Data\Validators\ValidationService
*/
protected $validator;
/**
* @var \ProgrammingAreHard\Data\Validators\UserValidation
*/
protected $userValidation;
/**
* Input data
*
* @var array
*/
protected $input;
/**
* Inject the validation service and user validation rules
*
* @param \ProgrammingAreHard\Data\Validators\ValidationService $validator
* @param \ProgrammingAreHard\Data\Validators\UserValidation $userValidation
*/
public function __construct(ValidationService $validator, UserValidation $userValidation)
{
$this->validator = $validator;
$this->userValidation = $userValidation;
}
/**
* Validate the data for the registration form
*
* @param array $input
* @return bool
*/
public function validate(array $input)
{
$this->input = $input;
$rules = $this->userValidation->getRulesForCreate();
$this->validator->setRules($rules);
if ($this->validator->validate($this->input)) {
return true;
}
return false;
}
/**
* Get only the user input provided
*
* @return array
*/
public function getUserData()
{
return array_only($this->input, $this->userValidation->getFields());
}
/**
* Get all errors from the validation attempt
*
* @return array
*/
public function getErrors()
{
return $this->validator->getErrors();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment