Completed
Push — master ( 4ac004...470d43 )
by Gabriel
06:41
created

Record::getPrimaryKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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) {
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...
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);
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);
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);
242
    }
243
244
    /**
245
     * @param bool|array $data
246
     */
247 1
    public function writeData($data = false)
248
    {
249 1
        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...
250 1
            $this->__set($key, $value);
251
        }
252 1
    }
253
254
    /**
255
     * Clone the relations records from a sibling
256
     * @param self $from
257
     * @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...
258
     */
259
    public function cloneRelations($from)
260
    {
261
        return $this->getManager()->cloneRelations($from, $this);
0 ignored issues
show
Documentation Bug introduced by
The method cloneRelations does not exist on object<Nip\Records\AbstractModels\RecordManager>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
262
    }
263
264
    /**
265
     * @return \Nip\Request
266
     */
267
    protected function getRequest()
268
    {
269
        return $this->getManager()->getRequest();
270
    }
271
}
272