From f1fdd81bc38e3062468c962ac3fcd97940818441 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Thu, 10 Jan 2008 02:15:46 +0000 Subject: [PATCH] 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 --- core/ArrayData.php | 33 ++++++++++++++++++++++++++++++++- core/model/DataObjectSet.php | 30 +++++++++++++++++++++++------- 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/core/ArrayData.php b/core/ArrayData.php index d4662a007..294c03e59 100755 --- a/core/ArrayData.php +++ b/core/ArrayData.php @@ -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) { - $this->array = $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; + } + } ?> \ No newline at end of file diff --git a/core/model/DataObjectSet.php b/core/model/DataObjectSet.php index 8243e8c57..6dc448061 100644 --- a/core/model/DataObjectSet.php +++ b/core/model/DataObjectSet.php @@ -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)) { - // 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; + // 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 + for($i=0; $iitems[] = $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();