1
|
|
|
<?php namespace H4ad\Scheduler\Models; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Esse arquivo faz parte do Scheduler, |
5
|
|
|
* uma biblioteca para auxiliar com agendamentos. |
6
|
|
|
* |
7
|
|
|
* @license MIT |
8
|
|
|
* @package H4ad\Scheduler |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
use Illuminate\Database\Eloquent\Model; |
12
|
|
|
use H4ad\Scheduler\Exceptions\ModelNotFound; |
13
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @property string $model_type |
17
|
|
|
* @property int $model_id |
18
|
|
|
* @property int $status |
19
|
|
|
*/ |
20
|
|
|
class Schedule extends Model |
21
|
|
|
{ |
22
|
|
|
use SoftDeletes; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Os atributos que devem ser mutados para os tipos nátivos. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $casts = [ |
30
|
|
|
'data' => 'array', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Os atributos que podem ser atribuíveis em massa. |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $fillable = [ |
39
|
|
|
'model_type', 'model_id', 'start_at', 'end_at', 'status', 'data' |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Os atributos que devem ser transformados para data. |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $dates = [ |
48
|
|
|
'start_at', 'end_at', 'deleted_at' |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Seta um status para o horário agendado. |
53
|
|
|
* |
54
|
|
|
* @param int|string $status Pode ser passado o ID do status ou seu nome para seta-lo no horário. |
55
|
|
|
*/ |
56
|
|
|
public function setStatus($name) |
57
|
|
|
{ |
58
|
|
|
$this->fill($this->parseStatusKey($name))->save(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Retorna o ID do status caso passem o nome do status. |
63
|
|
|
* |
64
|
|
|
* @param int|string $status ID ou o nome do status. |
65
|
|
|
* @return array |
66
|
|
|
* |
67
|
|
|
* @throws \H4ad\Scheduler\Exceptions\ModelNotFound |
68
|
|
|
*/ |
69
|
|
|
public function parseStatusKey($status) |
70
|
|
|
{ |
71
|
|
|
if(is_int($status)) |
72
|
|
|
$status = ScheduleStatus::find($status); |
73
|
|
|
|
74
|
|
|
if(is_string($status)) |
75
|
|
|
$status = ScheduleStatus::where('name', $status)->first(); |
76
|
|
|
|
77
|
|
|
if(is_null($status)) |
78
|
|
|
throw (new ModelNotFound)->setValues(ScheduleStatus::class); |
79
|
|
|
|
80
|
|
|
return ['status' => $status->id]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Escopo de uma consulta que busca horarios pela data de início. |
85
|
|
|
* |
86
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
87
|
|
|
* @param \Carbon\Carbon|string $start_at |
88
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
89
|
|
|
*/ |
90
|
|
|
public function scopeByStartAt($query, $start_at) |
91
|
|
|
{ |
92
|
|
|
return $query->where('start_at', $start_at); |
93
|
|
|
} |
94
|
|
|
} |