Skip to content

Instantly share code, notes, and snippets.

@beeblebrox3
Created September 30, 2014 13:19
Show Gist options
  • Save beeblebrox3/047f6bceadd8b09f3276 to your computer and use it in GitHub Desktop.
Save beeblebrox3/047f6bceadd8b09f3276 to your computer and use it in GitHub Desktop.
<?php
namespace App\Models\Observers;
use Illuminate\Validation\Factory;
abstract class AbstractObserver
{
/**
* The Validator instance
*
* @var \Illuminate\Validation\Factory
*/
protected $validator;
/**
* Name of the connection to be used by validator
* @var string
*/
protected $connection = null;
/**
* Validation rules
* @var array
*/
protected $rules = array (
'creating' => array(),
'updating' => array(),
);
/**
* Error messages
* @var mixed
*/
protected $errors = null;
/**
* @param Factory $validator
*/
public function __construct(Factory $validator)
{
$this->validator = $validator;
$this->validator->getPresenceVerifier()->setConnection($this->connection);
}
/**
* Validate $data against $rules
* @param array $data data to be validated
* @param array $rules validation rules
* @return boolean
*/
protected function passes(array $data, array $rules)
{
$validator = $this->validator->make($data, $rules);
if ($validator->passes()) {
return true;
}
$this->errors = $validator->errors();
return false;
}
/**
* Update unique rules adding the current $id to the exclusion list
* @param array $rules
* @return array modified $rules
*/
protected function updateUniques(array $rules)
{
$data = $this->data;
foreach ($rules as $field => $rule) {
$rules[$field] = preg_replace(
"/unique\:([a-z_]+)/i",
'unique:$1,' . $field . ',' . $data['id'],
$rule
);
}
return $rules;
}
/**
* Determine what rule set to use based on current route
* If doesn't exists a ruleset with the name of the route, the
* $default will be used. If $default is not defined, the 'creating' will
* be used instead
* @param string $default
* @param boolean $forceRule if true, will not try to detect rules based on current Route
* @return array
*/
public function resolveRules($default = null, $forceRule = false)
{
if ($forceRule === true && is_null($default)) {
throw new \InvalidArgumentException("You must define default rules when use force mode");
}
$rules = $this->rules['creating'];
if ($forceRule === true && isset($this->rules[$default])) {
return array_merge($rules, $this->rules[$default]);
}
$route = \Route::currentRouteName();
if (isset($this->rules[$route])) {
$rules = array_merge($rules, $this->rules[$route]);
} elseif ($default && isset($this->rules[$default])) {
$rules = array_merge($rules, $this->rules[$default]);
}
return $rules;
}
public function creating(\Eloquent $model)
{
$this->data = $model->getAttributes();
$rules = $this->resolveRules('creating');
if ($this->passes($this->data, $rules)) {
// for models that have password
if ($model->isFillable('password')) {
if ($model->password_confirmation) {
unset($model->password_confirmation);
}
$model->password = \Hash::make($model->password);
}
return true;
}
$model->setErrors($this->errors);
return false;
}
public function updating(\Eloquent $model)
{
$this->data = $model->getAttributes();
if (!$model->isDirty('password')) {
unset($this->data['password']);
unset($this->data['password_confirmation']);
}
$rules = $this->resolveRules('updating');
$rules = $this->updateUniques($rules);
if ($this->passes($this->data, $rules)) {
// for models that have password
if ($model->isFillable('password')) {
if ($model->password_confirmation) {
unset($model->password_confirmation);
}
if ($model->isDirty('password')) {
$model->password = \Hash::make($model->password);
}
}
return true;
}
$model->setErrors($this->errors);
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment