Test Setup Failed
Push — master ( e88459...4f943d )
by Gabriel
09:07
created

Record::__set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Nip\Records\AbstractModels;
4
5
use Nip\HelperBroker;
6
use Nip\Logger\Exception;
7
use Nip\Utility\Traits\NameWorksTrait;
8
9
/**
10
 * Class Row
11
 * @package Nip\Records\_Abstract
12
 *
13
 * @method \Nip_Helper_Url URL()
14
 */
15
abstract class Record
16
{
17
    use NameWorksTrait;
18
19
    protected $_name = null;
20
    protected $_manager = null;
21
22
    /**
23
     * @var string
24
     */
25
    protected $managerName = null;
26
27
    protected $_dbData = [];
28
    protected $_helpers = [];
29
30
31
    protected $_data;
32
33
    public function &__get($name)
34
    {
35
        if (!$this->__isset($name)) {
36
            $this->_data[$name] = null;
37
        }
38
39
        return $this->_data[$name];
40
    }
41
42
    public function __set($name, $value)
43
    {
44
        $this->_data[$name] = $value;
45
    }
46
47
    public function __isset($name)
48
    {
49
        return isset($this->_data[$name]);
50
    }
51
52
    public function __unset($name)
53
    {
54
        unset($this->_data[$name]);
55
    }
56
57
58
    /**
59
     * Overloads Ucfirst() helper
60
     *
61
     * @param string $name
62
     * @param array $arguments
63
     * @return \Nip\Helpers\AbstractHelper|null
64
     */
65
    public function __call($name, $arguments)
66
    {
67
        if ($name === ucfirst($name)) {
68
            return $this->getHelper($name);
69
        }
70
71
        trigger_error("Call to undefined method $name", E_USER_ERROR);
72
        return null;
73
    }
74
75
    /**
76
     * @param string $name
77
     * @return \Nip\Helpers\AbstractHelper
78
     */
79
    public function getHelper($name)
80
    {
81
        return HelperBroker::get($name);
82
    }
83
84
    /**
85
     * @return mixed
86
     */
87
    public function getName()
88
    {
89
        if ($this->_name == null) {
90
            $this->_name = inflector()->unclassify(get_class($this));
91
        }
92
        return $this->_name;
93
    }
94
95
    /**
96
     * @param mixed $name
97
     */
98
    public function setName($name)
99
    {
100
        $this->_name = $name;
101
    }
102
103
    /**
104
     * @param bool|array $data
105
     */
106
    public function writeDBData($data = false)
107
    {
108
        foreach ($data as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $data of type boolean|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
109
            $this->_dbData[$key] = $value;
110
        }
111
    }
112
113
    /**
114
     * @return array
115
     */
116
    public function getDBData()
117
    {
118
        return $this->_dbData;
119
    }
120
121
    /**
122
     * @return mixed
123
     */
124
    public function getPrimaryKey()
125
    {
126
        $pk = $this->getManager()->getPrimaryKey();
127
128
        return $this->{$pk};
129
    }
130
131
    /**
132
     * @return \Nip\Records\RecordManager
133
     */
134
    public function getManager()
135
    {
136
        if ($this->_manager == null) {
137
            $this->initManager();
138
        }
139
140
        return $this->_manager;
141
    }
142
143
    /**
144
     * @param RecordManager $manager
145
     */
146
    public function setManager($manager)
147
    {
148
        $this->_manager = $manager;
149
    }
150
151
    protected function initManager()
152
    {
153
        $class = $this->getManagerName();
154
        $manager = $this->getManagerInstance($class);
155
        $this->setManager($manager);
156
    }
157
158
    /**
159
     * @return string
160
     */
161
    public function getManagerName()
162
    {
163
        if ($this->managerName === null) {
164
            $this->initManagerName();
165
        }
166
167
        return $this->managerName;
168
    }
169
170
    /**
171
     * @param string $managerName
172
     */
173
    public function setManagerName($managerName)
174
    {
175
        $this->managerName = $managerName;
176
    }
177
178
    protected function initManagerName()
179
    {
180
        $this->setManagerName($this->inflectManagerName());
181
    }
182
183
    /**
184
     * @return string
185
     */
186
    protected function inflectManagerName()
187
    {
188
        return ucfirst(inflector()->pluralize($this->getClassName()));
189
    }
190
191
    /**
192
     * @param string $class
193
     * @return RecordManager
194
     * @throws Exception
195
     */
196
    protected function getManagerInstance($class)
197
    {
198
        if (class_exists($class)) {
199
            return call_user_func([$class, 'instance']);
200
        }
201
        throw new Exception('invalid manager name [' . $class . ']');
202
    }
203
204
    /**
205
     * @return bool
206
     */
207
    public function insert()
208
    {
209
        $pk = $this->getManager()->getPrimaryKey();
210
        $lastId = $this->getManager()->insert($this);
211
        if ($pk == 'id') {
212
            $this->{$pk} = $lastId;
213
        }
214
215
        return $lastId > 0;
216
    }
217
218
    /**
219
     * @return bool|\Nip\Database\Result
220
     */
221
    public function update()
222
    {
223
        $return = $this->getManager()->update($this);
224
        return $return;
225
    }
226
227
    public function save()
228
    {
229
        $this->getManager()->save($this);
230
    }
231
232
    public function saveRecord()
233
    {
234
        $this->getManager()->save($this);
235
    }
236
237
    public function delete()
238
    {
239
        $this->getManager()->delete($this);
240
    }
241
242
    /**
243
     * @return bool
244
     */
245
    public function isInDB()
246
    {
247
        $pk = $this->getManager()->getPrimaryKey();
248
        return $this->{$pk} > 0;
249
    }
250
251
    /**
252
     * @return bool|false|Record
253
     */
254
    public function exists()
255
    {
256
        return $this->getManager()->exists($this);
257
    }
258
259
    /**
260
     * @return string
261
     */
262
    public function toJSON()
263
    {
264
        return json_encode($this->toArray());
265
    }
266
267
    /**
268
     * @return mixed
269
     */
270
    public function toArray()
271
    {
272
        $vars = get_object_vars($this);
273
        return $vars['_data'];
274
    }
275
276
    /**
277
     * @return mixed
278
     */
279
    public function toApiArray()
280
    {
281
        $data = $this->toArray();
282
        return $data;
283
    }
284
285
    /**
286
     * @return Record
287
     */
288
    public function getCloneWithRelations()
289
    {
290
        $item = $this->getClone();
291
        $item->cloneRelations($this);
292
293
        return $item;
294
    }
295
296
    /**
297
     * @return Record
298
     */
299
    public function getClone()
300
    {
301
        $clone = $this->getManager()->getNew();
302
        $clone->updateDataFromRecord($this);
303
304
        unset($clone->{$this->getManager()->getPrimaryKey()}, $clone->created);
305
306
        return $clone;
307
    }
308
309
    /**
310
     * @param self $record
311
     */
312
    public function updateDataFromRecord($record)
313
    {
314
        $data = $record->toArray();
315
        $this->writeData($data);
316
317
        unset($this->{$this->getManager()->getPrimaryKey()}, $this->created);
318
    }
319
320
    /**
321
     * @param bool|array $data
322
     */
323
    public function writeData($data = false)
324
    {
325
        foreach ($data as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $data of type boolean|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
326
            $this->__set($key, $value);
327
        }
328
    }
329
330
    /**
331
     * Clone the relations records from a sibling
332
     * @param self $from
333
     * @return \Nip\Records\Traits\Relations\HasRelationsRecordTrait
0 ignored issues
show
Comprehensibility Bug introduced by
The return type \Nip\Records\Traits\Rela...HasRelationsRecordTrait is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
334
     */
335
    public function cloneRelations($from)
336
    {
337
        return $this->getManager()->cloneRelations($from, $this);
338
    }
339
340
    /**
341
     * @return \Nip\Request
342
     */
343
    protected function getRequest()
344
    {
345
        return $this->getManager()->getRequest();
346
    }
347
}
348