@@ -16,13 +16,13 @@ discard block |
||
16 | 16 | { |
17 | 17 | use SoftDeletes; |
18 | 18 | |
19 | - /** |
|
19 | + /** |
|
20 | 20 | * Os atributos que podem ser atribuíveis em massa. |
21 | 21 | * |
22 | 22 | * @var array |
23 | 23 | */ |
24 | 24 | protected $fillable = [ |
25 | - 'name', 'description' |
|
25 | + 'name', 'description' |
|
26 | 26 | ]; |
27 | 27 | |
28 | 28 | /** |
@@ -31,7 +31,7 @@ discard block |
||
31 | 31 | * @var array |
32 | 32 | */ |
33 | 33 | protected $dates = [ |
34 | - 'deleted_at' |
|
34 | + 'deleted_at' |
|
35 | 35 | ]; |
36 | 36 | |
37 | 37 | /** |
@@ -26,85 +26,85 @@ |
||
26 | 26 | */ |
27 | 27 | trait SchedulerModelTrait |
28 | 28 | { |
29 | - /** |
|
29 | + /** |
|
30 | 30 | * Retorna apenas os horários que possuem o mesmo [model_type] do [parent] dessa [trait]. |
31 | 31 | * |
32 | 32 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
33 | 33 | */ |
34 | - public function schedules() |
|
35 | - { |
|
36 | - return $this->belongsTo(Config::get('scheduler.schedules_table'), 'model_id')->where('model_type', self::class); |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * Agenda um horário para esta model. |
|
41 | - * |
|
42 | - * @param string|Carbon\Carbon $start_at Data em que será agendado, pode ser em string ou em numa classe Carbon. |
|
43 | - * @param string|Carbon\Carbon|int $end_at Data em que acabada esse agendamento, pode ser em string, ou numa classe Carbon |
|
44 | - * ou em int(sendo considerado os minutos de duração). |
|
45 | - * @param int $status Status desse horário ao ser agendado. |
|
46 | - * @return \H4ad\Scheduler\Models\Schedule |
|
47 | - * |
|
48 | - * @throws \H4ad\Scheduler\Exceptions\CantAddWithoutEnd |
|
49 | - * @throws \H4ad\Scheduler\Exceptions\CantAddWithSameStartAt |
|
50 | - * @throws \H4ad\Scheduler\Exceptions\EndCantBeforeStart |
|
51 | - */ |
|
52 | - public function addSchedule($start_at, $end_at = null, $status = null) |
|
53 | - { |
|
54 | - if(!Config::get('scheduler.enable_schedule_without_end') && is_null($end_at)) |
|
55 | - throw new CantAddWithoutEnd; |
|
56 | - |
|
57 | - if(is_string($start_at)) |
|
58 | - $start_at = Carbon::parse($start_at); |
|
59 | - |
|
60 | - if(is_string($end_at)) |
|
61 | - $end_at = Carbon::parse($end_at); |
|
62 | - |
|
63 | - if(is_int($end_at)) |
|
64 | - $end_at = Carbon::parse($start_at->toDateTimeString())->addMinutes($end_at); |
|
65 | - |
|
66 | - if(Config::get('scheduler.enable_schedule_conflict')) |
|
67 | - if(Scheduler::hasScheduleBetween($start_at, $end_at ?? $start_at)) |
|
68 | - throw new CantAddWithSameStartAt; |
|
69 | - |
|
70 | - if($start_at->greaterThan($end_at) && !is_null($end_at)) |
|
71 | - throw new EndCantBeforeStart; |
|
72 | - |
|
73 | - $model_id = $this->getKey(); |
|
74 | - $model_type = self::class; |
|
75 | - |
|
76 | - return Schedule::create(compact('start_at', 'end_at', 'status', 'model_id', 'model_type')); |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Remove um horário agendado pelo seu ID ou pelo horário em que foi marcado. |
|
81 | - * Caso a configuração "enable_schedule_conflict" estiver desabilitada, será lançado uma exceção |
|
82 | - * se for tentado remover um horário agendado pela data de quando foi marcado. |
|
83 | - * |
|
84 | - * @param int|string|Carbon\Carbon $schedule Horário agendado. |
|
85 | - * @return bool|null |
|
86 | - * |
|
87 | - * @throws \H4ad\Scheduler\Exceptions\DoesNotBelong |
|
88 | - * @throws \H4ad\Scheduler\Exceptions\CantRemoveByDate |
|
89 | - * @throws \H4ad\Scheduler\Exceptions\ModelNotFound |
|
90 | - */ |
|
91 | - public function removeSchedule($schedule) |
|
92 | - { |
|
93 | - if(!Config::get('scheduler.enable_schedule_conflict') && !is_int($schedule)) |
|
94 | - throw new CantRemoveByDate; |
|
95 | - |
|
96 | - if(is_int($schedule)) |
|
97 | - $schedule = Schedule::find($schedule); |
|
98 | - |
|
99 | - if(is_string($schedule) || $schedule instanceof Carbon) |
|
100 | - $schedule = Schedule::byStartAt($schedule)->first(); |
|
101 | - |
|
102 | - if(!($schedule instanceof Model)) |
|
103 | - throw (new ModelNotFound)->setValues(Schedule::class); |
|
104 | - |
|
105 | - if($schedule->model_type != self::class) |
|
106 | - throw new DoesNotBelong; |
|
107 | - |
|
108 | - return $schedule->delete(); |
|
109 | - } |
|
34 | + public function schedules() |
|
35 | + { |
|
36 | + return $this->belongsTo(Config::get('scheduler.schedules_table'), 'model_id')->where('model_type', self::class); |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * Agenda um horário para esta model. |
|
41 | + * |
|
42 | + * @param string|Carbon\Carbon $start_at Data em que será agendado, pode ser em string ou em numa classe Carbon. |
|
43 | + * @param string|Carbon\Carbon|int $end_at Data em que acabada esse agendamento, pode ser em string, ou numa classe Carbon |
|
44 | + * ou em int(sendo considerado os minutos de duração). |
|
45 | + * @param int $status Status desse horário ao ser agendado. |
|
46 | + * @return \H4ad\Scheduler\Models\Schedule |
|
47 | + * |
|
48 | + * @throws \H4ad\Scheduler\Exceptions\CantAddWithoutEnd |
|
49 | + * @throws \H4ad\Scheduler\Exceptions\CantAddWithSameStartAt |
|
50 | + * @throws \H4ad\Scheduler\Exceptions\EndCantBeforeStart |
|
51 | + */ |
|
52 | + public function addSchedule($start_at, $end_at = null, $status = null) |
|
53 | + { |
|
54 | + if(!Config::get('scheduler.enable_schedule_without_end') && is_null($end_at)) |
|
55 | + throw new CantAddWithoutEnd; |
|
56 | + |
|
57 | + if(is_string($start_at)) |
|
58 | + $start_at = Carbon::parse($start_at); |
|
59 | + |
|
60 | + if(is_string($end_at)) |
|
61 | + $end_at = Carbon::parse($end_at); |
|
62 | + |
|
63 | + if(is_int($end_at)) |
|
64 | + $end_at = Carbon::parse($start_at->toDateTimeString())->addMinutes($end_at); |
|
65 | + |
|
66 | + if(Config::get('scheduler.enable_schedule_conflict')) |
|
67 | + if(Scheduler::hasScheduleBetween($start_at, $end_at ?? $start_at)) |
|
68 | + throw new CantAddWithSameStartAt; |
|
69 | + |
|
70 | + if($start_at->greaterThan($end_at) && !is_null($end_at)) |
|
71 | + throw new EndCantBeforeStart; |
|
72 | + |
|
73 | + $model_id = $this->getKey(); |
|
74 | + $model_type = self::class; |
|
75 | + |
|
76 | + return Schedule::create(compact('start_at', 'end_at', 'status', 'model_id', 'model_type')); |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Remove um horário agendado pelo seu ID ou pelo horário em que foi marcado. |
|
81 | + * Caso a configuração "enable_schedule_conflict" estiver desabilitada, será lançado uma exceção |
|
82 | + * se for tentado remover um horário agendado pela data de quando foi marcado. |
|
83 | + * |
|
84 | + * @param int|string|Carbon\Carbon $schedule Horário agendado. |
|
85 | + * @return bool|null |
|
86 | + * |
|
87 | + * @throws \H4ad\Scheduler\Exceptions\DoesNotBelong |
|
88 | + * @throws \H4ad\Scheduler\Exceptions\CantRemoveByDate |
|
89 | + * @throws \H4ad\Scheduler\Exceptions\ModelNotFound |
|
90 | + */ |
|
91 | + public function removeSchedule($schedule) |
|
92 | + { |
|
93 | + if(!Config::get('scheduler.enable_schedule_conflict') && !is_int($schedule)) |
|
94 | + throw new CantRemoveByDate; |
|
95 | + |
|
96 | + if(is_int($schedule)) |
|
97 | + $schedule = Schedule::find($schedule); |
|
98 | + |
|
99 | + if(is_string($schedule) || $schedule instanceof Carbon) |
|
100 | + $schedule = Schedule::byStartAt($schedule)->first(); |
|
101 | + |
|
102 | + if(!($schedule instanceof Model)) |
|
103 | + throw (new ModelNotFound)->setValues(Schedule::class); |
|
104 | + |
|
105 | + if($schedule->model_type != self::class) |
|
106 | + throw new DoesNotBelong; |
|
107 | + |
|
108 | + return $schedule->delete(); |
|
109 | + } |
|
110 | 110 | } |
111 | 111 | \ No newline at end of file |