1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Resta\Client;
|
4
|
|
|
|
5
|
|
|
use Resta\Contracts\ApplicationContracts;
|
6
|
|
|
|
7
|
|
|
class ClientAnnotationManager extends ClientAnnotationAbstract
|
8
|
|
|
{
|
9
|
|
|
/**
|
10
|
|
|
* @var array $exceptionParams
|
11
|
|
|
*/
|
12
|
|
|
protected $exceptionParams = [];
|
13
|
|
|
|
14
|
|
|
/**
|
15
|
|
|
* @var string $annotation
|
16
|
|
|
*/
|
17
|
|
|
protected $annotation;
|
18
|
|
|
|
19
|
|
|
/**
|
20
|
|
|
* RequestAnnotationManager constructor.
|
21
|
|
|
* @param ApplicationContracts $app
|
22
|
|
|
* @param $request
|
23
|
|
|
*/
|
24
|
|
|
public function __construct(ApplicationContracts $app,$request)
|
25
|
|
|
{
|
26
|
|
|
parent::__construct($app);
|
27
|
|
|
|
28
|
|
|
$this->setReflection($request);
|
29
|
|
|
|
30
|
|
|
$this->getInputs();
|
31
|
|
|
}
|
32
|
|
|
|
33
|
|
|
/**
|
34
|
|
|
* check annotation
|
35
|
|
|
*
|
36
|
|
|
* @param $method
|
37
|
|
|
* @param $key
|
38
|
|
|
* @return mixed|void
|
39
|
|
|
*/
|
40
|
|
|
public function annotation($method,$key)
|
41
|
|
|
{
|
42
|
|
|
//set annotation value with getting reflection
|
43
|
|
|
$reflection = $this->getReflection('reflection')->reflectionMethodParams($method);
|
44
|
|
|
$this->annotation = $reflection->document;
|
45
|
|
|
|
46
|
|
|
//get remove from request object
|
47
|
|
|
$this->getRemove($key);
|
48
|
|
|
|
49
|
|
|
//get exception values from request object
|
50
|
|
|
$this->getException($key);
|
51
|
|
|
|
52
|
|
|
//get regex from request object
|
53
|
|
|
$this->getRegex($key);
|
54
|
|
|
|
55
|
|
|
//get regex from request object
|
56
|
|
|
$this->getRules($key);
|
57
|
|
|
}
|
58
|
|
|
|
59
|
|
|
/**
|
60
|
|
|
* catch exception from regex method
|
61
|
|
|
*
|
62
|
|
|
* @param string $key
|
63
|
|
|
* @param null|string $data
|
64
|
|
|
*/
|
65
|
|
|
private function catchException($key,$data)
|
66
|
|
|
{
|
67
|
|
|
if(isset($this->exceptionParams[$key])){
|
68
|
|
|
|
69
|
|
|
//get key params for exception params
|
70
|
|
|
$keyParams = ($this->exceptionParams[$key]['params']) ?? [];
|
71
|
|
|
|
72
|
|
|
//catch exception
|
73
|
|
|
exception($this->exceptionParams[$key]['name'],$keyParams)
|
74
|
|
|
->unexpectedValue($key.' input value is not valid as format ('.$data.')');
|
75
|
|
|
}
|
76
|
|
|
else{
|
77
|
|
|
//catch exception
|
78
|
|
|
exception()->unexpectedValue($key.' input value is not valid as format ('.$data.')');
|
79
|
|
|
}
|
80
|
|
|
}
|
81
|
|
|
|
82
|
|
|
/**
|
83
|
|
|
* get request exception from annotation
|
84
|
|
|
*
|
85
|
|
|
* @param $key
|
86
|
|
|
*/
|
87
|
|
|
private function getException($key)
|
88
|
|
|
{
|
89
|
|
|
if(preg_match('@exception\((.*?)\)\r\n@is',$this->annotation,$exception)){
|
90
|
|
|
|
91
|
|
|
$exceptionSpaceExplode = explode(" ",$exception[1]);
|
92
|
|
|
|
93
|
|
|
foreach ($exceptionSpaceExplode as $exceptions){
|
94
|
|
|
$exceptionsDotExplode = explode(":",$exceptions);
|
95
|
|
|
$this->exceptionParams[$key][$exceptionsDotExplode[0]] = $exceptionsDotExplode[1];
|
96
|
|
|
}
|
97
|
|
|
|
98
|
|
|
if(isset($this->exceptionParams[$key]['params'])){
|
99
|
|
|
|
100
|
|
|
$paramsCommaExplode = explode(",",$this->exceptionParams[$key]['params']);
|
101
|
|
|
unset($this->exceptionParams[$key]['params']);
|
102
|
|
|
|
103
|
|
|
foreach ($paramsCommaExplode as $params){
|
104
|
|
|
$paramsEqualExplode = explode("=",$params);
|
105
|
|
|
if(isset($paramsEqualExplode[0]) && isset($paramsEqualExplode[1])){
|
106
|
|
|
$this->exceptionParams[$key]['params'][$paramsEqualExplode[0]] = $paramsEqualExplode[1];
|
107
|
|
|
}
|
108
|
|
|
}
|
109
|
|
|
}
|
110
|
|
|
}
|
111
|
|
|
}
|
112
|
|
|
|
113
|
|
|
/**
|
114
|
|
|
* get regular expression from request object
|
115
|
|
|
*
|
116
|
|
|
* @param $key
|
117
|
|
|
*/
|
118
|
|
|
private function getRegex($key)
|
119
|
|
|
{
|
120
|
|
|
// with the method based regex annotation,
|
121
|
|
|
// we check the rule definition for our requests.
|
122
|
|
|
if(preg_match('@regex\((.*?)\)\r\n@is',$this->annotation,$regex)){
|
123
|
|
|
if(isset($this->inputs[$key])){
|
124
|
|
|
|
125
|
|
|
// for the definition of rules,
|
126
|
|
|
// our inputs can be array and in this case we offer array option for user comfort.
|
127
|
|
|
if(is_array($this->inputs[$key])){
|
128
|
|
|
|
129
|
|
|
foreach ($this->inputs[$key] as $this->inputsKey => $this->inputsValue){
|
130
|
|
|
if(!preg_match('@'.$regex[1].'@is',$this->inputsValue)){
|
131
|
|
|
$this->catchException($key,$regex[1]);
|
132
|
|
|
}
|
133
|
|
|
}
|
134
|
|
|
}
|
135
|
|
|
else{
|
136
|
|
|
|
137
|
|
|
// we control the regex rule that evaluates when only string arrives.
|
138
|
|
|
if(!preg_match('@'.$regex[1].'@is',$this->inputs[$key])){
|
139
|
|
|
$this->catchException($key,$regex[1]);
|
140
|
|
|
}
|
141
|
|
|
}
|
142
|
|
|
}
|
143
|
|
|
}
|
144
|
|
|
}
|
145
|
|
|
|
146
|
|
|
/**
|
147
|
|
|
* get remove regex pattern with request object
|
148
|
|
|
*
|
149
|
|
|
* @param string $key
|
150
|
|
|
* @return void|mixed
|
151
|
|
|
*/
|
152
|
|
|
private function getRemove($key)
|
153
|
|
|
{
|
154
|
|
|
if(preg_match('@remove\((.*?)\)\r\n@is',$this->annotation,$remove)){
|
155
|
|
|
if(isset($this->inputs[$key])){
|
156
|
|
|
if(preg_match('@'.$remove[1].'@is',$this->inputs[$key])){
|
157
|
|
|
unset($this->inputs[$key]);
|
158
|
|
|
}
|
159
|
|
|
}
|
160
|
|
|
}
|
161
|
|
|
}
|
162
|
|
|
|
163
|
|
|
/**
|
164
|
|
|
* check rules from request
|
165
|
|
|
*
|
166
|
|
|
* @param $key
|
167
|
|
|
*/
|
168
|
|
|
private function getRules($key)
|
169
|
|
|
{
|
170
|
|
|
if(preg_match('@rule\((.*?)\)\r\n@is',$this->annotation,$rule)){
|
171
|
|
|
|
172
|
|
|
$requestRules = $this->getReflection('rules');
|
173
|
|
|
|
174
|
|
|
$rules = explode(":",$rule[1]);
|
175
|
|
|
if(isset($this->inputs[$key])){
|
176
|
|
|
foreach($rules as $rule){
|
177
|
|
|
if(isset($requestRules[$rule])){
|
178
|
|
|
if(!preg_match('@'.$requestRules[$rule].'@',$this->inputs[$key])){
|
179
|
|
|
exception($rule,['key'=>$key])
|
180
|
|
|
->invalidArgument($key.' input value is not valid for '.$rule.' request rule');
|
181
|
|
|
}
|
182
|
|
|
}
|
183
|
|
|
}
|
184
|
|
|
}
|
185
|
|
|
}
|
186
|
|
|
}
|
187
|
|
|
} |