1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Records\AbstractModels; |
4
|
|
|
|
5
|
|
|
use Nip\HelperBroker; |
6
|
|
|
use \Exception; |
7
|
|
|
use Nip\Records\Traits\ActiveRecord\ActiveRecordTrait; |
8
|
|
|
use Nip\Records\Traits\HasManager\HasManagerRecordTrait; |
9
|
|
|
use Nip\Utility\Traits\NameWorksTrait; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Row |
13
|
|
|
* @package Nip\Records\_Abstract |
14
|
|
|
* |
15
|
|
|
* @method \Nip_Helper_Url URL() |
16
|
|
|
*/ |
17
|
|
|
abstract class Record |
18
|
|
|
{ |
19
|
|
|
use NameWorksTrait; |
20
|
|
|
use ActiveRecordTrait; |
21
|
|
|
use HasManagerRecordTrait; |
22
|
|
|
|
23
|
|
|
protected $_name = null; |
24
|
|
|
|
25
|
|
|
protected $_helpers = []; |
26
|
|
|
|
27
|
|
|
protected $_data; |
28
|
|
|
|
29
|
17 |
|
public function &__get($name) |
30
|
|
|
{ |
31
|
17 |
|
if (!$this->__isset($name)) { |
32
|
5 |
|
$this->_data[$name] = null; |
33
|
|
|
} |
34
|
|
|
|
35
|
17 |
|
return $this->_data[$name]; |
36
|
|
|
} |
37
|
|
|
|
38
|
16 |
|
public function __set($name, $value) |
39
|
|
|
{ |
40
|
16 |
|
$this->_data[$name] = $value; |
41
|
16 |
|
} |
42
|
|
|
|
43
|
17 |
|
public function __isset($name) |
44
|
|
|
{ |
45
|
17 |
|
return isset($this->_data[$name]); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function __unset($name) |
49
|
|
|
{ |
50
|
|
|
unset($this->_data[$name]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Overloads Ucfirst() helper |
55
|
|
|
* |
56
|
|
|
* @param string $name |
57
|
|
|
* @param array $arguments |
58
|
|
|
* @return \Nip\Helpers\AbstractHelper|null |
59
|
|
|
*/ |
60
|
|
|
public function __call($name, $arguments) |
61
|
|
|
{ |
62
|
|
|
if ($name === ucfirst($name)) { |
63
|
|
|
return $this->getHelper($name); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
trigger_error("Call to undefined method $name", E_USER_ERROR); |
67
|
|
|
return null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $name |
72
|
|
|
* @return \Nip\Helpers\AbstractHelper |
73
|
|
|
*/ |
74
|
|
|
public function getHelper($name) |
75
|
|
|
{ |
76
|
|
|
return HelperBroker::get($name); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
|
|
public function getName() |
83
|
|
|
{ |
84
|
|
|
if ($this->_name == null) { |
85
|
|
|
$this->_name = inflector()->unclassify(get_class($this)); |
86
|
|
|
} |
87
|
|
|
return $this->_name; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param mixed $name |
92
|
|
|
*/ |
93
|
|
|
public function setName($name) |
94
|
|
|
{ |
95
|
|
|
$this->_name = $name; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function toJSON() |
102
|
|
|
{ |
103
|
|
|
return json_encode($this->toArray()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
|
|
public function toArray() |
110
|
|
|
{ |
111
|
|
|
$vars = get_object_vars($this); |
112
|
|
|
return $vars['_data']; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param $fields |
117
|
|
|
* @param string $glue |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
1 |
|
public function implodeFields($fields, $glue = '-') |
121
|
|
|
{ |
122
|
1 |
|
$return = []; |
123
|
1 |
|
foreach ($fields as $field) { |
124
|
1 |
|
$return[] = $this->{$field}; |
125
|
|
|
} |
126
|
1 |
|
return implode($glue, $return); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return mixed |
131
|
|
|
*/ |
132
|
|
|
public function toApiArray() |
133
|
|
|
{ |
134
|
|
|
$data = $this->toArray(); |
135
|
|
|
return $data; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return Record |
140
|
|
|
*/ |
141
|
|
|
public function getCloneWithRelations() |
142
|
|
|
{ |
143
|
|
|
$item = $this->getClone(); |
144
|
|
|
$item->cloneRelations($this); |
|
|
|
|
145
|
|
|
|
146
|
|
|
return $item; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return Record |
151
|
|
|
*/ |
152
|
|
|
public function getClone() |
153
|
|
|
{ |
154
|
|
|
$clone = $this->getManager()->getNew(); |
155
|
|
|
$clone->updateDataFromRecord($this); |
156
|
|
|
|
157
|
|
|
unset($clone->{$this->getManager()->getPrimaryKey()}, $clone->created); |
|
|
|
|
158
|
|
|
|
159
|
|
|
return $clone; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param self $record |
164
|
|
|
*/ |
165
|
|
|
public function updateDataFromRecord($record) |
166
|
|
|
{ |
167
|
|
|
$data = $record->toArray(); |
168
|
|
|
$this->writeData($data); |
169
|
|
|
|
170
|
|
|
unset($this->{$this->getManager()->getPrimaryKey()}, $this->created); |
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param bool|array $data |
175
|
|
|
*/ |
176
|
4 |
|
public function writeData($data = false) |
177
|
|
|
{ |
178
|
4 |
|
foreach ($data as $key => $value) { |
179
|
4 |
|
$this->__set($key, $value); |
180
|
|
|
} |
181
|
4 |
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return \Nip\Request |
185
|
|
|
*/ |
186
|
|
|
protected function getRequest() |
187
|
|
|
{ |
188
|
|
|
return $this->getManager()->getRequest(); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|