1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/old-town/workflow-zf2-dispatch |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace OldTown\Workflow\ZF2\Dispatch\Validator; |
7
|
|
|
|
8
|
|
|
use Zend\Validator\AbstractValidator; |
9
|
|
|
use OldTown\Workflow\ZF2\Dispatch\Dispatcher\WorkflowDispatchEventInterface; |
10
|
|
|
use Zend\Http\Request; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class HttpMethod |
14
|
|
|
* |
15
|
|
|
* @package OldTown\Workflow\ZF2\Dispatch\Validator |
16
|
|
|
*/ |
17
|
|
|
class HttpMethod extends AbstractValidator |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
const HTTP_METHOD_INVALID = 'httpMethodInvalid'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
const VALIDATE_VALUE_INVALID = 'validateValueInvalid'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
const REQUEST_INVALID = 'requestInvalid'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Validation failure message template definitions |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $messageTemplates = [ |
40
|
|
|
self::HTTP_METHOD_INVALID => 'Http method invalid', |
41
|
|
|
self::VALIDATE_VALUE_INVALID => 'Validate value not implement WorkflowDispatchEventInterface', |
42
|
|
|
self::REQUEST_INVALID => 'The request is not http', |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Настройки валидатора |
47
|
|
|
* |
48
|
|
|
* @var array|null |
49
|
|
|
*/ |
50
|
|
|
protected $allowedHttpMethods; |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param WorkflowDispatchEventInterface $value |
55
|
|
|
* |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
public function isValid($value) |
59
|
|
|
{ |
60
|
|
|
if (!$value instanceof WorkflowDispatchEventInterface) { |
61
|
|
|
$this->error(self::VALIDATE_VALUE_INVALID); |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (null === $this->getAllowedHttpMethods()) { |
66
|
|
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$request = $value->getMvcEvent()->getRequest(); |
70
|
|
|
if (!$request instanceof Request) { |
71
|
|
|
$this->error(self::REQUEST_INVALID); |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** @var Request $request*/ |
76
|
|
|
$method = $request->getMethod(); |
77
|
|
|
$method = strtolower($method); |
78
|
|
|
|
79
|
|
|
$allowedHttpMethods = $this->getAllowedHttpMethods(); |
80
|
|
|
if (!array_key_exists($method, $allowedHttpMethods)) { |
81
|
|
|
$this->error(self::HTTP_METHOD_INVALID); |
82
|
|
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
|
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
public function getAllowedHttpMethods() |
94
|
|
|
{ |
95
|
|
|
return $this->allowedHttpMethods; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param array|null $allowedHttpMethods |
100
|
|
|
* |
101
|
|
|
* @return $this |
102
|
|
|
*/ |
103
|
|
|
public function setAllowedHttpMethods(array $allowedHttpMethods = null) |
104
|
|
|
{ |
105
|
|
|
$results = null; |
106
|
|
|
if (is_array($allowedHttpMethods)) { |
107
|
|
|
$results = array_map(function ($method) { |
108
|
|
|
$method = trim($method); |
109
|
|
|
$method = strtolower($method); |
110
|
|
|
|
111
|
|
|
return $method; |
112
|
|
|
}, $allowedHttpMethods); |
113
|
|
|
|
114
|
|
|
$results = array_combine($results, $results); |
115
|
|
|
} |
116
|
|
|
$this->allowedHttpMethods = $results; |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|