From 29ac3aeea6ace6a1947f0e0226b9d751ed157c5f Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Fri, 7 Jun 2013 11:02:26 -0400 Subject: [PATCH] Added Jsonable support. --- framework/yii/YiiBase.php | 25 ++++++++++++++++++++++ framework/yii/base/Jsonable.php | 22 +++++++++++++++++++ framework/yii/base/Model.php | 13 ++++++++++- framework/yii/base/Object.php | 19 +++++++++++++--- .../yii/console/controllers/AssetController.php | 1 - framework/yii/helpers/base/Json.php | 20 ++++++++++------- 6 files changed, 87 insertions(+), 13 deletions(-) create mode 100644 framework/yii/base/Jsonable.php diff --git a/framework/yii/YiiBase.php b/framework/yii/YiiBase.php index 4fe0e77..6114ced 100644 --- a/framework/yii/YiiBase.php +++ b/framework/yii/YiiBase.php @@ -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( diff --git a/framework/yii/base/Jsonable.php b/framework/yii/base/Jsonable.php new file mode 100644 index 0000000..4a3f7d9 --- /dev/null +++ b/framework/yii/base/Jsonable.php @@ -0,0 +1,22 @@ + + * @since 2.0 + */ +interface Jsonable +{ + /** + * @return string the JSON representation of this object + */ + function toJson(); +} diff --git a/framework/yii/base/Model.php b/framework/yii/base/Model.php index c7432f5..b9b7d2d 100644 --- a/framework/yii/base/Model.php +++ b/framework/yii/base/Model.php @@ -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 * @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. diff --git a/framework/yii/base/Object.php b/framework/yii/base/Object.php index a90a231..79c1f7b 100644 --- a/framework/yii/base/Object.php +++ b/framework/yii/base/Object.php @@ -7,12 +7,15 @@ namespace yii\base; +use Yii; +use yii\helpers\Json; + /** * @include @yii/base/Object.md * @author Qiang Xue * @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)); + } } diff --git a/framework/yii/console/controllers/AssetController.php b/framework/yii/console/controllers/AssetController.php index 4c759de..cd8710e 100644 --- a/framework/yii/console/controllers/AssetController.php +++ b/framework/yii/console/controllers/AssetController.php @@ -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; diff --git a/framework/yii/helpers/base/Json.php b/framework/yii/helpers/base/Json.php index 8de55f9..41d5bf0 100644 --- a/framework/yii/helpers/base/Json.php +++ b/framework/yii/helpers/base/Json.php @@ -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; }