2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
|
2008-01-08 07:37:50 +01:00
|
|
|
/**
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage view
|
|
|
|
*/
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-01-10 03:18:20 +01:00
|
|
|
* Lets you wrap a bunch of array data into a {@link ViewableData} object.
|
2007-07-19 12:40:28 +02:00
|
|
|
*
|
2008-01-10 03:18:20 +01:00
|
|
|
* <code>
|
2007-07-19 12:40:28 +02:00
|
|
|
* new ArrayData(array(
|
|
|
|
* "ClassName" => "Page",
|
|
|
|
* "AddAction" => "Add a new Page page",
|
|
|
|
* ));
|
2008-01-10 03:18:20 +01:00
|
|
|
* </code>
|
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @subpackage view
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class ArrayData extends ViewableData {
|
2007-10-20 10:36:47 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $array;
|
|
|
|
|
2008-01-10 03:15:46 +01:00
|
|
|
/**
|
2008-01-10 03:18:20 +01:00
|
|
|
* @param object|array $array Either an object with simple properties or an associative array.
|
|
|
|
* Converts object-properties to indices of an associative array.
|
2008-01-10 03:15:46 +01:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function __construct($array) {
|
2008-01-10 03:15:46 +01:00
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getField($f) {
|
2007-10-20 10:36:47 +02:00
|
|
|
if(is_array($this->array[$f])) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return new ArrayData($this->array[$f]);
|
2007-10-20 10:36:47 +02:00
|
|
|
} else {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->array[$f];
|
2007-10-20 10:36:47 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-10-20 10:36:47 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
public function hasField($f) {
|
|
|
|
return isset($this->array[$f]);
|
|
|
|
}
|
2007-10-20 10:36:47 +02:00
|
|
|
|
2008-01-10 03:15:46 +01:00
|
|
|
/**
|
|
|
|
* Converts an object with simple properties to
|
|
|
|
* an associative array.
|
|
|
|
*
|
2008-01-11 02:49:50 +01:00
|
|
|
* @todo Allow for recursive creation of DataObjectSets when property value is an object/array
|
2008-01-10 03:15:46 +01:00
|
|
|
*
|
|
|
|
* @param obj $obj
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
static function object_to_array($obj) {
|
|
|
|
$arr = array();
|
|
|
|
foreach($obj as $k=>$v) {
|
|
|
|
$arr[$k] = $v;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $arr;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|