1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Loadsman\LaravelPlugin\Entities; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
7
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
8
|
|
|
use Illuminate\Support\Arr; |
9
|
|
|
use JsonSerializable; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class BaseEntity. |
13
|
|
|
* |
14
|
|
|
* @package Loadsman\Laravel\Entities |
15
|
|
|
*/ |
16
|
|
|
abstract class BaseEntity implements Arrayable, Jsonable, ArrayAccess, JsonSerializable |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Names of attributes that can be filled. |
20
|
|
|
* |
21
|
|
|
* @type array |
22
|
|
|
*/ |
23
|
|
|
protected $fillable = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Entity attributes. |
27
|
|
|
* |
28
|
|
|
* @type array |
29
|
|
|
*/ |
30
|
|
|
protected $attributes = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* BaseEntity constructor. |
34
|
|
|
* |
35
|
|
|
* @param array $data |
36
|
|
|
*/ |
37
|
|
|
public function __construct(array $data = []) |
38
|
|
|
{ |
39
|
|
|
$this->fill($data); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Fill attributes that can be filled. |
44
|
|
|
* |
45
|
|
|
* @param $data |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public function fill(array $data) |
50
|
|
|
{ |
51
|
|
|
$this->attributes = array_merge($this->attributes, $this->filterFillable($data)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the instance as an array. |
56
|
|
|
* |
57
|
|
|
* @return array return array representation of object. |
58
|
|
|
*/ |
59
|
|
|
public function toArray() |
60
|
|
|
{ |
61
|
|
|
return $this->attributes; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Convert the object to its JSON representation. |
66
|
|
|
* |
67
|
|
|
* @param int $options |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function toJson($options = 0) |
72
|
|
|
{ |
73
|
|
|
return json_encode($this->jsonSerialize(), $options); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Return array with with key that can be filled. |
78
|
|
|
* |
79
|
|
|
* @param array $data |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
protected function filterFillable(array $data) |
84
|
|
|
{ |
85
|
|
|
return array_only($data, $this->fillable); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Whether a offset exists. |
90
|
|
|
* |
91
|
|
|
* @param mixed $offset An offset to check for. |
92
|
|
|
* |
93
|
|
|
* @return boolean true on success or false on failure. |
94
|
|
|
*/ |
95
|
|
|
public function offsetExists($offset) |
96
|
|
|
{ |
97
|
|
|
return array_key_exists($offset, $this->attributes); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Offset to retrieve. |
102
|
|
|
* |
103
|
|
|
* @param mixed $offset The offset to retrieve. |
104
|
|
|
* |
105
|
|
|
* @return mixed Can return all value types. |
106
|
|
|
*/ |
107
|
|
|
public function offsetGet($offset) |
108
|
|
|
{ |
109
|
|
|
return $this->attributes[$offset]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Offset to set. |
114
|
|
|
* |
115
|
|
|
* @param mixed $offset The offset to assign the value to. |
116
|
|
|
* @param mixed $value The value to set. |
117
|
|
|
* |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
|
|
public function offsetSet($offset, $value) |
121
|
|
|
{ |
122
|
|
|
$this->attributes[$offset] = $value; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Offset to unset. |
127
|
|
|
* |
128
|
|
|
* @param mixed $offset The offset to unset. |
129
|
|
|
* |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
|
|
public function offsetUnset($offset) |
133
|
|
|
{ |
134
|
|
|
unset($this->attributes[$offset]); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Specify data which should be serialized to JSON |
139
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
140
|
|
|
* @return mixed data which can be serialized by <b>json_encode</b>, |
141
|
|
|
* which is a value of any type other than a resource. |
142
|
|
|
* @since 5.4.0 |
143
|
|
|
*/ |
144
|
|
|
public function jsonSerialize() |
145
|
|
|
{ |
146
|
|
|
return $this->toArray(); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|