Skip to content

Commit

Permalink
new: change folder structure, add trait and support to set messages
Browse files Browse the repository at this point in the history
  • Loading branch information
zot24 committed Jul 30, 2015
1 parent 74fd9b3 commit 51599e3
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 46 deletions.
37 changes: 33 additions & 4 deletions src/BaseValidator.php
@@ -1,11 +1,13 @@
<?php namespace Motty\Laravel\Validator;

use Illuminate\Contracts\Validation\Factory;

abstract class BaseValidator
{
/**
* The validator instance
* Validator factory object
*
* @var object
* @var Factory
*/
protected $validator;

Expand All @@ -30,6 +32,23 @@ abstract class BaseValidator
*/
protected $errors = [];

/**
* Validation messages
*
* @var array
*/
public $messages = [];

/**
* Construct
*
* @param Factory $validator
*/
public function __construct(Factory $validator)
{
$this->validator = $validator;
}

/**
* Set data to validate
*
Expand All @@ -48,10 +67,20 @@ public function with(array $data)
*
* @return boolean
*/
abstract public function validate();
public function validate()
{
$validator = $this->validator->make($this->data, $this->rules, $this->messages);

if ($validator->fails()) {
$this->errors = $validator->messages();
return false;
}

return true;
}

/**
* Return errors
* Return the errors message bag
*
* @return \Illuminate\Support\MessageBag
*/
Expand Down
39 changes: 39 additions & 0 deletions src/Exceptions/ValidateException.php
@@ -0,0 +1,39 @@
<?php namespace Motty\Laravel\Validator\Exceptions;

use Exception;
use Illuminate\Support\MessageBag;

class ValidateException extends Exception
{
/**
* Errors object
*
* @var \Illuminate\Support\MessageBag
*/
protected $errors;

/**
* Create a new validate exception instance
*
* @param string $message
* @param MessageBag $errors
* @param integer $code
* @param Exception|null $previous
*/
public function __construct($message, MessageBag $errors, $code = 0, Exception $previous = null)
{
$this->errors = $errors;

parent::__construct($message, $code, $previous);
}

/**
* Gets the errors object
*
* @return \Illuminate\Support\MessageBag
*/
public function erros()
{
return $this->errors;
}
}
42 changes: 0 additions & 42 deletions src/Laravel/Validator.php

This file was deleted.

52 changes: 52 additions & 0 deletions src/Traits/Validatable.php
@@ -0,0 +1,52 @@
<?php namespace Motty\Laravel\Validator\Traits;

use Motty\Laravel\Validator\Contracts\Validator;
use Motty\Laravel\Validator\Exceptions\ValidateException;

trait Validatable
{
/**
* The errors MessageBag instance
*
* @var \Illuminate\Support\MessageBag
*/
protected $errors;

/**
* Run the validation checks in the input data
*
* @param array $data
*
* @return bool
* @throws Exception
* @throws ValidateException
*/
public function runValidationChecks(array $data)
{
foreach ($this->validators as $validator) {
if ($validator instanceof Validator) {
if (!$validator->with($data)->validate()) {
$this->errors->merge($validator->errors());
}
} else {
throw new Exception("{$validator} is not an instance of Motty\\Laravel\\Validator\\Contracts\\Validator");
}
}

if ($this->errors->isEmpty()) {
return true;
}

throw new ValidateException('Validation failed', $this->errors);
}

/**
* Return the errors message bag
*
* @return \Illuminate\Support\MessageBag
*/
public function errors()
{
return $this->errors;
}
}

0 comments on commit 51599e3

Please sign in to comment.