ENHANCEMENT: added getter to get array back out of an ArrayData instance. MINOR: updated docblocks in ArrayData (from r108012)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@112705 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2010-10-18 22:39:19 +00:00
parent bd96d249f0
commit c35349f90d
2 changed files with 40 additions and 6 deletions

View File

@ -14,6 +14,10 @@
*/ */
class ArrayData extends ViewableData { class ArrayData extends ViewableData {
/**
* @var array
* @see ArrayData::_construct()
*/
protected $array; protected $array;
/** /**
@ -35,12 +39,27 @@ class ArrayData extends ViewableData {
parent::__construct(); parent::__construct();
} }
/**
* Get the source array
*
* @return array
*/
public function getArray() {
return $this->array;
}
/**
* Get a value from a given field
*
* @param string $f field key
* @return mixed
*/
public function getField($f) { public function getField($f) {
if((is_object($this->array[$f]) && !$this->array[$f] instanceof ViewableData) || (is_array($this->array[$f]) && ArrayLib::is_associative($this->array[$f]))) { if((is_object($this->array[$f]) && !$this->array[$f] instanceof ViewableData) || (is_array($this->array[$f]) && ArrayLib::is_associative($this->array[$f]))) {
return new ArrayData($this->array[$f]); return new ArrayData($this->array[$f]);
} else {
return $this->array[$f];
} }
return $this->array[$f];
} }
/** /**
@ -53,6 +72,12 @@ class ArrayData extends ViewableData {
$this->array[$field] = $value; $this->array[$field] = $value;
} }
/**
* Check array to see if field isset
*
* @param string Field Key
* @return bool
*/
public function hasField($f) { public function hasField($f) {
return isset($this->array[$f]); return isset($this->array[$f]);
} }
@ -79,7 +104,4 @@ class ArrayData extends ViewableData {
function forTemplate() { function forTemplate() {
return var_export($this->array, true); return var_export($this->array, true);
} }
} }
?>

View File

@ -85,6 +85,18 @@ class ArrayDataTest extends SapphireTest {
$this->assertEquals('Delta', $arrayData->getField('d')); $this->assertEquals('Delta', $arrayData->getField('d'));
} }
function testGetArray() {
$array = array(
'Foo' => 'Foo',
'Bar' => 'Bar',
'Baz' => 'Baz'
);
$arrayData = new ArrayData($array);
$this->assertEquals($arrayData->getArray(), $array);
}
} }
class ArrayDataTest_ArrayData_Exposed extends ArrayData { class ArrayDataTest_ArrayData_Exposed extends ArrayData {