|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* Copyright (C) 2024 Rafael San José <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU General Public License as published by |
|
7
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
8
|
|
|
* any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU General Public License |
|
16
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Alxarafe\Base\Model\Trait; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Trait for controllers using databases. |
|
23
|
|
|
*/ |
|
24
|
|
|
trait DtoTrait |
|
25
|
|
|
{ |
|
26
|
|
|
public function __get($name) |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* TODO: Option to use camel_case and snake_case formats |
|
30
|
|
|
*/ |
|
31
|
|
|
$getter = 'get_' . $name; |
|
32
|
|
|
if (method_exists($this, $getter)) { |
|
33
|
|
|
return $this->$getter(); |
|
34
|
|
|
} |
|
35
|
|
|
$getter = 'get' . ucfirst($name); |
|
36
|
|
|
if (method_exists($this, $getter)) { |
|
37
|
|
|
return $this->$getter(); |
|
38
|
|
|
} |
|
39
|
|
|
return property_exists($this, $name) ? $this->{$name} : null; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function __set($name, $value) |
|
43
|
|
|
{ |
|
44
|
|
|
/** |
|
45
|
|
|
* TODO: Option to use camel_case and snake_case formats |
|
46
|
|
|
*/ |
|
47
|
|
|
$setter = 'set_' . $name; |
|
48
|
|
|
if (method_exists($this, $setter)) { |
|
49
|
|
|
return $this->$setter($value); |
|
50
|
|
|
} |
|
51
|
|
|
/** |
|
52
|
|
|
* TODO: See if it is necessary to use ...$value and give the option to |
|
53
|
|
|
* receive more than one value. |
|
54
|
|
|
*/ |
|
55
|
|
|
$setter = 'set' . ucfirst($name); |
|
56
|
|
|
if (method_exists($this, $setter)) { |
|
57
|
|
|
return $this->$setter($value); |
|
58
|
|
|
} |
|
59
|
|
|
$this->{$name} = $value; |
|
60
|
|
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function __call($name, $arguments) |
|
64
|
|
|
{ |
|
65
|
|
|
/** |
|
66
|
|
|
* TODO: Option to use camel_case and snake_case formats |
|
67
|
|
|
*/ |
|
68
|
|
|
$caller = 'call_' . $name; |
|
69
|
|
|
if (method_exists($this, $caller)) { |
|
70
|
|
|
return $this->$caller($arguments); |
|
71
|
|
|
} |
|
72
|
|
|
$caller = 'call' . ucfirst($name); |
|
73
|
|
|
if (method_exists($this, $caller)) { |
|
74
|
|
|
return $this->$caller($arguments); |
|
75
|
|
|
} |
|
76
|
|
|
return null; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function __isset($name) |
|
80
|
|
|
{ |
|
81
|
|
|
/** |
|
82
|
|
|
* TODO: Option to use camel_case and snake_case formats |
|
83
|
|
|
*/ |
|
84
|
|
|
$getter = 'get_' . $name; |
|
85
|
|
|
if (method_exists($this, $getter)) { |
|
86
|
|
|
return $this->$getter() !== null; |
|
87
|
|
|
} |
|
88
|
|
|
$getter = 'get' . ucfirst($name); |
|
89
|
|
|
if (method_exists($this, $getter)) { |
|
90
|
|
|
return $this->$getter() !== null; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return isset($this->$name); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function getData() |
|
97
|
|
|
{ |
|
98
|
|
|
$record = new static(); |
|
99
|
|
|
foreach (get_class_vars(get_class($record)) as $field => $null) { |
|
100
|
|
|
if (!isset($data->{$field})) { |
|
|
|
|
|
|
101
|
|
|
continue; |
|
102
|
|
|
} |
|
103
|
|
|
$record->{$field} = $data->{$field}; |
|
104
|
|
|
} |
|
105
|
|
|
return $record; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function setData(\stdClass $data) |
|
109
|
|
|
{ |
|
110
|
|
|
foreach ($data as $field => $value) { |
|
111
|
|
|
$this->{$field} = $value; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function toArray() |
|
116
|
|
|
{ |
|
117
|
|
|
return json_decode(json_encode($this), true); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|