-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathModel.php
139 lines (130 loc) · 4.09 KB
/
Model.php
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
<?php
/**
* File defining \Base\Model
*
* PHP Version 5.3
*
* @category Backend
* @package Base
* @author J Jurgens du Toit <jrgns@jrgns.net>
* @copyright 2011 - 2012 Jade IT (cc)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @link http://backend-php.net
*/
namespace Backend\Base;
/**
* The main Model class.
*
* Normal / bindable properties should NOT start with an underscore. Meta properties should.
*
* @category Backend
* @package Base
* @author J Jurgens du Toit <jrgns@jrgns.net>
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @link http://backend-php.net
*/
class Model implements \Backend\Interfaces\ModelInterface
{
/**
* Magic __get function
*
* @param string $name The name of the property being retrieved
*
* @return mixed The value of the property
*/
public function __get($name)
{
$funcName = new Utilities\String($name);
$funcName = 'get' . $funcName->camelCase();
if (method_exists($this, $funcName)) {
return $this->$funcName();
} elseif (property_exists($this, $name)) {
return $this->$name;
}
throw new \ErrorException('Undefined property: ' . get_class($this) . '::$' . $name);
}
/**
* Magic __set function
*
* @param string $name The name of the property being set
* @param mixed $value The value of the property being set
*
* @return Model The current Model
*/
public function __set($name, $value)
{
$funcName = new Utilities\String($name);
$funcName = 'set' . $funcName->camelCase();
if (method_exists($this, $funcName)) {
$this->$funcName($value);
} elseif (property_exists($this, $name)) {
$this->$name = $value;
} else {
throw new \ErrorException(
'Trying to set Undefined property: ' . get_class($this) . '::$' . $name
);
}
return $this;
}
/**
* Populate the Model with the specified properties.
*
* The function will use any `set` functions defined.
*
* @param array $properties An array containing the properties for the model
*
* @return Object The object that was populated
*/
public function populate(array $properties)
{
foreach ($properties as $name => $value) {
$funcName = new Utilities\String($name);
$funcName = 'set' . $funcName->camelCase();
if (is_callable(array($this, $funcName))) {
$this->$funcName($value);
} elseif (property_exists($this, $name)) {
$this->$name = $value;
} elseif ($name[0] !== '_') {
throw new \ErrorException(
'Undefined property: ' . __CLASS__ . '::$' . $name
);
}
}
return $this;
}
/**
* Get the properties of the Model
*
* @return array The properties of the model as a key / value array
*/
public function getProperties()
{
$result = array();
$reflector = new \ReflectionClass($this);
do {
$properties = $reflector->getProperties();
foreach ($properties as $property) {
$name = $property->getName();
if ($property->isStatic() || substr($name, 0, 1) == '_' || array_key_exists($name, $result)) {
continue;
}
$result[$property->getName()] = $this->{$property->getName()};
}
$reflector = $reflector->getParentClass();
} while (empty($reflector) === false && $reflector->name != 'Backend\Base\Model');
return $result;
}
/**
* Convert the Model to JSON.
*
* @return string The model as a JSON string.
*/
public function toJson()
{
$json = json_encode($this->getProperties());
if ($error = json_last_error()) {
throw new \RuntimeException('Json Encoding Error: ' . $error);
}
return $json;
}
}