mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 06:15:19 +08:00
Added Jsonable support.
This commit is contained in:
@ -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(
|
||||
|
22
framework/yii/base/Jsonable.php
Normal file
22
framework/yii/base/Jsonable.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?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
|
||||
@ -637,6 +638,16 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
@ -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,7 +91,9 @@ class Json
|
||||
$token = '!{[' . count($expressions) . ']}!';
|
||||
$expressions['"' . $token . '"'] = $data->expression;
|
||||
return $token;
|
||||
}
|
||||
} elseif ($data instanceof Jsonable) {
|
||||
return $data->toJson();
|
||||
} else {
|
||||
$result = array();
|
||||
foreach ($data as $key => $value) {
|
||||
if (is_array($value) || is_object($value)) {
|
||||
@ -100,6 +103,7 @@ class Json
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
} else {
|
||||
return $data;
|
||||
}
|
||||
|
Reference in New Issue
Block a user