Completed
Push — master ( a7cd2a...b33b8e )
by Gabriel
08:40
created

Record::getCloneWithRelations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 4
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 getClone()
90
    {
91
        $clone = $this->getManager()->getNew();
92
        $clone->updateDataFromRecord($this);
93
94
        return $clone;
95
    }
96
97
    /**
98
     * @param self $record
99
     */
100
    public function updateDataFromRecord($record)
101
    {
102
        $data = $record->toArray();
103
        $cleanup = [$this->getManager()->getPrimaryKey(), 'created'];
104
        foreach ($cleanup as $key) {
105
            unset($data[$key]);
106
        }
107
108
        $this->writeData($data);
109
    }
110
111
    /**
112
     * @return \Nip\Request
113
     */
114
    protected function getRequest()
115
    {
116
        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

116
        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...
117
    }
118
}
119