mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
5c9044a007
API Introduce HTMLFragment as casting helper for HTMLText with shortcodes disabled API Introduce DBField::CDATA for XML file value encoding API RSSFeed now casts from the underlying model rather than by override API Introduce CustomMethods::getExtraMethodConfig() to allow metadata to be queried BUG Remove _call hack from VirtualPage API Remove FormField::$dontEscape API Introduce HTMLReadonlyField for non-editable readonly HTML API FormField::Field() now returns string in many cases rather than DBField instance. API Remove redundant *_val methods from ViewableData API ViewableData::obj() no longer has a $forceReturnObject parameter as it always returns an object BUG Fix issue with ViewableData caching incorrect field values after being modified. API Remove deprecated DB class methods API Enforce plain text left/right formfield titles
107 lines
2.3 KiB
PHP
107 lines
2.3 KiB
PHP
<?php
|
|
/**
|
|
* Lets you wrap a bunch of array data, or object members, into a {@link ViewableData} object.
|
|
*
|
|
* <code>
|
|
* new ArrayData(array(
|
|
* "ClassName" => "Page",
|
|
* "AddAction" => "Add a new Page page",
|
|
* ));
|
|
* </code>
|
|
*
|
|
* @package framework
|
|
* @subpackage view
|
|
*/
|
|
class ArrayData extends ViewableData {
|
|
|
|
/**
|
|
* @var array
|
|
* @see ArrayData::_construct()
|
|
*/
|
|
protected $array;
|
|
|
|
/**
|
|
* @param object|array $value An associative array, or an object with simple properties.
|
|
* Converts object properties to keys of an associative array.
|
|
*/
|
|
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();
|
|
} else {
|
|
$message = 'Parameter to ArrayData constructor needs to be an object or associative array';
|
|
throw new InvalidArgumentException($message);
|
|
}
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Get the source array
|
|
*
|
|
* @return array
|
|
*/
|
|
public function toMap() {
|
|
return $this->array;
|
|
}
|
|
|
|
/**
|
|
* Gets a field from this object.
|
|
*
|
|
* @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.
|
|
*/
|
|
public function getField($f) {
|
|
$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;
|
|
}
|
|
}
|
|
/**
|
|
* Add or set a field on this object.
|
|
*
|
|
* @param string $field
|
|
* @param mixed $value
|
|
* @return $this
|
|
*/
|
|
public function setField($field, $value) {
|
|
$this->array[$field] = $value;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Check array to see if field isset
|
|
*
|
|
* @param string Field Key
|
|
* @return bool
|
|
*/
|
|
public function hasField($f) {
|
|
return isset($this->array[$f]);
|
|
}
|
|
|
|
/**
|
|
* Converts an associative array to a simple object
|
|
*
|
|
* @param array
|
|
* @return stdClass $obj
|
|
*/
|
|
public static function array_to_object($arr = null) {
|
|
$obj = new stdClass();
|
|
if ($arr) foreach($arr as $name => $value) $obj->$name = $value;
|
|
return $obj;
|
|
}
|
|
|
|
}
|