-
-
Save tperrelli/a22ecc30228ff589df01efa382409e65 to your computer and use it in GitHub Desktop.
PHP Validates
This file contains hidden or 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 App\Units\Core\Validation; | |
use Illuminate\Validation\Validator; | |
use App\Units\Core\Validation\ValidatorException; | |
trait Validates | |
{ | |
/** | |
* Validate the given data with the given rules. | |
* | |
* @param array $data | |
* @param array $rules | |
* @param array $messages | |
* @param array $customAttributes | |
* @return void | |
*/ | |
public function validateThrowingException(array $data, array $rules, array $messages = [], array $customAttributes = []) | |
{ | |
$validator = $this->getValidationFactory()->make($data, $rules, $messages, $customAttributes); | |
if ($validator->fails()) { | |
$this->throwInvalidArgumentException($validator); | |
} | |
} | |
/** | |
* Throw the failed validation exception. | |
* | |
* @param Validator $validator | |
* @return void | |
*/ | |
protected function throwInvalidArgumentException(Validator $validator) | |
{ | |
throw new ValidatorException($validator->errors()); | |
} | |
/** | |
* Get a validation factory instance. | |
* | |
* @return \Illuminate\Contracts\Validation\Factory | |
*/ | |
protected function getValidationFactory() | |
{ | |
return app('validator'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment