forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntity.php
More file actions
188 lines (163 loc) · 4.25 KB
/
Copy pathEntity.php
File metadata and controls
188 lines (163 loc) · 4.25 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php namespace CodeIgniter;
class Entity
{
/**
* Maps names used in sets and gets against unique
* names within the class, allowing independence from
* database column names.
*
* Example:
* $datamap = [
* 'db_name' => 'class_name'
* ];
*
* @var array
*/
protected $datamap = [];
/**
* Takes an array of key/value pairs and sets them as
* class properties, using any `setCamelCasedProperty()` methods
* that may or may not exist.
*
* @param array $data
*/
public function fill(array $data)
{
foreach ($data as $key => $value)
{
$method = 'set'.str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $key)));
if (method_exists($this, $method))
{
$this->$method($value);
}
elseif (property_exists($this, $key))
{
$this->$key = $value;
}
}
}
//--------------------------------------------------------------------
/**
* Magic method to allow retrieval of protected and private
* class properties either by their name, or through a `getCamelCasedProperty()`
* method.
*
* Examples:
*
* $p = $this->my_property
* $p = $this->getMyProperty()
*
* @param string $key
*
* @return mixed
*/
public function __get(string $key)
{
$key = $this->mapProperty($key);
// Convert to CamelCase for the method
$method = 'get'.str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $key)));
// if a set* method exists for this key,
// use that method to insert this value.
if (method_exists($this, $method))
{
return $this->$method();
}
// Otherwise return the protected property
// if it exists.
if (property_exists($this, $key))
{
return $this->$key;
}
}
//--------------------------------------------------------------------
/**
* Magic method to all protected/private class properties to be easily set,
* either through a direct access or a `setCamelCasedProperty()` method.
*
* Examples:
*
* $this->my_property = $p;
* $this->setMyProperty() = $p;
*
* @param string $key
* @param null $value
*
* @return $this
*/
public function __set(string $key, $value = null)
{
$key = $this->mapProperty($key);
// if a set* method exists for this key,
// use that method to insert this value.
$method = 'set'.str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $key)));
if (method_exists($this, $method))
{
$this->$method($value);
return $this;
}
elseif (property_exists($this, $key))
{
$this->$key = $value;
return $this;
}
}
//--------------------------------------------------------------------
/**
* Unsets a protected/private class property. Sets the value to null.
* However, if there was a default value for the parent class, this
* attribute will be reset to that default value.
*
* @param string $key
*/
public function __unset(string $key)
{
// If not actual property exists, get out
// before we confuse our data mapping.
if (! property_exists($this, $key)) return;
$this->$key = null;
// Get the class' original default value for this property
// so we can reset it to the original value.
$reflectionClass = new \ReflectionClass($this);
$defaultProperties = $reflectionClass->getDefaultProperties();
if (isset($defaultProperties[$key]))
{
$this->$key = $defaultProperties[$key];
}
}
//--------------------------------------------------------------------
/**
* Returns true if a property exists names $key, or a getter method
* exists named like for __get().
*
* @param string $key
*
* @return bool
*/
public function __isset(string $key): bool
{
// Ensure an actual property exists, otherwise
// we confuse the data mapping.
$value = property_exists($this, $key)
? $this->$key
: null;
return ! is_null($value);
}
//--------------------------------------------------------------------
/**
* Checks the datamap to see if this column name is being mapped,
* and returns the mapped name, if any, or the original name.
*
* @param string $key
*
* @return mixed|string
*/
protected function mapProperty(string $key)
{
if (array_key_exists($key, $this->datamap))
{
return $this->datamap[$key];
}
return $key;
}
//--------------------------------------------------------------------
}