-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJB.php
More file actions
145 lines (134 loc) · 3.39 KB
/
Copy pathJB.php
File metadata and controls
145 lines (134 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* @author mlapko
* @version 0.01
*/
class JB
{
/**
* Store current properties
*/
private $_jObject;
/**
* Vars
* @var array
*/
private static $_vars = array();
public function __construct($object = null)
{
$this->_jObject = $object === null ? new stdClass() : $object;
}
/**
* Encode to json
* @param callback $fn
*
* @return string JSON
*/
public static function encode($fn)
{
$json = new self();
$fn($json);
return "$json";
}
/**
*
* @return stdClass
*/
public function getObject()
{
return $this->_jObject;
}
/**
* Set property to current object
* @param stdClass $data
* @param string|array $fields
*/
public function set($data, $fields = array())
{
if (empty($fields)) {
foreach ($this->_getEntityVars($data) as $key => $value) {
$this->_jObject->{$key} = $value;
}
} else {
$fields = is_array($fields) ? $fields : preg_split('/\s*,\s*/', trim($fields), -1, PREG_SPLIT_NO_EMPTY);
foreach ($fields as $fieldName) {
$this->_jObject->{$fieldName} = $data->{$fieldName};
}
}
return $this;
}
/**
* Set var for JB
*
* @param string|array $name
* @param mixed $value
*/
public static function setVar($name, $value = null)
{
if (is_array($name)) {
foreach ($name as $k => $v) {
self::$_vars[$k] = $v;
}
} else {
self::$_vars[$name] = $value;
}
}
/**
* Get setted var
* @param string $name
* @param mixed $default
* @return mixed
*/
public static function getVar($name, $default = null)
{
return isset(self::$_vars[$name]) ? self::$_vars[$name] : $default;
}
public function __set($name, $value)
{
return $this->_jObject->{$name} = $value;
}
public function __call($name, $args)
{
if (count($args) === 1 && is_callable($args[0])) {
$json = new self();
call_user_func($args[0], $json);
$this->_jObject->$name = $json->getObject();
} elseif (count($args) == 2 && is_callable($args[1])) {
$result = array();
foreach ($args[0] as $e) {
$json = new self();
$temp = call_user_func($args[1], $json, $e);
$result[] = isset($temp) ? $temp : $json->getObject();
}
$this->_jObject->{$name} = $result;
} else {
$this->_jObject->{$name} = $args[0];
}
}
/**
* Convert object to string
* @return string
*/
public function __toString()
{
return CJSON::encode($this->_jObject);
}
/**
* Get property
* @param mixed $entity
* @return array
*/
protected function _getEntityVars($entity)
{
$type = gettype($entity);
if ($entity === 'array') {
return $entity;
} elseif ($entity === 'object') {
if ($entity instanceof Traversable) {
return $entity;
}
return get_object_vars($entity);
}
return array();
}
}