Test Setup Failed
Push — master ( aeaf6f...22aec1 )
by Php Easy Api
04:36
created

ClientAnnotationManager::annotation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 17
rs 10
c 0
b 0
f 0
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($this->exceptionParams[$key]['name'].' 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
131
                        if(is_array($this->inputsValue)){
132
133
                            foreach ($this->inputsValue as $inputsValueKey => $inputsValueItem){
134
                                if(!preg_match('@'.$regex[1].'@is',$inputsValueItem)){
135
                                    $this->catchException($key,$regex[1]);
136
                                }
137
                            }
138
139
                        }
140
                        else{
141
                            if(!preg_match('@'.$regex[1].'@is',$this->inputsValue)){
142
                                $this->catchException($key,$regex[1]);
143
                            }
144
                        }
145
146
                    }
147
                }
148
                else{
149
150
                    // we control the regex rule that evaluates when only string arrives.
151
                    if(!preg_match('@'.$regex[1].'@is',$this->inputs[$key])){
152
                        $this->catchException($key,$regex[1]);
153
                    }
154
                }
155
            }
156
        }
157
    }
158
159
    /**
160
     * get remove regex pattern with request object
161
     *
162
     * @param string $key
163
     * @return void|mixed
164
     */
165
    private function getRemove($key)
166
    {
167
        if(preg_match('@remove\((.*?)\)\r\n@is',$this->annotation,$remove)){
168
            if(isset($this->inputs[$key])){
169
                if(preg_match('@'.$remove[1].'@is',$this->inputs[$key])){
170
                    unset($this->inputs[$key]);
171
                }
172
            }
173
        }
174
    }
175
176
    /**
177
     * check rules from request
178
     *
179
     * @param $key
180
     */
181
    private function getRules($key)
182
    {
183
        if(preg_match('@rule\((.*?)\)\r\n@is',$this->annotation,$rule)){
184
185
            $requestRules = $this->getReflection('rules');
186
187
            $rules = explode(":",$rule[1]);
188
            if(isset($this->inputs[$key]) && !is_array($this->inputs[$key])){
189
                foreach($rules as $rule){
190
                    if(isset($requestRules[$rule])){
191
                        if(!preg_match('@'.$requestRules[$rule].'@',$this->inputs[$key])){
192
                            exception($rule,['key'=>$key])
193
                                ->invalidArgument($key.' input value is not valid for '.$rule.' request rule');
194
                        }
195
                    }
196
                }
197
            }
198
            else{
199
200
                foreach ($this->inputs[$key] as $key=>$input){
0 ignored issues
show
introduced by
$key is overwriting one of the parameters of this function.
Loading history...
201
202
                    if(!is_array($input)){
203
                        foreach($rules as $rule){
204
                            if(isset($requestRules[$rule])){
205
                                if(!preg_match('@'.$requestRules[$rule].'@',$input)){
206
                                    exception($rule,['key'=>$key])
207
                                        ->invalidArgument($key.' input value is not valid for '.$rule.' request rule');
208
                                }
209
                            }
210
                        }
211
                    }
212
                    else{
213
214
                        foreach ($input as $ikey=>$item){
215
                            foreach($rules as $rule){
216
                                if(isset($requestRules[$rule])){
217
                                    if(!preg_match('@'.$requestRules[$rule].'@',$item)){
218
                                        exception($rule,['key'=>$item])
219
                                            ->invalidArgument($item.' input value is not valid for '.$rule.' request rule');
220
                                    }
221
                                }
222
                            }
223
                        }
224
225
                    }
226
227
                }
228
            }
229
        }
230
    }
231
}