Implmenting ArrayAcces in CakeValidationSet

This commit is contained in:
Jose Lorenzo Rodriguez 2012-05-05 21:00:40 -04:30
parent 877e6c0f66
commit 6f16a66b83
3 changed files with 136 additions and 5 deletions

View file

@ -467,6 +467,7 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
* @return void
**/
public function offsetUnset($field) {
$this->_parseRules();
unset($this->_fields[$field]);
}

View file

@ -1,6 +1,6 @@
<?php
/**
* ModelValidator.
* CakeValidationSet.
*
* Provides the Model validation logic.
*
@ -27,7 +27,7 @@ App::uses('CakeRule', 'Model/Validator');
* @package Cake.Model.Validator
* @link http://book.cakephp.org/2.0/en/data-validation.html
*/
class CakeValidationSet {
class CakeValidationSet implements ArrayAccess {
/**
* Holds the ValidationRule objects
@ -166,10 +166,13 @@ class CakeValidationSet {
* Sets a ValidationRule $rule for key $key
*
* @param mixed $key The key under which the rule should be set
* @param ValidationRule $rule The ValidationRule to be set
* @param CakeRule|array $rule The validation rule to be set
* @return ModelField
*/
public function setRule($key, CakeRule $rule) {
public function setRule($key, $rule) {
if (!$rule instanceof CakeRule) {
$rule = new CakeRule($rule);
}
$this->_rules[$key] = $rule;
return $this;
}
@ -241,4 +244,44 @@ class CakeValidationSet {
return $message;
}
/**
* Returns wheter an index exists in the rule set
*
* @param string $index name of the rule
* @return boolean
**/
public function offsetExists($index) {
return isset($this->_rules[$index]);
}
/**
* Returns a rule object by its index
*
* @param string $index name of the rule
* @return CakeRule
**/
public function offsetGet($index) {
return $this->_rules[$index];
}
/**
* Sets or replace a validation rule
*
* @param string $index name of the rule
* @param CakeRule|array rule to add to $index
**/
public function offsetSet($index, $rule) {
$this->setRule($index, $rule);
}
/**
* Unsets a validation rule
*
* @param string $index name of the rule
* @return void
**/
public function offsetUnset($index) {
unset($this->_rules[$index]);
}
}

View file

@ -24,7 +24,7 @@ App::uses('CakeValidationSet', 'Model/Validator');
*
* @package Cake.Test.Case.Model.Validator
*/
class CakeValidationSetTest extends CakeTestModel {
class CakeValidationSetTest extends CakeTestCase {
/**
* setUp method
@ -157,4 +157,91 @@ class CakeValidationSetTest extends CakeTestModel {
$this->assertEquals(array('validEmail', 'notEmpty'), array_keys($result));
}
/**
* Tests getting a rule from the set using array access
*
* @return void
*/
public function testArrayAccessGet() {
$Set = new CakeValidationSet('title', array(
'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
'numeric' => array('rule' => 'numeric'),
'other' => array('rule' => array('other', 1)),
));
$rule = $Set['notEmpty'];
$this->assertInstanceOf('CakeRule', $rule);
$this->assertEquals('notEmpty', $rule->rule);
$rule = $Set['numeric'];
$this->assertInstanceOf('CakeRule', $rule);
$this->assertEquals('numeric', $rule->rule);
$rule = $Set['other'];
$this->assertInstanceOf('CakeRule', $rule);
$this->assertEquals(array('other', 1), $rule->rule);
}
/**
* Tests checking a rule from the set using array access
*
* @return void
*/
public function testArrayAccessExists() {
$Set = new CakeValidationSet('title', array(
'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
'numeric' => array('rule' => 'numeric'),
'other' => array('rule' => array('other', 1)),
));
$this->assertTrue(isset($Set['notEmpty']));
$this->assertTrue(isset($Set['numeric']));
$this->assertTrue(isset($Set['other']));
$this->assertFalse(isset($Set['fail']));
}
/**
* Tests setting a rule in the set using array access
*
* @return void
*/
public function testArrayAccessSet() {
$Set = new CakeValidationSet('title', array(
'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
));
$this->assertFalse(isset($Set['other']));
$Set['other'] = array('rule' => array('other', 1));
$rule = $Set['other'];
$this->assertInstanceOf('CakeRule', $rule);
$this->assertEquals(array('other', 1), $rule->rule);
$this->assertFalse(isset($Set['numeric']));
$Set['numeric'] = new CakeRule(array('rule' => 'numeric'));
$rule = $Set['numeric'];
$this->assertInstanceOf('CakeRule', $rule);
$this->assertEquals('numeric', $rule->rule);
}
/**
* Tests unseting a rule from the set using array access
*
* @return void
*/
public function testArrayAccessUnset() {
$Set = new CakeValidationSet('title', array(
'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
'numeric' => array('rule' => 'numeric'),
'other' => array('rule' => array('other', 1)),
));
unset($Set['notEmpty']);
$this->assertFalse(isset($Set['notEmpty']));
unset($Set['numeric']);
$this->assertFalse(isset($Set['notEmpty']));
unset($Set['other']);
$this->assertFalse(isset($Set['notEmpty']));
}
}