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();