|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SimpleCrud; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Base class used by Row and RowCollection. |
|
7
|
|
|
* |
|
8
|
|
|
* @property mixed $id |
|
9
|
|
|
*/ |
|
10
|
|
|
abstract class BaseRow implements RowInterface |
|
11
|
|
|
{ |
|
12
|
|
|
protected $entity; |
|
13
|
|
|
protected $methods = []; |
|
14
|
|
|
protected $properties = []; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Constructor. |
|
18
|
|
|
* |
|
19
|
|
|
* @param Entity $entity |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct(Entity $entity) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->entity = $entity; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @see RowInterface |
|
28
|
|
|
* |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
|
|
public function getEntity() |
|
32
|
|
|
{ |
|
33
|
|
|
return $this->entity; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @see RowInterface |
|
38
|
|
|
* |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getAttribute($name) |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->entity->getDb()->getAttribute($name); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @see RowInterface |
|
48
|
|
|
* |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
* |
|
51
|
|
|
* @return self |
|
52
|
|
|
*/ |
|
53
|
|
|
public function registerMethod($name, callable $callable) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->methods[$name] = $callable; |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @see RowInterface |
|
62
|
|
|
* |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
* |
|
65
|
|
|
* @return self |
|
66
|
|
|
*/ |
|
67
|
|
|
public function registerProperty($name, callable $callable) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->properties[$name] = $callable; |
|
70
|
|
|
|
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @see JsonSerializable |
|
76
|
|
|
* |
|
77
|
|
|
* @return array |
|
78
|
|
|
*/ |
|
79
|
|
|
public function jsonSerialize() |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->toArray(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Deletes the row(s) in the database. |
|
86
|
|
|
* |
|
87
|
|
|
* @return self |
|
88
|
|
|
*/ |
|
89
|
|
|
public function delete() |
|
90
|
|
|
{ |
|
91
|
|
|
$id = $this->id; |
|
92
|
|
|
|
|
93
|
|
|
if (!empty($id)) { |
|
94
|
|
|
$this->entity->delete() |
|
95
|
|
|
->byId($id) |
|
96
|
|
|
->run(); |
|
97
|
|
|
|
|
98
|
|
|
$this->id = null; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Magic method to execute custom methods defined in the entity class. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $name |
|
108
|
|
|
*/ |
|
109
|
|
|
public function __call($name, $arguments) |
|
110
|
|
|
{ |
|
111
|
|
|
if (isset($this->methods[$name])) { |
|
112
|
|
|
array_unshift($arguments, $this); |
|
113
|
|
|
|
|
114
|
|
|
return call_user_func_array($this->methods[$name], $arguments); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
//Queries of related entities |
|
118
|
|
|
switch ($this->entity->getRelation($name)) { |
|
119
|
|
|
case Entity::RELATION_HAS_ONE: |
|
120
|
|
|
$entity = $this->entity->getDb()->get($name); |
|
121
|
|
|
return $entity->select()->one()->relatedWith($this); |
|
122
|
|
|
|
|
123
|
|
|
case Entity::RELATION_HAS_MANY: |
|
124
|
|
|
case Entity::RELATION_HAS_BRIDGE: |
|
125
|
|
|
$entity = $this->entity->getDb()->get($name); |
|
126
|
|
|
return $entity->select()->relatedWith($this); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
throw new \BadMethodCallException(sprintf('Call to undefined method %s', $name)); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|