-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathValidatingObserver.php
More file actions
85 lines (75 loc) · 2.25 KB
/
ValidatingObserver.php
File metadata and controls
85 lines (75 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace Watson\Validating;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Event;
class ValidatingObserver
{
/**
* Register the validation event for saving the model. Saving validation
* should only occur if creating and updating validation does not.
*
* @return bool
*/
public function saving(Model $model)
{
return $this->performValidation($model, 'saving');
}
/**
* Register the validation event for restoring the model.
*
* @return bool
*/
public function restoring(Model $model)
{
return $this->performValidation($model, 'restoring');
}
/**
* Perform validation with the specified ruleset.
*
* @param string $event
* @return bool
*/
protected function performValidation(Model $model, $event)
{
// If the model has validating enabled, perform it.
if ($model->getValidating()) {
// Fire the namespaced validating event and prevent validation
// if it returns a value.
if ($this->fireValidatingEvent($model, $event) !== null) {
return;
}
if ($model->isValid() === false) {
// Fire the validating failed event.
$this->fireValidatedEvent($model, 'failed');
if ($model->getThrowValidationExceptions()) {
$model->throwValidationException();
}
return false;
}
// Fire the validating.passed event.
$this->fireValidatedEvent($model, 'passed');
} else {
$this->fireValidatedEvent($model, 'skipped');
}
}
/**
* Fire the namespaced validating event.
*
* @param string $event
* @return mixed
*/
protected function fireValidatingEvent(Model $model, $event)
{
return Event::until('eloquent.validating: '.get_class($model), [$model, $event]);
}
/**
* Fire the namespaced post-validation event.
*
* @param string $status
* @return void
*/
protected function fireValidatedEvent(Model $model, $status)
{
Event::dispatch('eloquent.validated: '.get_class($model), [$model, $status]);
}
}