2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
2011-03-25 05:29:33 +01:00
|
|
|
* Lets you wrap a bunch of array data, or object members, 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
|
|
|
|
2010-10-19 00:39:19 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
* @see ArrayData::_construct()
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $array;
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
2011-03-25 05:29:33 +01:00
|
|
|
* @param object|array $value An associative array, or an object with simple properties.
|
|
|
|
* Converts object properties to keys of an associative array.
|
2008-02-25 03:10:37 +01:00
|
|
|
*/
|
2011-03-25 05:29:33 +01:00
|
|
|
public function __construct($value) {
|
|
|
|
if (is_object($value)) {
|
|
|
|
$this->array = get_object_vars($value);
|
|
|
|
} elseif (ArrayLib::is_associative($value)) {
|
|
|
|
$this->array = $value;
|
|
|
|
} elseif (is_array($value) && count($value) === 0) {
|
|
|
|
$this->array = array();
|
2008-02-25 03:10:37 +01:00
|
|
|
} else {
|
2011-03-25 05:29:33 +01:00
|
|
|
$message = 'Parameter to ArrayData constructor needs to be an object or associative array';
|
|
|
|
throw new InvalidArgumentException($message);
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
2009-03-04 04:44:11 +01:00
|
|
|
parent::__construct();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2010-10-19 00:39:19 +02:00
|
|
|
/**
|
|
|
|
* Get the source array
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2011-03-27 12:37:06 +02:00
|
|
|
public function toMap() {
|
2010-10-19 00:39:19 +02:00
|
|
|
return $this->array;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-25 05:29:33 +01:00
|
|
|
* Gets a field from this object.
|
2010-10-19 00:39:19 +02:00
|
|
|
*
|
2011-03-25 05:29:33 +01:00
|
|
|
* @param string $field
|
|
|
|
*
|
|
|
|
* If the value is an object but not an instance of
|
|
|
|
* ViewableData, it will be converted recursively to an
|
|
|
|
* ArrayData.
|
|
|
|
*
|
|
|
|
* If the value is an associative array, it will likewise be
|
|
|
|
* converted recursively to an ArrayData.
|
2010-10-19 00:39:19 +02:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function getField($f) {
|
2011-03-25 05:29:33 +01:00
|
|
|
$value = $this->array[$f];
|
|
|
|
if (is_object($value) && !$value instanceof ViewableData) {
|
|
|
|
return new ArrayData($value);
|
|
|
|
} elseif (ArrayLib::is_associative($value)) {
|
|
|
|
return new ArrayData($value);
|
|
|
|
} else {
|
|
|
|
return $value;
|
2007-10-20 10:36:47 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2010-10-15 05:47:27 +02:00
|
|
|
/**
|
|
|
|
* Add or set a field on this object.
|
|
|
|
*
|
|
|
|
* @param string $field
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function setField($field, $value) {
|
|
|
|
$this->array[$field] = $value;
|
|
|
|
}
|
|
|
|
|
2010-10-19 00:39:19 +02:00
|
|
|
/**
|
|
|
|
* Check array to see if field isset
|
|
|
|
*
|
|
|
|
* @param string Field Key
|
|
|
|
* @return bool
|
|
|
|
*/
|
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
|
|
|
|
2011-03-22 22:23:57 +01:00
|
|
|
/**
|
|
|
|
* Converts an associative array to a simple object
|
|
|
|
*
|
|
|
|
* @param array
|
|
|
|
* @return obj $obj
|
|
|
|
*/
|
|
|
|
public static function array_to_object($arr = null) {
|
|
|
|
$obj = new stdClass();
|
|
|
|
if ($arr) foreach($arr as $name => $value) $obj->$name = $value;
|
|
|
|
return $obj;
|
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
|
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);
|
|
|
|
}
|
2011-03-27 12:37:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated 3.0 Use {@link ArrayData::toMap()}.
|
|
|
|
*/
|
|
|
|
public function getArray() {
|
2011-10-29 01:02:11 +02:00
|
|
|
Deprecation::notice('3.0', 'Use ArrayData::toMap() instead.');
|
2011-03-27 12:37:06 +02:00
|
|
|
return $this->toMap();
|
|
|
|
}
|
|
|
|
|
2010-10-19 00:39:19 +02:00
|
|
|
}
|