Skip to content
Permalink
Browse files

Implmenting ArrayAcces in CakeValidationSet

  • Loading branch information...
lorenzo committed May 6, 2012
1 parent 877e6c0 commit 6f16a66b833176edd7a8e81c1a15dbe83fdd34d5
@@ -467,6 +467,7 @@ public function offsetSet($field, $rules) {
* @return void
**/
public function offsetUnset($field) {
$this->_parseRules();
unset($this->_fields[$field]);
}
@@ -1,6 +1,6 @@
<?php
/**
* ModelValidator.
* CakeValidationSet.
*
* Provides the Model validation logic.
*
@@ -27,7 +27,7 @@
* @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 @@ public function getRules() {
* 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 @@ protected function _processValidationResponse($name, $rule) {
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]);
}
}
@@ -24,7 +24,7 @@
*
* @package Cake.Test.Case.Model.Validator
*/
class CakeValidationSetTest extends CakeTestModel {
class CakeValidationSetTest extends CakeTestCase {
/**
* setUp method
@@ -157,4 +157,91 @@ public function testSetRules() {
$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']));
}
}

0 comments on commit 6f16a66

Please sign in to comment.
You can’t perform that action at this time.