Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup file, refactor, make PSR-1 and PSR-2 compliant, make save compatible with Eloquent save #1

Merged
merged 5 commits into from
Apr 14, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
298 changes: 153 additions & 145 deletions src/Awareness/Aware.php
Original file line number Diff line number Diff line change
@@ -1,167 +1,175 @@
<?php namespace Awareness;

use \Illuminate\Database\Eloquent;
use \Illuminate\Support\Contracts\MessageProviderInterface;
use \Illuminate\Support\MessageBag;
use \Illuminate\Validation;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Contracts\MessageProviderInterface;
use Illuminate\Support\MessageBag;
use Illuminate\Support\Facades\Validator;

/**
* Aware Models
* Self-validating Eloquent Models
*/
abstract class Aware extends Eloquent\Model implements MessageProviderInterface {
abstract class Aware extends Model implements MessageProviderInterface
{

protected
/**
* Error message container
*
* @var Illuminate\Support\MessageBag
*/

protected $errorBag;

/**
* Aware Validation Messages
*
* @var array $messages
*/

public static $messages = array();

/**
* Aware Validation Rules
*
* @var array $rules
*/
public static $rules = array();

/**
* Returns the errors container
*
* @return Illuminate\Support\MessageBag
*/
public function getErrors()
{
if (!$this->errorBag) {
$this->errorBag = new MessageBag();
}

return $this->errorBag;
}

/**
* Error message container
*
* @var Illuminate\Support\MessageBag
*/
$error_bag;
* Returns attirbutes with updated values
*
* @return array
*/
public function getChanged()
{
return array_diff($this->attributes, $this->original);
}

public static
/**
* Returns the errors container
*
* @return Illuminate\Support\MessageBag
*/
public function getMessageBag()
{
return $this->errors();
}

/**
* Aware Validation Messages
*
* @var array $messages
*/
$messages = array(),
* Returns rules and data that needs validating
*
* @return array
*/
public function getValidationInfo(array $rulesOverride = null, array $messagesOverride = null)
{
if ($this->exists) {
$data = $this->getChanged();
$rules = array_intersect_key($rulesOverride ?: static::$rules, $data);
} else {
$data = $this->attributes;
$rules = $rulesOverride ?: static::$rules;
}

return count($rules) > 0 ?
array($data, $rules, $messagesOverride ?: static::$messages) :
array(null, null, null);
}

/**
* Aware Validation Rules
*
* @var array $rules
*/
$rules = array();

/**
* Returns the errors container
*
* @return Illuminate\Support\MessageBag
*/
function getErrors() {
if (!$this->error_bag) {
$this->error_bag = new MessageBag();
* Validate the Model
* runs the validator and binds any errors to the model
*
* @param array $rules
* @param array $messages
* @return bool
*/
public function isValid(array $rulesOverride = null, array $messagesOverride = null)
{
$valid = true;
list($data, $rules, $messages) = $this->getValidationInfo($rulesOverride);

if ($rules) {
$validator = Validator::make($data, $rules, $messages);
$valid = $validator->passes();
}

if (!$valid) {
$this->errorBag = $validator->errors();
} elseif ($this->errorBag && $this->errorBag->any()) {
$this->errorBag = new MessageBag();
}

return $valid;
}
return $this->error_bag;
}

/**
* Returns attirbutes with updated values
*
* @return array
*/
function getChanged() {
return array_diff($this->attributes, $this->original);
}

/**
* Returns the errors container
*
* @return Illuminate\Support\MessageBag
*/
function getMessageBag() {
return $this->errors();
}

/**
* Returns rules and data that needs validating
*
* @return array
*/
function getValidationInfo($rules_override=null, $messages_override=null) {

if ($this->exists) {
$data = $this->getChanged();
$rules = array_intersect_key($rules_override ?: static::$rules, $data);
} else {
$data = $this->attributes;
$rules = $rules_override ?: static::$rules;

/**
* Called evertime a model is saved - to halt the save, return false
*
* @return bool
*/
public function onSave()
{
return true;
}

return count($rules) > 0 ?
array($data, $rules, $messages_override ?: static::$messages) :
array(null, null, null);
}

/**
* Validate the Model
* runs the validator and binds any errors to the model
*
* @param array $rules
* @param array $messages
* @return bool
*/
function isValid($rules_override=null, $messages_override=null) {
$valid = true;
list($data, $rules, $messages) = $this->getValidationInfo($rules_override);

if ($rules) {
$validator = Validator::make($data, $rules, $messages);
$valid = $validator->passes();
/**
* Called evertime a model is forceSaved - to halt the forceSave, return false
*
* @return bool
*/
public function onForceSave()
{
return true;
}

if (!$valid) {
$this->error_bag = $validator->errors();
} else if ($this->error_bag && $this->error_bag->any()) {
$this->error_bag = new MessageBag();
/**
* Save the model if it is valid
*
* @param array $rules
* @param array $messages
* @param closure $callback
* @return bool
*/
public function save(array $rules = array(), array $messages = array(), Closure $callback = null)
{
// evaluate onSave
$before = is_null($callback) ? $this->onSave() : call_user_func($callback, $this);

// check before & valid, then pass to parent
return ($before && $this->isValid($rules, $messages)) ? parent::save() : false;
}

return $valid;
}

/**
* Called evertime a model is saved - to halt the save, return false
*
* @return bool
*/
function onSave() {
return true;
}

/**
* Called evertime a model is forceSaved - to halt the forceSave, return false
*
* @return bool
*/
function onForceSave() {
return true;
}

/**
* Save the model if it is valid
*
* @param array
* @param array
* @param closure
* @return bool
*/
function save($rules=array(), $messages=array(), $onSave=null) {
// evaluate onSave
$before = is_null($onSave) ? $this->onSave() : $onSave($this);

// check before & valid, then pass to parent
return ($before && $this->isValid($rules, $messages)) ? parent::save() : false;
}

/**
* Attempts to save model even if it doesn't validate
*
* @param array
* @param array
* @return bool
*/
function forceSave($rules=array(), $messages=array(), $onForceSave=null) {
// execute onForceSave
$before = is_null($onForceSave) ? $this->onForceSave() : $onForceSave($this);

// validate the model
$this->isValid($rules, $messages);

// save regardless of the result of validation
return $before ? parent::save() : false;
}

}
/**
* Attempts to save model even if it doesn't validate
*
* @param array $rules
* @param array $messages
* @param callable $callback
* @return bool
*/
public function forceSave(array $rules = array(), array $messages = array(), Closure $callback = null)
{
// execute onForceSave
$before = is_null($callback) ? $this->onForceSave() : call_user_func($callback, $this);

// validate the model
$this->isValid($rules, $messages);

// save regardless of the result of validation
return $before ? parent::save() : false;
}
}
17 changes: 17 additions & 0 deletions tests/AwareModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use \Mockery as m;

class ModelTest extends PHPUnit_Framework_TestCase
{
function tearDown()
{
m::close();
}

function testErrorsMethod()
{
$model = m::mock('\Awareness\Aware[]');
$this->assertInstanceOf('\Illuminate\Support\MessageBag', $model->getErrors());
}
}
20 changes: 0 additions & 20 deletions tests/ModelTest.php

This file was deleted.