BUG Fixed handing of false values in GridState_Data

API Added ability to unset values
This commit is contained in:
Damian Mooyman 2014-02-04 10:18:26 +13:00
parent 6df276c843
commit cd213ab488
2 changed files with 14 additions and 10 deletions

View File

@ -153,7 +153,7 @@ class GridState_Data {
* @return mixed The value associated with this key, or the value specified by $default if not set * @return mixed The value associated with this key, or the value specified by $default if not set
*/ */
public function getData($name, $default = null) { public function getData($name, $default = null) {
if(empty($this->data[$name])) { if(!isset($this->data[$name])) {
$this->data[$name] = $default; $this->data[$name] = $default;
} else if(is_array($this->data[$name])) { } else if(is_array($this->data[$name])) {
$this->data[$name] = new GridState_Data($this->data[$name]); $this->data[$name] = new GridState_Data($this->data[$name]);
@ -169,6 +169,10 @@ class GridState_Data {
public function __isset($name) { public function __isset($name) {
return isset($this->data[$name]); return isset($this->data[$name]);
} }
public function __unset($name) {
unset($this->data[$name]);
}
public function __toString() { public function __toString() {
if(!$this->data) { if(!$this->data) {

View File

@ -123,22 +123,22 @@ class GridFieldTest extends SapphireTest {
$obj->State->NoValue = 10; $obj->State->NoValue = 10;
$this->assertEquals(10, $obj->State->NoValue); $this->assertEquals(10, $obj->State->NoValue);
$this->assertEquals(10, $obj->State->NoValue(20)); $this->assertEquals(10, $obj->State->NoValue(20));
// Test that values can be set, unset, and inspected
$this->assertFalse(isset($obj->State->NotSet));
$obj->State->NotSet = false;
$this->assertTrue(isset($obj->State->NotSet));
unset($obj->State->NotSet);
$this->assertFalse(isset($obj->State->NotSet));
//Show that values returned by the assignment function don't // Test that false evaluating values are storable
// actually return the value stored
$this->assertEquals(0, $obj->State->Falsey0(0)); // expect 0 back $this->assertEquals(0, $obj->State->Falsey0(0)); // expect 0 back
$this->assertEquals(0, $obj->State->Falsey0(10)); // expect 0 back $this->assertEquals(0, $obj->State->Falsey0(10)); // expect 0 back
$this->assertEquals(0, $obj->State->Falsey0); //expect 0 back $this->assertEquals(0, $obj->State->Falsey0); //expect 0 back
$obj->State->Falsey0 = 0; //manually assign 0 $obj->State->Falsey0 = 0; //manually assign 0
$this->assertEquals(0, $obj->State->Falsey0); //expect 0 back $this->assertEquals(0, $obj->State->Falsey0); //expect 0 back
//repeat for other falsey values // Test that false is storable
$this->assertEquals(array(), $obj->State->Falsey1(array()));
$this->assertEquals(array(), $obj->State->Falsey1(array('test')));
$this->assertEquals(array(), $obj->State->Falsey1);
$obj->State->Falsey1 = array(); //manually assign 0
$this->assertEquals(array(), $obj->State->Falsey1); //expect 0 back
$this->assertFalse($obj->State->Falsey2(false)); $this->assertFalse($obj->State->Falsey2(false));
$this->assertFalse($obj->State->Falsey2(true)); $this->assertFalse($obj->State->Falsey2(true));
$this->assertFalse($obj->State->Falsey2); $this->assertFalse($obj->State->Falsey2);