Commit 29ac3aee by Qiang Xue

Added Jsonable support.

parent b14bb52c
...@@ -611,6 +611,31 @@ class YiiBase ...@@ -611,6 +611,31 @@ class YiiBase
return is_array($params) ? strtr($message, $params) : $message; 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( 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; ...@@ -10,6 +10,7 @@ namespace yii\base;
use ArrayObject; use ArrayObject;
use ArrayIterator; use ArrayIterator;
use yii\helpers\Inflector; use yii\helpers\Inflector;
use yii\helpers\Json;
use yii\validators\RequiredValidator; use yii\validators\RequiredValidator;
use yii\validators\Validator; use yii\validators\Validator;
...@@ -41,7 +42,7 @@ use yii\validators\Validator; ...@@ -41,7 +42,7 @@ use yii\validators\Validator;
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @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 * @event ModelEvent an event raised at the beginning of [[validate()]]. You may set
...@@ -638,6 +639,16 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess ...@@ -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. * Returns an iterator for traversing the attributes in the model.
* This method is required by the interface IteratorAggregate. * This method is required by the interface IteratorAggregate.
* @return ArrayIterator an iterator for traversing the items in the list. * @return ArrayIterator an iterator for traversing the items in the list.
......
...@@ -7,12 +7,15 @@ ...@@ -7,12 +7,15 @@
namespace yii\base; namespace yii\base;
use Yii;
use yii\helpers\Json;
/** /**
* @include @yii/base/Object.md * @include @yii/base/Object.md
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class Object class Object implements Jsonable
{ {
/** /**
* @return string the fully qualified name of this class. * @return string the fully qualified name of this class.
...@@ -38,8 +41,8 @@ class Object ...@@ -38,8 +41,8 @@ class Object
*/ */
public function __construct($config = array()) public function __construct($config = array())
{ {
foreach ($config as $name => $value) { if (!empty($config)) {
$this->$name = $value; Yii::configure($this, $config);
} }
$this->init(); $this->init();
} }
...@@ -216,4 +219,14 @@ class Object ...@@ -216,4 +219,14 @@ class Object
{ {
return method_exists($this, 'set' . $name) || $checkVar && property_exists($this, $name); 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 ...@@ -164,7 +164,6 @@ class AssetController extends Controller
protected function loadConfiguration($configFile) protected function loadConfiguration($configFile)
{ {
echo "Loading configuration from '{$configFile}'...\n"; echo "Loading configuration from '{$configFile}'...\n";
foreach (require($configFile) as $name => $value) { foreach (require($configFile) as $name => $value) {
if (property_exists($this, $name) || $this->canSetProperty($name)) { if (property_exists($this, $name) || $this->canSetProperty($name)) {
$this->$name = $value; $this->$name = $value;
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace yii\helpers\base; namespace yii\helpers\base;
use yii\base\InvalidParamException; use yii\base\InvalidParamException;
use yii\base\Jsonable;
use yii\web\JsExpression; use yii\web\JsExpression;
/** /**
...@@ -90,16 +91,19 @@ class Json ...@@ -90,16 +91,19 @@ class Json
$token = '!{[' . count($expressions) . ']}!'; $token = '!{[' . count($expressions) . ']}!';
$expressions['"' . $token . '"'] = $data->expression; $expressions['"' . $token . '"'] = $data->expression;
return $token; return $token;
} } elseif ($data instanceof Jsonable) {
$result = array(); return $data->toJson();
foreach ($data as $key => $value) { } else {
if (is_array($value) || is_object($value)) { $result = array();
$result[$key] = static::processData($value, $expressions); foreach ($data as $key => $value) {
} else { if (is_array($value) || is_object($value)) {
$result[$key] = $value; $result[$key] = static::processData($value, $expressions);
} else {
$result[$key] = $value;
}
} }
return $result;
} }
return $result;
} else { } else {
return $data; 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