1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NwLaravel\Entities; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Prettus\Repository\Contracts\Presentable; |
7
|
|
|
use Prettus\Repository\Contracts\PresenterInterface; |
8
|
|
|
use NwLaravel\Repositories\Criterias\InputCriteria; |
9
|
|
|
use Illuminate\Support\Arr; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class AbstractEntity |
13
|
|
|
* |
14
|
|
|
* @abstract |
15
|
|
|
*/ |
16
|
|
|
abstract class AbstractEntity extends Model implements Presentable |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $columns; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* FEITO O PULL REQUEST # ATE SER SOLICITADO |
25
|
|
|
* INICIO: Prettus\Repository\Traits\PresentableTrait |
26
|
|
|
*/ |
27
|
|
|
/** |
28
|
|
|
* @var PresenterInterface |
29
|
|
|
*/ |
30
|
|
|
protected $presenter = null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param \Prettus\Repository\Contracts\PresenterInterface $presenter |
34
|
|
|
* |
35
|
|
|
* @return $this |
36
|
|
|
*/ |
37
|
|
|
public function setPresenter(PresenterInterface $presenter) |
38
|
|
|
{ |
39
|
|
|
$this->presenter = $presenter; |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $key |
46
|
|
|
* @param null $default |
47
|
|
|
* |
48
|
|
|
* @return mixed|null |
49
|
|
|
*/ |
50
|
|
|
public function present($key, $default = null) |
51
|
|
|
{ |
52
|
|
|
if ($this->hasPresenter()) { |
53
|
|
|
$data = $this->presenter()['data']; |
54
|
|
|
|
55
|
|
|
return Arr::get($data, $key, $default); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $default; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
public function hasPresenter() |
65
|
|
|
{ |
66
|
|
|
return isset($this->presenter) && $this->presenter instanceof PresenterInterface; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return $this|mixed |
71
|
|
|
*/ |
72
|
|
|
public function presenter() |
73
|
|
|
{ |
74
|
|
|
if ($this->hasPresenter()) { |
75
|
|
|
return $this->presenter->present($this); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
/** |
81
|
|
|
* FIM Prettus\Repository\Traits\PresentableTrait |
82
|
|
|
*/ |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Set a given attribute on the model. |
86
|
|
|
* |
87
|
|
|
* @param string $key String Key |
88
|
|
|
* @param mixed $value Mixed Value |
89
|
|
|
* |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
14 |
|
public function setAttribute($key, $value) |
93
|
|
|
{ |
94
|
14 |
|
if (is_string($value)) { |
95
|
14 |
|
$value = trim($value); |
96
|
14 |
|
} |
97
|
|
|
|
98
|
14 |
|
if (empty($value) && $value != "0") { |
99
|
1 |
|
$value = null; |
100
|
1 |
|
} |
101
|
|
|
|
102
|
14 |
|
parent::setAttribute($key, $value); |
103
|
14 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get an attribute from the $attributes array. |
107
|
|
|
* |
108
|
|
|
* @param string $key String Key |
109
|
|
|
* |
110
|
|
|
* @return mixed |
111
|
|
|
*/ |
112
|
1 |
|
public function getRawAttribute($key) |
113
|
|
|
{ |
114
|
1 |
|
return $this->getAttributeFromArray($key); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Lista de Colunas |
119
|
|
|
* |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
1 |
|
public function columns() |
123
|
|
|
{ |
124
|
1 |
|
if (is_null($this->columns)) { |
125
|
1 |
|
$table = $this->getTable(); |
126
|
1 |
|
$this->columns = $this->getConnection()->getSchemaBuilder()->getColumnListing($table); |
127
|
1 |
|
$this->columns = array_map('strtolower', $this->columns); |
128
|
1 |
|
} |
129
|
|
|
|
130
|
|
|
// MongoDB |
131
|
1 |
|
if (array_search('_id', $this->columns)===false) { |
132
|
1 |
|
$this->columns[] = '_id'; |
133
|
1 |
|
} |
134
|
|
|
|
135
|
1 |
|
if (array_search('id', $this->columns)===false) { |
136
|
1 |
|
$this->columns[] = 'id'; |
137
|
1 |
|
} |
138
|
|
|
|
139
|
1 |
|
return $this->columns; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Is Column in Table |
144
|
|
|
* |
145
|
|
|
* @param string $key String Key |
146
|
|
|
* |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
1 |
|
public function isColumn($key) |
150
|
|
|
{ |
151
|
1 |
|
return in_array(strtolower($key), $this->columns()); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Retorna o ultimo id registrado |
156
|
|
|
* |
157
|
|
|
* @return int |
158
|
|
|
*/ |
159
|
1 |
|
public function lastInsertId() |
160
|
|
|
{ |
161
|
1 |
|
return $this->getConnection()->getPdo()->lastInsertId(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Convert a DateTime to a storable string. |
166
|
|
|
* |
167
|
|
|
* @param \DateTime|int $value Date Time\Integer Value |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
1 |
|
public function fromDateTime($value) |
171
|
|
|
{ |
172
|
1 |
|
$value = fromDateTime($value); |
173
|
1 |
|
if (empty($value)) { |
174
|
1 |
|
return $value; |
175
|
|
|
} |
176
|
1 |
|
return parent::fromDateTime($value); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Return a timestamp as DateTime object. |
181
|
|
|
* |
182
|
|
|
* @param mixed $value Mixed Value |
183
|
|
|
* @return \Carbon\Carbon |
184
|
|
|
*/ |
185
|
2 |
|
public function asDateTime($value) |
186
|
|
|
{ |
187
|
2 |
|
$value = asDateTime($value); |
188
|
2 |
|
if (empty($value)) { |
189
|
1 |
|
return $value; |
190
|
|
|
} |
191
|
|
|
|
192
|
2 |
|
return parent::asDateTime($value); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Scope Where Criteria |
197
|
|
|
* |
198
|
|
|
* @param unknown $query Query |
199
|
|
|
* @param array $input Input |
200
|
|
|
* |
201
|
|
|
* @return mixed |
202
|
|
|
*/ |
203
|
1 |
|
public function scopeWhereCriteria($query, array $input) |
204
|
|
|
{ |
205
|
1 |
|
$criteria = app(InputCriteria::class, [$input]); |
206
|
1 |
|
return $criteria->apply($query); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|