ENHANCEMENT: Refactoring and documenting the ArrayData class. Deprecating the "object_to_array" method (fixes #4875, thanks tobych)

This commit is contained in:
Julian Seidenberg 2011-03-25 17:29:33 +13:00 committed by Ingo Schommer
parent dec5c0ae60
commit 5ea4615d4d

View File

@ -1,6 +1,6 @@
<?php <?php
/** /**
* Lets you wrap a bunch of array data into a {@link ViewableData} object. * Lets you wrap a bunch of array data, or object members, into a {@link ViewableData} object.
* *
* <code> * <code>
* new ArrayData(array( * new ArrayData(array(
@ -21,20 +21,19 @@ class ArrayData extends ViewableData {
protected $array; protected $array;
/** /**
* @param object|array $array Either an object with simple properties or an associative array. * @param object|array $value An associative array, or an object with simple properties.
* Converts object-properties to indices of an associative array. * Converts object properties to keys of an associative array.
*/ */
public function __construct($array) { public function __construct($value) {
if(is_object($array)) { if (is_object($value)) {
$this->array = self::object_to_array($array); $this->array = get_object_vars($value);
} elseif(is_array($array) && (ArrayLib::is_associative($array) || count($array) === 0)) { } elseif (ArrayLib::is_associative($value)) {
$this->array = $array; $this->array = $value;
} elseif (is_array($value) && count($value) === 0) {
$this->array = array();
} else { } else {
$this->array = $array; $message = 'Parameter to ArrayData constructor needs to be an object or associative array';
user_error( throw new InvalidArgumentException($message);
"ArrayData::__construct: Parameter needs to be an object or associative array",
E_USER_WARNING
);
} }
parent::__construct(); parent::__construct();
} }
@ -49,19 +48,27 @@ class ArrayData extends ViewableData {
} }
/** /**
* Get a value from a given field * Gets a field from this object.
* *
* @param string $f field key * @param string $field
* @return mixed *
* 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.
*/ */
public function getField($f) { public function getField($f) {
if((is_object($this->array[$f]) && !$this->array[$f] instanceof ViewableData) || (is_array($this->array[$f]) && ArrayLib::is_associative($this->array[$f]))) { $value = $this->array[$f];
return new ArrayData($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;
} }
return $this->array[$f];
} }
/** /**
* Add or set a field on this object. * Add or set a field on this object.
* *
@ -83,6 +90,7 @@ class ArrayData extends ViewableData {
} }
/** /**
* @deprecated Use get_object_vars($obj)
* Converts an object with simple properties to * Converts an object with simple properties to
* an associative array. * an associative array.
* *
@ -90,12 +98,7 @@ class ArrayData extends ViewableData {
* @return array * @return array
*/ */
protected static function object_to_array($obj) { protected static function object_to_array($obj) {
$arr = array(); return get_object_vars($obj);
foreach($obj as $k=>$v) {
$arr[$k] = $v;
}
return $arr;
} }
/** /**