1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Framework\Models\Sql\DataObjects\Result; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Database; |
19
|
|
|
use O2System\Framework\Models\Sql\Model; |
20
|
|
|
use O2System\Spl\DataStructures\SplArrayObject; |
21
|
|
|
use O2System\Spl\Info\SplClassInfo; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Row |
25
|
|
|
* |
26
|
|
|
* @package O2System\Framework\Models\Sql\DataObjects |
27
|
|
|
*/ |
28
|
|
|
class Row extends SplArrayObject |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Row::$model |
32
|
|
|
* |
33
|
|
|
* @var \O2System\Spl\Info\SplClassInfo |
34
|
|
|
*/ |
35
|
|
|
private $model; |
36
|
|
|
|
37
|
|
|
// ------------------------------------------------------------------------ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Row::__construct |
41
|
|
|
* |
42
|
|
|
* @param Database\DataObjects\Result\Row|array $row |
43
|
|
|
* @param Model $model |
44
|
|
|
*/ |
45
|
|
|
public function __construct($row, Model &$model) |
46
|
|
|
{ |
47
|
|
|
$this->model = new SplClassInfo($model); |
48
|
|
|
|
49
|
|
|
if ( ! models()->has($this->model->getClass())) { |
|
|
|
|
50
|
|
|
models()->add($model, $this->model->getClass()); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($row instanceof Database\DataObjects\Result\Row) { |
54
|
|
|
parent::__construct($row->getArrayCopy()); |
55
|
|
|
} elseif (is_array($row)) { |
|
|
|
|
56
|
|
|
parent::__construct($row); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
models($this->model->getClass())->row = $this; |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// ------------------------------------------------------------------------ |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Row::offsetGet |
66
|
|
|
* |
67
|
|
|
* @param string $offset |
68
|
|
|
* |
69
|
|
|
* @return bool|\O2System\Framework\Models\Sql\DataObjects\Result|\O2System\Framework\Models\Sql\DataObjects\Result\Row|null |
70
|
|
|
*/ |
71
|
|
|
public function offsetGet($offset) |
72
|
|
|
{ |
73
|
|
|
if ($this->offsetExists($offset)) { |
74
|
|
|
return parent::offsetGet($offset); |
75
|
|
|
} elseif (null !== ($result = $this->__call($offset))) { |
76
|
|
|
return $result; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return null; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// ------------------------------------------------------------------------ |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Row::__call |
86
|
|
|
* |
87
|
|
|
* @param string $method |
88
|
|
|
* @param array $args |
89
|
|
|
* |
90
|
|
|
* @return bool|\O2System\Framework\Models\Sql\DataObjects\Result|\O2System\Framework\Models\Sql\DataObjects\Result\Row|null |
91
|
|
|
*/ |
92
|
|
|
public function __call($method, $args = []) |
93
|
|
|
{ |
94
|
|
|
$model = models($this->model->getClass()); |
95
|
|
|
|
96
|
|
|
if (method_exists($model, $method)) { |
97
|
|
|
$model->row = $this; |
|
|
|
|
98
|
|
|
|
99
|
|
|
if (false !== ($result = call_user_func_array([&$model, $method], $args))) { |
100
|
|
|
$this->offsetSet($method, $result); |
101
|
|
|
|
102
|
|
|
return $result; |
103
|
|
|
} |
104
|
|
|
} elseif (strpos($method, 'Url')) { |
105
|
|
|
$key = str_replace('Url', '', $method); |
106
|
|
|
|
107
|
|
|
if ($key === $model->uploadedImageKey) { |
|
|
|
|
108
|
|
|
if (isset($model->uploadedImageFilePath)) { |
|
|
|
|
109
|
|
|
if (is_file($filePath = $model->uploadedImageFilePath . $this->offsetGet($key))) { |
110
|
|
|
return images_url($filePath); |
111
|
|
|
} elseif (is_file($filePath = PATH_STORAGE . 'images/default/not-found.jpg')) { |
112
|
|
|
return images_url($filePath); |
113
|
|
|
} elseif (is_file($filePath = PATH_STORAGE . 'images/default/not-found.png')) { |
114
|
|
|
return images_url($filePath); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} elseif (in_array($key, $model->uploadedImageKeys)) { |
|
|
|
|
118
|
|
|
if (isset($model->uploadedImageFilePath)) { |
119
|
|
|
if (is_file($filePath = $model->uploadedImageFilePath . $this->offsetGet($key))) { |
120
|
|
|
return images_url($filePath); |
121
|
|
|
} elseif (is_file($filePath = PATH_STORAGE . 'images/default/not-found.jpg')) { |
122
|
|
|
return images_url($filePath); |
123
|
|
|
} elseif (is_file($filePath = PATH_STORAGE . 'images/default/not-found.png')) { |
124
|
|
|
return images_url($filePath); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} elseif ($key === $model->uploadedFileKey) { |
|
|
|
|
128
|
|
|
if (isset($model->uploadedFileFilepath)) { |
|
|
|
|
129
|
|
|
return storage_url($model->uploadedFileFilepath . $this->offsetGet($key)); |
130
|
|
|
} |
131
|
|
|
} elseif (in_array($key, $model->uploadedFileKeys)) { |
|
|
|
|
132
|
|
|
if (isset($model->uploadedFileFilepath)) { |
133
|
|
|
return storage_url($model->uploadedFileFilepath . $this->offsetGet($key)); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return null; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// ------------------------------------------------------------------------ |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Row::delete |
145
|
|
|
* |
146
|
|
|
* @return bool |
147
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
148
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
149
|
|
|
*/ |
150
|
|
|
public function delete() |
151
|
|
|
{ |
152
|
|
|
$model = models($this->model->getClass()); |
153
|
|
|
|
154
|
|
|
$primaryKey = isset($model->primaryKey) ? $model->primaryKey : 'id'; |
|
|
|
|
155
|
|
|
$id = $this->offsetGet($primaryKey); |
156
|
|
|
|
157
|
|
|
if (method_exists($model, 'beforeDelete')) { |
158
|
|
|
$model->beforeDelete(); |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if ($model->qb->table($model->table)->limit(1)->delete([$primaryKey => $id])) { |
|
|
|
|
162
|
|
|
if (method_exists($model, 'afterDelete')) { |
163
|
|
|
return $model->afterDelete(); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return true; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return false; |
170
|
|
|
} |
171
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.