mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENHANCEMENT: added getter to get array back out of an ArrayData instance. MINOR: updated docblocks in ArrayData
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@108012 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
ae6d1c8e33
commit
797c8bf3f3
@ -14,6 +14,10 @@
|
||||
*/
|
||||
class ArrayData extends ViewableData {
|
||||
|
||||
/**
|
||||
* @var array
|
||||
* @see ArrayData::_construct()
|
||||
*/
|
||||
protected $array;
|
||||
|
||||
/**
|
||||
@ -35,12 +39,27 @@ class ArrayData extends ViewableData {
|
||||
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) {
|
||||
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]);
|
||||
} else {
|
||||
return $this->array[$f];
|
||||
}
|
||||
|
||||
return $this->array[$f];
|
||||
}
|
||||
|
||||
/**
|
||||
@ -53,6 +72,12 @@ class ArrayData extends ViewableData {
|
||||
$this->array[$field] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check array to see if field isset
|
||||
*
|
||||
* @param string Field Key
|
||||
* @return bool
|
||||
*/
|
||||
public function hasField($f) {
|
||||
return isset($this->array[$f]);
|
||||
}
|
||||
@ -79,7 +104,4 @@ class ArrayData extends ViewableData {
|
||||
function forTemplate() {
|
||||
return var_export($this->array, true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
}
|
@ -84,6 +84,18 @@ class ArrayDataTest extends SapphireTest {
|
||||
$this->assertTrue($arrayData->hasField('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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user