Created
October 17, 2013 00:33
-
-
Save dadamssg/7017396 to your computer and use it in GitHub Desktop.
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
<?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