2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Lets you wrap a bunch of array data into a {@link ViewableData} object.
|
2007-07-19 12:40:28 +02:00
|
|
|
*
|
2008-02-25 03:10:37 +01:00
|
|
|
* <code>
|
2007-07-19 12:40:28 +02:00
|
|
|
* new ArrayData(array(
|
|
|
|
* "ClassName" => "Page",
|
|
|
|
* "AddAction" => "Add a new Page page",
|
|
|
|
* ));
|
2008-02-25 03:10:37 +01:00
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @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-02-25 03:10:37 +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.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function __construct($array) {
|
2008-02-25 03:10:37 +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-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|