Commit 29ac3aee by Qiang Xue

Added Jsonable support.

parent b14bb52c
......@@ -611,6 +611,31 @@ class YiiBase
return is_array($params) ? strtr($message, $params) : $message;
}
}
/**
* Configures an object with the initial property values.
* @param object $object the object to be configured
* @param array $properties the property initial values given in terms of name-value pairs.
*/
public static function configure($object, $properties)
{
foreach ($properties as $name => $value) {
$object->$name = $value;
}
}
/**
* Returns the public member variables of an object.
* This method is provided such that we can get the public member variables of an object.
* It is different from "get_object_vars()" because the latter will return private
* and protected variables if it is called within the object itself.
* @param object $object the object to be handled
* @return array the public member variables of the object
*/
public static function getObjectVars($object)
{
return get_object_vars($object);
}
}
YiiBase::$aliases = array(
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
/**
* Jsonable should be implemented by classes that need to be represented in JSON format.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
interface Jsonable
{
/**
* @return string the JSON representation of this object
*/
function toJson();
}
......@@ -10,6 +10,7 @@ namespace yii\base;
use ArrayObject;
use ArrayIterator;
use yii\helpers\Inflector;
use yii\helpers\Json;
use yii\validators\RequiredValidator;
use yii\validators\Validator;
......@@ -41,7 +42,7 @@ use yii\validators\Validator;
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Model extends Component implements \IteratorAggregate, \ArrayAccess
class Model extends Component implements \IteratorAggregate, \ArrayAccess, Jsonable
{
/**
* @event ModelEvent an event raised at the beginning of [[validate()]]. You may set
......@@ -638,6 +639,16 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
}
/**
* Returns the JSON representation of this object.
* The default implementation will return [[attributes]].
* @return string the JSON representation of this object.
*/
public function toJson()
{
return Json::encode($this->getAttributes());
}
/**
* Returns an iterator for traversing the attributes in the model.
* This method is required by the interface IteratorAggregate.
* @return ArrayIterator an iterator for traversing the items in the list.
......
......@@ -7,12 +7,15 @@
namespace yii\base;
use Yii;
use yii\helpers\Json;
/**
* @include @yii/base/Object.md
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Object
class Object implements Jsonable
{
/**
* @return string the fully qualified name of this class.
......@@ -38,8 +41,8 @@ class Object
*/
public function __construct($config = array())
{
foreach ($config as $name => $value) {
$this->$name = $value;
if (!empty($config)) {
Yii::configure($this, $config);
}
$this->init();
}
......@@ -216,4 +219,14 @@ class Object
{
return method_exists($this, 'set' . $name) || $checkVar && property_exists($this, $name);
}
/**
* Returns the JSON representation of this object.
* The default implementation will return all public member variables.
* @return string the JSON representation of this object.
*/
public function toJson()
{
return Json::encode(Yii::getObjectVars($this));
}
}
......@@ -164,7 +164,6 @@ class AssetController extends Controller
protected function loadConfiguration($configFile)
{
echo "Loading configuration from '{$configFile}'...\n";
foreach (require($configFile) as $name => $value) {
if (property_exists($this, $name) || $this->canSetProperty($name)) {
$this->$name = $value;
......
......@@ -8,6 +8,7 @@
namespace yii\helpers\base;
use yii\base\InvalidParamException;
use yii\base\Jsonable;
use yii\web\JsExpression;
/**
......@@ -90,16 +91,19 @@ class Json
$token = '!{[' . count($expressions) . ']}!';
$expressions['"' . $token . '"'] = $data->expression;
return $token;
}
$result = array();
foreach ($data as $key => $value) {
if (is_array($value) || is_object($value)) {
$result[$key] = static::processData($value, $expressions);
} else {
$result[$key] = $value;
} elseif ($data instanceof Jsonable) {
return $data->toJson();
} else {
$result = array();
foreach ($data as $key => $value) {
if (is_array($value) || is_object($value)) {
$result[$key] = static::processData($value, $expressions);
} else {
$result[$key] = $value;
}
}
return $result;
}
return $result;
} else {
return $data;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment