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); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return \Nip\Request |
123
|
|
|
*/ |
124
|
|
|
protected function getRequest() |
125
|
|
|
{ |
126
|
|
|
return $this->getManager()->getRequest(); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|