Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
29ac3aee
Commit
29ac3aee
authored
Jun 07, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added Jsonable support.
parent
b14bb52c
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
80 additions
and
6 deletions
+80
-6
YiiBase.php
framework/yii/YiiBase.php
+25
-0
Jsonable.php
framework/yii/base/Jsonable.php
+22
-0
Model.php
framework/yii/base/Model.php
+12
-1
Object.php
framework/yii/base/Object.php
+16
-3
AssetController.php
framework/yii/console/controllers/AssetController.php
+0
-1
Json.php
framework/yii/helpers/base/Json.php
+5
-1
No files found.
framework/yii/YiiBase.php
View file @
29ac3aee
...
...
@@ -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
(
...
...
framework/yii/base/Jsonable.php
0 → 100644
View file @
29ac3aee
<?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
();
}
framework/yii/base/Model.php
View file @
29ac3aee
...
...
@@ -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.
...
...
framework/yii/base/Object.php
View file @
29ac3aee
...
...
@@ -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
));
}
}
framework/yii/console/controllers/AssetController.php
View file @
29ac3aee
...
...
@@ -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
;
...
...
framework/yii/helpers/base/Json.php
View file @
29ac3aee
...
...
@@ -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
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment