Skip to content

Instantly share code, notes, and snippets.

@heruputra
Forked from gravitano/Creator.php
Created March 28, 2014 13:12
Show Gist options
  • Select an option

  • Save heruputra/9832356 to your computer and use it in GitHub Desktop.

Select an option

Save heruputra/9832356 to your computer and use it in GitHub Desktop.
<?php namespace Smile\Services\Creators;
use Illuminate\Validation\Factory as Validator;
abstract class Creator implements CreatorInterface
{
protected $model;
protected $errors;
protected $rules = [];
protected $messages = [];
protected $input = [];
public function __construct(Validator $validator) {
$this->model = new $this->model;
$this->validator = $validator;
}
abstract public function create($input);
public function validate($input, $rules = array(), $messages = array())
{
$validator = $this->validator->make($input, $rules, $messages);
if($validator->passes())
{
return true;
}
$this->errors = $validator->messages();
return false;
}
public function isValid($input = null)
{
$input = $input ?: $this->input;
return $this->validate($input, $this->rules, $this->messages);
}
public function getErrors()
{
return $this->errors;
}
}
<?php namespace Smile\Services\Creators;
use Exception;
class CreatorException extends Exception
{
protected $errors;
public function __construct($errors, $code = 0, Exception $previous = null)
{
$this->errors = $errors;
}
public function getErrors()
{
return $this->errors;
}
}
<?php namespace Smile\Services\Creators;
interface CreatorInterface
{
public function create($input);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment