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) {
|
2008-07-17 23:44:47 +02:00
|
|
|
if((is_object($this->array[$f]) && !$this->array[$f] instanceof ArrayData) || (is_array($this->array[$f]) && ArrayLib::is_associative($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.
|
|
|
|
*
|
|
|
|
* @param obj $obj
|
|
|
|
* @return array
|
|
|
|
*/
|
2008-07-17 23:44:47 +02:00
|
|
|
protected static function object_to_array($obj) {
|
2008-02-25 03:10:37 +01:00
|
|
|
$arr = array();
|
|
|
|
foreach($obj as $k=>$v) {
|
|
|
|
$arr[$k] = $v;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $arr;
|
|
|
|
}
|
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
/**
|
|
|
|
* This is pretty crude, but it helps diagnose error situations
|
|
|
|
*/
|
|
|
|
function forTemplate() {
|
|
|
|
return var_export($this->array, true);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|