|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File containing the {@see \Mailcode\Traits\Commands\Validation\QueryParamsTrait} trait. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Mailcode |
|
6
|
|
|
* @subpackage Validation |
|
7
|
|
|
* @see \Mailcode\Traits\Commands\Validation\QueryParamsTrait |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Mailcode\Traits\Commands\Validation; |
|
13
|
|
|
|
|
14
|
|
|
use Mailcode\Interfaces\Commands\Validation\QueryParamsInterface; |
|
15
|
|
|
use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Command validation drop-in: checks for the presence |
|
19
|
|
|
* of query parameters specified in separate string |
|
20
|
|
|
* literals, identified by the format <code>"paramName=paramValue"</code>. |
|
21
|
|
|
* |
|
22
|
|
|
* @package Mailcode |
|
23
|
|
|
* @subpackage Validation |
|
24
|
|
|
* @author Sebastian Mordziol <[email protected]> |
|
25
|
|
|
* |
|
26
|
|
|
* @see QueryParamsInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
trait QueryParamsTrait |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var array<string,string> |
|
32
|
|
|
*/ |
|
33
|
|
|
private array $queryParams = array(); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral[] |
|
37
|
|
|
*/ |
|
38
|
|
|
private array $queryParamTokens = array(); |
|
39
|
|
|
|
|
40
|
|
|
protected function validateSyntax_query_params() : void |
|
41
|
|
|
{ |
|
42
|
|
|
$literals = $this->requireParams() |
|
|
|
|
|
|
43
|
|
|
->getInfo() |
|
44
|
|
|
->getStringLiterals(); |
|
45
|
|
|
|
|
46
|
|
|
foreach($literals as $literal) |
|
47
|
|
|
{ |
|
48
|
|
|
$text = $literal->getText(); |
|
49
|
|
|
|
|
50
|
|
|
if(strpos($text, '=') !== false && $this->analyzeQueryString($text)) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->queryParamTokens[] = $literal; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
private function analyzeQueryString(string $param) : bool |
|
58
|
|
|
{ |
|
59
|
|
|
$tokens = explode('=', $param); |
|
60
|
|
|
$name = trim(array_shift($tokens)); |
|
61
|
|
|
$value = trim(implode('=', $tokens)); |
|
62
|
|
|
|
|
63
|
|
|
$this->queryParams[$name] = $value; |
|
64
|
|
|
|
|
65
|
|
|
return true; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function hasQueryParams() : bool |
|
69
|
|
|
{ |
|
70
|
|
|
return !empty($this->queryParams); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return array<string,string> |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getQueryParams() : array |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->queryParams; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral[] |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getQueryParamTokens() : array |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->queryParamTokens; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function getQueryParam(string $name) : string |
|
90
|
|
|
{ |
|
91
|
|
|
if(isset($this->queryParams[$name])) |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->queryParams[$name]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return ''; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function hasQueryParam(string $name) : bool |
|
100
|
|
|
{ |
|
101
|
|
|
return isset($this->queryParams[$name]); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|