mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Merged revisions 47808 via svnmerge from
svn://svn.silverstripe.com/silverstripe/modules/sapphire/branches/2.2.0-mesq ........ r47808 | ischommer | 2008-01-10 14:08:05 +1300 (Thu, 10 Jan 2008) | 1 line allowing object-parameters in DataObjectSet and ArrayData, added ArrayData::object_to_array() ........ git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@47811 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
baefbd1b96
commit
f1fdd81bc3
@ -22,8 +22,21 @@ class ArrayData extends ViewableData {
|
||||
|
||||
protected $array;
|
||||
|
||||
/**
|
||||
* @param object|array $array Either an object with simple properties or an associative array
|
||||
*/
|
||||
public function __construct($array) {
|
||||
if(is_object($array)) {
|
||||
$this->array = self::object_to_array($array);
|
||||
} elseif(is_array($array) && ArrayLib::is_associative($array)) {
|
||||
$this->array = $array;
|
||||
} else {
|
||||
$this->array = $array;
|
||||
user_error(
|
||||
"ArrayData::__construct: Parameter needs to be an object or associative array",
|
||||
E_USER_WARNING
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function getField($f) {
|
||||
@ -38,6 +51,24 @@ class ArrayData extends ViewableData {
|
||||
return isset($this->array[$f]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an object with simple properties to
|
||||
* an associative array.
|
||||
*
|
||||
* TODO Allow for recursive creation of DataObjectSets when property value is an object/array
|
||||
*
|
||||
* @param obj $obj
|
||||
* @return array
|
||||
*/
|
||||
static function object_to_array($obj) {
|
||||
$arr = array();
|
||||
foreach($obj as $k=>$v) {
|
||||
$arr[$k] = $v;
|
||||
}
|
||||
|
||||
return $arr;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
@ -63,16 +63,32 @@ class DataObjectSet extends ViewableData implements Iterator {
|
||||
|
||||
/**
|
||||
* Create a new DataObjectSet.
|
||||
* @param array|DataObject,... $items The DataObjects to use in this set, either as an array or as multiple variables
|
||||
*
|
||||
* @param ViewableData|array|mixed $items Parameters to use in this set, either as an associative array, object with simple properties, or as multiple parameters.
|
||||
* TODO Does NOT automatically convert objects with complex datatypes (e.g. converting arrays within an objects to its own DataObjectSet)
|
||||
*/
|
||||
public function __construct($items = null, $otherArg = null) {
|
||||
public function __construct($items = null) {
|
||||
if($items) {
|
||||
if (is_array($items)) {
|
||||
// if the first parameter is not an array, or we have more than one parameter, collate all parameters to an array
|
||||
// otherwise use the passed array
|
||||
$itemsArr = (!is_array($items) || count(func_get_args()) > 1) ? func_get_args() : $items;
|
||||
|
||||
// We now have support for using the key of a data object set
|
||||
$this->items = $items;
|
||||
} else if($allArgs = func_get_args()) {
|
||||
$this->items = $allArgs;
|
||||
for($i=0; $i<count($itemsArr); $i++) {
|
||||
if(is_subclass_of($itemsArr[$i], 'ViewableData')) {
|
||||
$this->items[] = $itemsArr[$i];
|
||||
} elseif(is_object($itemsArr[$i]) || ArrayLib::is_associative($itemsArr[$i])) {
|
||||
$this->items[] = new ArrayData($itemsArr[$i]);
|
||||
} else {
|
||||
user_error(
|
||||
"DataObjectSet::__construct: Passed item #{$i} is not an object or associative array,
|
||||
can't be properly iterated on in templates",
|
||||
E_USER_WARNING
|
||||
);
|
||||
$this->items[] = $itemsArr[$i];
|
||||
}
|
||||
}
|
||||
|
||||
$this->current = $this->prepareItem(current($this->items));
|
||||
}
|
||||
parent::__construct();
|
||||
|
Loading…
x
Reference in New Issue
Block a user