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