forked from jsebrech/php-o
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectClass.php
More file actions
159 lines (136 loc) · 3.73 KB
/
Copy pathObjectClass.php
File metadata and controls
159 lines (136 loc) · 3.73 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace O;
if (!class_exists("\\O\\ReflectionClass")) include("ReflectionClass.php");
/**
* Supporting class for the o() function
*/
class ObjectClass implements \IteratorAggregate, \ArrayAccess
{
private $o;
function __construct($o) {
if (is_string($o)) $o = json_decode($o);
$this->o = (object) $o;
}
function __toString() {
return json_encode($this->o);
}
function __call($fn, $args) {
if (method_exists($this->o, $fn)) {
return call_user_func_array(array($this->o, $fn), $args);
} else if (isset($this->o->$fn)) {
return call_user_func_array($this->o->$fn, $args);
} else return NULL;
}
function __get($prop) {
return $this->o->$prop;
}
function __set($prop, $value) {
return $this->o->$prop = $value;
}
function __isset($prop) {
return isset($this->o->$prop);
}
function __unset($prop) {
unset($this->o->$prop);
}
function cast($asType = "stdClass") {
if ($asType == "stdClass") {
return $this->o;
} else {
if (!class_exists($asType)) $asType = "O\\".$asType;
if (class_exists($asType)) {
if (is_object($this->o)) {
$a = (array) $this->o;
$refl = new \O\ReflectionClass($asType);
$props = $refl->getProperties(
ReflectionProperty::IS_STATIC|ReflectionProperty::IS_PUBLIC);
$result = new $asType();
// convert properties to the right type
foreach ($props as $prop) {
$propName = $prop->getName();
if (isset($a[$propName])) {
$result->$propName =
convertType($a[$propName], $prop->getType());
};
};
return $result;
} else {
return NULL;
}
} else {
throw new \Exception("Unrecognized type: ".$asType);
}
}
}
function raw() {
return $this->o;
}
function render($template) {
extract((array) $this->o);
/** @noinspection PhpIncludeInspection */
include $template;
}
// IteratorAggregate
function getIterator() {
$o = new \ArrayObject($this->o);
return $o->getIterator();
}
// ArrayAccess
function offsetExists($offset) {
return isset($this->o[$offset]);
}
function offsetGet($offset) {
return $this->o[$offset];
}
function offsetSet($offset, $value) {
$this->o[$offset] = $value;
}
function offsetUnset($offset) {
unset($this->o[$offset]);
}
}
/**
* @param mixed $p
* @return \O\ObjectClass
*/
function o($p) {
if ($p instanceof \O\ObjectClass) {
return $p;
} else {
return new \O\ObjectClass($p);
}
}
// supports types from phplint/phpdoc
// http://www.icosaedro.it/phplint/phpdoc.html#types
function convertType($value, $type) {
if ($value === NULL) return $value;
$type = s($type)->parse_type();
if ($type->isArray) {
if (is_array($value)) {
$newVal = array();
foreach ($value as $key => $item) {
if ($type->key !== NULL) {
$newVal[convertType($key, $type->key)] =
convertType($item, $type->value);
} else {
$newVal[] = convertType($item, $type->value);
};
};
return $newVal;
};
} else {
switch ($type->value) {
case "void": return NULL;
case "bool": case "boolean": return (bool) $value;
case "FALSE": case "false": return FALSE;
case "int": case "integer": return intval($value);
case "float": case "double": return floatval($value);
case "string": return strval($value);
case "mixed": return $value;
case "resource": return is_resource($value) ? $value : NULL;
case "object": return is_object($value) ? $value : o($value)->cast();
default: return o($value)->cast($type->value);
}
};
return NULL;
}