Completed
Push — master ( 470d43...3f2648 )
by Gabriel
03:36
created

Record::cloneRelations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Nip\Records\AbstractModels;
4
5
use Nip\HelperBroker;
6
use \Exception;
7
use Nip\Records\Traits\HasManager\HasManagerRecordTrait;
8
use Nip\Utility\Traits\NameWorksTrait;
9
10
/**
11
 * Class Row
12
 * @package Nip\Records\_Abstract
13
 *
14
 * @method \Nip_Helper_Url URL()
15
 */
16
abstract class Record
17
{
18
    use NameWorksTrait;
19
    use HasManagerRecordTrait;
20
21
    protected $_name = null;
22
23
    protected $_dbData = [];
24
    protected $_helpers = [];
25
26
27
    protected $_data;
28
29 11
    public function &__get($name)
30
    {
31 11
        if (!$this->__isset($name)) {
32 5
            $this->_data[$name] = null;
33
        }
34
35 11
        return $this->_data[$name];
36
    }
37
38 10
    public function __set($name, $value)
39
    {
40 10
        $this->_data[$name] = $value;
41 10
    }
42
43 11
    public function __isset($name)
44
    {
45 11
        return isset($this->_data[$name]);
46
    }
47
48
    public function __unset($name)
49
    {
50
        unset($this->_data[$name]);
51
    }
52
53
54
    /**
55
     * Overloads Ucfirst() helper
56
     *
57
     * @param string $name
58
     * @param array $arguments
59
     * @return \Nip\Helpers\AbstractHelper|null
60
     */
61
    public function __call($name, $arguments)
62
    {
63
        if ($name === ucfirst($name)) {
64
            return $this->getHelper($name);
65
        }
66
67
        trigger_error("Call to undefined method $name", E_USER_ERROR);
68
        return null;
69
    }
70
71
    /**
72
     * @param string $name
73
     * @return \Nip\Helpers\AbstractHelper
74
     */
75
    public function getHelper($name)
76
    {
77
        return HelperBroker::get($name);
78
    }
79
80
    /**
81
     * @return mixed
82
     */
83
    public function getName()
84
    {
85
        if ($this->_name == null) {
86
            $this->_name = inflector()->unclassify(get_class($this));
87
        }
88
        return $this->_name;
89
    }
90
91
    /**
92
     * @param mixed $name
93
     */
94
    public function setName($name)
95
    {
96
        $this->_name = $name;
97
    }
98
99
    /**
100
     * @param bool|array $data
101
     */
102
    public function writeDBData($data = false)
103
    {
104
        foreach ($data as $key => $value) {
105
            $this->_dbData[$key] = $value;
106
        }
107
    }
108
109
    /**
110
     * @return array
111
     */
112
    public function getDBData()
113
    {
114
        return $this->_dbData;
115
    }
116
117
    /**
118
     * @return mixed
119
     */
120 5
    public function getPrimaryKey()
121
    {
122 5
        $pk = $this->getManager()->getPrimaryKey();
123
124 5
        return $this->{$pk};
125
    }
126
127
128
    /**
129
     * @return bool
130
     */
131
    public function insert()
132
    {
133
        $pk = $this->getManager()->getPrimaryKey();
134
        $lastId = $this->getManager()->insert($this);
135
        if ($pk == 'id') {
136
            $this->{$pk} = $lastId;
137
        }
138
139
        return $lastId > 0;
140
    }
141
142
    /**
143
     * @return bool|\Nip\Database\Result
144
     */
145
    public function update()
146
    {
147
        $return = $this->getManager()->update($this);
148
        return $return;
149
    }
150
151
    public function save()
152
    {
153
        $this->getManager()->save($this);
154
    }
155
156
    public function saveRecord()
157
    {
158
        $this->getManager()->save($this);
159
    }
160
161
    public function delete()
162
    {
163
        $this->getManager()->delete($this);
164
    }
165
166
    /**
167
     * @return bool
168
     */
169
    public function isInDB()
170
    {
171
        $pk = $this->getManager()->getPrimaryKey();
172
        return $this->{$pk} > 0;
173
    }
174
175
    /**
176
     * @return bool|false|Record
177
     */
178
    public function exists()
179
    {
180
        return $this->getManager()->exists($this);
181
    }
182
183
    /**
184
     * @return string
185
     */
186
    public function toJSON()
187
    {
188
        return json_encode($this->toArray());
189
    }
190
191
    /**
192
     * @return mixed
193
     */
194
    public function toArray()
195
    {
196
        $vars = get_object_vars($this);
197
        return $vars['_data'];
198
    }
199
200
    /**
201
     * @return mixed
202
     */
203
    public function toApiArray()
204
    {
205
        $data = $this->toArray();
206
        return $data;
207
    }
208
209
    /**
210
     * @return Record
211
     */
212
    public function getCloneWithRelations()
213
    {
214
        $item = $this->getClone();
215
        $item->cloneRelations($this);
0 ignored issues
show
Bug introduced by
The method cloneRelations() does not exist on Nip\Records\AbstractModels\Record. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

215
        $item->/** @scrutinizer ignore-call */ 
216
               cloneRelations($this);
Loading history...
216
217
        return $item;
218
    }
219
220
    /**
221
     * @return Record
222
     */
223
    public function getClone()
224
    {
225
        $clone = $this->getManager()->getNew();
226
        $clone->updateDataFromRecord($this);
227
228
        unset($clone->{$this->getManager()->getPrimaryKey()}, $clone->created);
0 ignored issues
show
Bug Best Practice introduced by
The property created does not exist on Nip\Records\AbstractModels\Record. Since you implemented __get, consider adding a @property annotation.
Loading history...
229
230
        return $clone;
231
    }
232
233
    /**
234
     * @param self $record
235
     */
236
    public function updateDataFromRecord($record)
237
    {
238
        $data = $record->toArray();
239
        $this->writeData($data);
240
241
        unset($this->{$this->getManager()->getPrimaryKey()}, $this->created);
0 ignored issues
show
Bug Best Practice introduced by
The property created does not exist on Nip\Records\AbstractModels\Record. Since you implemented __get, consider adding a @property annotation.
Loading history...
242
    }
243
244
    /**
245
     * @param bool|array $data
246
     */
247 1
    public function writeData($data = false)
248
    {
249 1
        foreach ($data as $key => $value) {
250 1
            $this->__set($key, $value);
251
        }
252 1
    }
253
254
    /**
255
     * @return \Nip\Request
256
     */
257
    protected function getRequest()
258
    {
259
        return $this->getManager()->getRequest();
260
    }
261
}
262