|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace indigerd\scenarios; |
|
4
|
|
|
|
|
5
|
|
|
use indigerd\scenarios\validation\validator\ValidatorInterface; |
|
6
|
|
|
use yii\base\Model; |
|
7
|
|
|
use yii\web\Request; |
|
8
|
|
|
use indigerd\scenarios\exception\ModelValidateException; |
|
9
|
|
|
use indigerd\scenarios\exception\RequestValidateException; |
|
10
|
|
|
use indigerd\scenarios\validation\factory\ValidatorFactory; |
|
11
|
|
|
use indigerd\scenarios\validation\factory\ValidatorCollectionFactory; |
|
12
|
|
|
|
|
13
|
|
|
class Scenario |
|
14
|
|
|
{ |
|
15
|
|
|
protected $validatorFactory; |
|
16
|
|
|
|
|
17
|
|
|
protected $validatorCollectionFactory; |
|
18
|
|
|
|
|
19
|
|
|
protected $attributes = []; |
|
20
|
|
|
|
|
21
|
2 |
|
public function __construct( |
|
22
|
|
|
ValidatorFactory $validatorFactory, |
|
23
|
|
|
ValidatorCollectionFactory $validatorCollectionFactory, |
|
24
|
|
|
array $validationRules = [] |
|
25
|
|
|
) { |
|
26
|
2 |
|
$this->validatorFactory = $validatorFactory; |
|
27
|
2 |
|
$this->validatorCollectionFactory = $validatorCollectionFactory; |
|
28
|
2 |
|
foreach ($validationRules as $rule) { |
|
29
|
|
|
if (!\is_array($rule) or \sizeof($rule) < 2) { |
|
30
|
|
|
throw new \InvalidArgumentException('Invalid rule configuration'); |
|
31
|
|
|
} |
|
32
|
|
|
$this->addValidationRule(...$rule); |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
2 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function addValidationRule($attribute, $validator, array $params = []) : self |
|
37
|
|
|
{ |
|
38
|
|
|
$attributes = \is_array($attribute) ? $attribute : [$attribute]; |
|
39
|
|
|
foreach ($attributes as $attributeName) { |
|
40
|
|
|
if (!($validator instanceof ValidatorInterface)) { |
|
41
|
|
|
$validator = $this->validatorFactory->create($validator, $params); |
|
42
|
|
|
} |
|
43
|
|
|
$this->addValidator($attributeName, $validator); |
|
44
|
|
|
} |
|
45
|
|
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
2 |
|
public function addValidator($attribute, ValidatorInterface $validator) : self |
|
49
|
|
|
{ |
|
50
|
2 |
|
if (!isset($this->attributes[$attribute])) { |
|
51
|
2 |
|
$this->attributes[$attribute] = $this->validatorCollectionFactory->create(); |
|
52
|
|
|
} |
|
53
|
2 |
|
$this->attributes[$attribute]->addValidator($validator); |
|
54
|
2 |
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function validateModel(Model $model) : bool |
|
58
|
|
|
{ |
|
59
|
|
|
$context = $model->toArray(); |
|
60
|
|
|
foreach ($this->attributes as $attribute => $validatorCollection) { |
|
61
|
|
|
if (!$validatorCollection->validate($model->$attribute, $context)) { |
|
62
|
|
|
foreach ($validatorCollection->getMessages() as $message) { |
|
63
|
|
|
$model->addError($attribute, $message); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
if ($model->hasErrors()) { |
|
68
|
|
|
throw new ModelValidateException($model); |
|
69
|
|
|
} |
|
70
|
|
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
2 |
|
public function validateRequest(Request $request, string $variables = 'body') : bool |
|
74
|
|
|
{ |
|
75
|
2 |
|
$errors = []; |
|
76
|
2 |
|
$context = ($variables == 'body' ? $request->getBodyParams() : $request->getQueryParams()); |
|
77
|
2 |
|
foreach ($this->attributes as $attribute => $validatorCollection) { |
|
78
|
2 |
|
$value = isset($context[$attribute]) ? $context[$attribute] : null; |
|
79
|
2 |
|
if (!$validatorCollection->validate($value, $context)) { |
|
80
|
1 |
|
foreach ($validatorCollection->getMessages() as $message) { |
|
81
|
2 |
|
$errors[$attribute][] = $message; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
2 |
|
if (\sizeof($errors) > 0) { |
|
86
|
1 |
|
throw new RequestValidateException($request, $errors); |
|
87
|
|
|
} |
|
88
|
1 |
|
return true; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
This check looks for function calls that miss required arguments.