Passed
Push — master ( 82c6b4...8a83ac )
by Gabriel
03:56 queued 12s
created

Record::getHelper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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\HasHelpers\HasHelpersRecordTrait;
9
use Nip\Records\Traits\HasManager\HasManagerRecordTrait;
10
use Nip\Utility\Traits\NameWorksTrait;
11
12
/**
13
 * Class Row
14
 * @package Nip\Records\_Abstract
15
 *
16
 * @method \Nip_Helper_Url URL()
17
 */
18
abstract class Record
19
{
20
    use NameWorksTrait;
21
    use ActiveRecordTrait;
22
    use HasHelpersRecordTrait;
23
    use HasManagerRecordTrait;
24
25
    protected $_name = null;
26
27
    protected $_data;
28
29 20
    public function &__get($name)
30
    {
31 20
        if (!$this->__isset($name)) {
32 7
            $this->_data[$name] = null;
33
        }
34
35 20
        return $this->_data[$name];
36
    }
37
38 18
    public function __set($name, $value)
39
    {
40 18
        $this->_data[$name] = $value;
41 18
    }
42
43 20
    public function __isset($name)
44
    {
45 20
        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 ($this->isHelperCall($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
    /**
72
     * @return mixed
73
     */
74
    public function getName()
75
    {
76
        if ($this->_name == null) {
77
            $this->_name = inflector()->unclassify(get_class($this));
78
        }
79
        return $this->_name;
80
    }
81
82
    /**
83
     * @param mixed $name
84
     */
85
    public function setName($name)
86
    {
87
        $this->_name = $name;
88
    }
89
90
    /**
91
     * @return string
92
     */
93
    public function toJSON()
94
    {
95
        return json_encode($this->toArray());
96
    }
97
98
    /**
99
     * @return mixed
100
     */
101
    public function toArray()
102
    {
103
        $vars = get_object_vars($this);
104
        return $vars['_data'];
105
    }
106
107
    /**
108
     * @param $fields
109
     * @param string $glue
110
     * @return string
111
     */
112 1
    public function implodeFields($fields, $glue = '-')
113
    {
114 1
        $return = [];
115 1
        foreach ($fields as $field) {
116 1
            $return[] = $this->{$field};
117
        }
118 1
        return implode($glue, $return);
119
    }
120
121
    /**
122
     * @return mixed
123
     */
124
    public function toApiArray()
125
    {
126
        $data = $this->toArray();
127
        return $data;
128
    }
129
130
    /**
131
     * @return Record
132
     */
133
    public function getCloneWithRelations()
134
    {
135
        $item = $this->getClone();
136
        $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

136
        $item->/** @scrutinizer ignore-call */ 
137
               cloneRelations($this);
Loading history...
137
138
        return $item;
139
    }
140
141
    /**
142
     * @return Record
143
     */
144
    public function getClone()
145
    {
146
        $clone = $this->getManager()->getNew();
147
        $clone->updateDataFromRecord($this);
148
149
        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...
150
151
        return $clone;
152
    }
153
154
    /**
155
     * @param self $record
156
     */
157
    public function updateDataFromRecord($record)
158
    {
159
        $data = $record->toArray();
160
        $this->writeData($data);
161
162
        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...
163
    }
164
165
    /**
166
     * @param bool|array $data
167
     */
168 4
    public function writeData($data = false)
169
    {
170 4
        foreach ($data as $key => $value) {
171 4
            $this->__set($key, $value);
172
        }
173 4
    }
174
175
    /**
176
     * @return \Nip\Request
177
     */
178
    protected function getRequest()
179
    {
180
        return $this->getManager()->getRequest();
181
    }
182
}
183