|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SimpleCrud; |
|
4
|
|
|
|
|
5
|
|
|
use JsonSerializable; |
|
6
|
|
|
use SimpleCrud\Scheme\Scheme; |
|
7
|
|
|
use Closure; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Base class used by Row and RowCollection. |
|
11
|
|
|
* |
|
12
|
|
|
* @property mixed $id |
|
13
|
|
|
*/ |
|
14
|
|
|
abstract class AbstractRow implements JsonSerializable |
|
15
|
|
|
{ |
|
16
|
|
|
protected $table; |
|
17
|
|
|
private $methods = []; |
|
18
|
|
|
private $bindMethods = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Constructor. |
|
22
|
|
|
* |
|
23
|
|
|
* @param Table $table |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(Table $table) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->table = $table; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Returns the table associated with this row. |
|
32
|
|
|
* |
|
33
|
|
|
* @return Table |
|
34
|
|
|
*/ |
|
35
|
|
|
public function getTable() |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->table; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Returns the database associated with this row. |
|
42
|
|
|
* |
|
43
|
|
|
* @return SimpleCrud |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getDatabase() |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->getTable()->getDatabase(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @see JsonSerializable |
|
52
|
|
|
* |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
|
|
public function jsonSerialize() |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->toArray(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Magic method to stringify the values. |
|
62
|
|
|
* |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
|
|
public function __toString() |
|
66
|
|
|
{ |
|
67
|
|
|
return json_encode($this, JSON_NUMERIC_CHECK); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Converts this object into an array. |
|
72
|
|
|
* |
|
73
|
|
|
* @param array $bannedEntities |
|
74
|
|
|
* |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
|
|
abstract public function toArray(array $bannedEntities = []); |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Magic method to return properties. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $name |
|
83
|
|
|
* |
|
84
|
|
|
* @return mixed |
|
85
|
|
|
*/ |
|
86
|
|
|
abstract public function __get($name); |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Magic method to edit a property. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $name |
|
92
|
|
|
* @param mixed $value |
|
93
|
|
|
* |
|
94
|
|
|
* @return mixed |
|
95
|
|
|
*/ |
|
96
|
|
|
abstract public function __set($name, $value); |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Deletes the row(s) in the database. |
|
100
|
|
|
* |
|
101
|
|
|
* @return self |
|
102
|
|
|
*/ |
|
103
|
|
|
public function delete() |
|
104
|
|
|
{ |
|
105
|
|
|
$id = $this->id; |
|
106
|
|
|
|
|
107
|
|
|
if (!empty($id)) { |
|
108
|
|
|
$this->getTable()->delete() |
|
|
|
|
|
|
109
|
|
|
->byId($id) |
|
110
|
|
|
->run(); |
|
111
|
|
|
|
|
112
|
|
|
$this->id = null; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Register a custom method. |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $name |
|
122
|
|
|
* @param Closure $method |
|
123
|
|
|
* |
|
124
|
|
|
* @return self |
|
125
|
|
|
*/ |
|
126
|
|
|
public function setMethod($name, Closure $method) |
|
127
|
|
|
{ |
|
128
|
|
|
$this->methods[$name] = $method; |
|
129
|
|
|
unset($this->bindMethods[$name]); |
|
130
|
|
|
|
|
131
|
|
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Magic method to execute queries or custom methods. |
|
136
|
|
|
* |
|
137
|
|
|
* @param string $name |
|
138
|
|
|
*/ |
|
139
|
|
|
public function __call($name, $arguments) |
|
140
|
|
|
{ |
|
141
|
|
|
if (isset($this->methods[$name])) { |
|
142
|
|
|
if (!isset($this->bindMethods[$name])) { |
|
143
|
|
|
$this->bindMethods[$name] = $this->methods[$name]->bindTo($this); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return call_user_func_array($this->bindMethods[$name], $arguments); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$scheme = $this->getTable()->getScheme(); |
|
150
|
|
|
|
|
151
|
|
|
if (!isset($scheme['relations'][$name])) { |
|
152
|
|
|
throw new \BadMethodCallException(sprintf('Call to undefined method %s', $name)); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$table = $this->getTable(); |
|
156
|
|
|
$relation = $table->getScheme()['relations'][$name]; |
|
157
|
|
|
$related = $table->getDatabase()->$name; |
|
158
|
|
|
|
|
159
|
|
|
if ($relation[0] === Scheme::HAS_ONE) { |
|
160
|
|
|
return $related->select()->one()->relatedWith($this); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return $related->select()->relatedWith($this); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: