Record::updateDataFromRecord()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 6
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
        return $this->attributes;
60
    }
61
62
    /**
63
     * @param $fields
64
     * @param string $glue
65
     * @return string
66
     */
67 1
    public function implodeFields($fields, $glue = '-')
68
    {
69 1
        $return = [];
70 1
        foreach ($fields as $field) {
71 1
            $return[] = $this->{$field};
72
        }
73 1
        return implode($glue, $return);
74
    }
75
76
    /**
77
     * @return mixed
78
     */
79
    public function toApiArray()
80
    {
81
        $data = $this->toArray();
82
        return $data;
83
    }
84
85
    /**
86
     * @return Record
87
     */
88
    public function getClone()
89
    {
90
        $clone = $this->getManager()->getNew();
91
        $clone->updateDataFromRecord($this);
92
93
        return $clone;
94
    }
95
96
    /**
97
     * @param self $record
98
     */
99
    public function updateDataFromRecord($record)
100
    {
101
        $data = $record->toArray();
102
        $cleanup = [$this->getManager()->getPrimaryKey(), 'created'];
103
        foreach ($cleanup as $key) {
104
            unset($data[$key]);
105
        }
106
107
        $this->writeData($data);
108
    }
109
110
    /**
111
     * @return \Nip\Http\Request
112
     */
113
    protected function getRequest()
114
    {
115
        return $this->getManager()->getRequest();
0 ignored issues
show
Deprecated Code introduced by
The function Nip\Records\AbstractMode...rdManager::getRequest() has been deprecated: use request() ( Ignorable by Annotation )

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

115
        return /** @scrutinizer ignore-deprecated */ $this->getManager()->getRequest();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
116
    }
117
}
118