Completed
Push — master ( 8a83ac...f28038 )
by Gabriel
04:28
created

Record::setName()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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\HasAttributes\HasAttributesRecordTrait;
9
use Nip\Records\Traits\HasHelpers\HasHelpersRecordTrait;
10
use Nip\Records\Traits\HasManager\HasManagerRecordTrait;
11
use Nip\Records\Traits\Serializable\SerializableRecord;
12
use Nip\Utility\Traits\NameWorksTrait;
13
14
/**
15
 * Class Row
16
 * @package Nip\Records\_Abstract
17
 *
18
 * @method \Nip_Helper_Url URL()
19
 */
20
abstract class Record implements \Serializable
21
{
22
    use NameWorksTrait;
23
    use ActiveRecordTrait;
24
    use HasHelpersRecordTrait;
25
    use HasManagerRecordTrait;
26
    use HasAttributesRecordTrait;
27
    use SerializableRecord;
28
29
    /**
30
     * Overloads Ucfirst() helper
31
     *
32
     * @param string $name
33
     * @param array $arguments
34
     * @return \Nip\Helpers\AbstractHelper|null
35
     */
36
    public function __call($name, $arguments)
37
    {
38
        if ($this->isHelperCall($name)) {
39
            return $this->getHelper($name);
40
        }
41
42
        trigger_error("Call to undefined method $name", E_USER_ERROR);
43
        return null;
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    public function toJSON()
50
    {
51
        return json_encode($this->toArray());
52
    }
53
54
    /**
55
     * @return mixed
56
     */
57
    public function toArray()
58
    {
59
        $vars = get_object_vars($this);
60
        return $vars['_data'];
61
    }
62
63
    /**
64
     * @param $fields
65
     * @param string $glue
66
     * @return string
67
     */
68 1
    public function implodeFields($fields, $glue = '-')
69
    {
70 1
        $return = [];
71 1
        foreach ($fields as $field) {
72 1
            $return[] = $this->{$field};
73
        }
74 1
        return implode($glue, $return);
75
    }
76
77
    /**
78
     * @return mixed
79
     */
80
    public function toApiArray()
81
    {
82
        $data = $this->toArray();
83
        return $data;
84
    }
85
86
    /**
87
     * @return Record
88
     */
89
    public function getCloneWithRelations()
90
    {
91
        $item = $this->getClone();
92
        $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

92
        $item->/** @scrutinizer ignore-call */ 
93
               cloneRelations($this);
Loading history...
93
94
        return $item;
95
    }
96
97
    /**
98
     * @return Record
99
     */
100
    public function getClone()
101
    {
102
        $clone = $this->getManager()->getNew();
103
        $clone->updateDataFromRecord($this);
104
105
        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...
106
107
        return $clone;
108
    }
109
110
    /**
111
     * @param self $record
112
     */
113
    public function updateDataFromRecord($record)
114
    {
115
        $data = $record->toArray();
116
        $this->writeData($data);
117
118
        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...
119
    }
120
121
    /**
122
     * @return \Nip\Request
123
     */
124
    protected function getRequest()
125
    {
126
        return $this->getManager()->getRequest();
127
    }
128
}
129