1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the trait {@see \Mailcode\Traits\Commands\EncodableTrait}. |
4
|
|
|
* |
5
|
|
|
* @package Mailcode |
6
|
|
|
* @subpackage Commands |
7
|
|
|
* @see \Mailcode\Traits\Commands\EncodableTrait |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Mailcode\Traits\Commands; |
13
|
|
|
|
14
|
|
|
use Mailcode\Interfaces\Commands\EncodableInterface; |
15
|
|
|
use Mailcode\Interfaces\Commands\Validation\IDNDecodeInterface; |
16
|
|
|
use Mailcode\Interfaces\Commands\Validation\IDNEncodeInterface; |
17
|
|
|
use Mailcode\Mailcode_Commands_Keywords; |
18
|
|
|
use Mailcode\Mailcode_Exception; |
19
|
|
|
use Mailcode\Mailcode_Interfaces_Commands_Validation_URLDecode; |
20
|
|
|
use Mailcode\Mailcode_Interfaces_Commands_Validation_URLEncode; |
21
|
|
|
use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token; |
22
|
|
|
use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token_Keyword; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Trait used to implement the encodable interface. |
26
|
|
|
* |
27
|
|
|
* @package Mailcode |
28
|
|
|
* @subpackage Commands |
29
|
|
|
* @author Sebastian Mordziol <[email protected]> |
30
|
|
|
* |
31
|
|
|
* @see EncodableInterface |
32
|
|
|
*/ |
33
|
|
|
trait EncodableTrait |
34
|
|
|
{ |
35
|
|
|
public function supportsEncoding(string $keyword) : bool |
36
|
|
|
{ |
37
|
|
|
return in_array($keyword, $this->getSupportedEncodings(), true); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function isEncodingEnabled(string $keyword) : bool |
41
|
|
|
{ |
42
|
|
|
return $this->requireParams() |
|
|
|
|
43
|
|
|
->getInfo() |
44
|
|
|
->hasKeyword($keyword); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $keyword |
49
|
|
|
* @param bool $enabled |
50
|
|
|
* @return $this |
51
|
|
|
* @throws Mailcode_Exception |
52
|
|
|
*/ |
53
|
|
|
public function setEncodingEnabled(string $keyword, bool $enabled) : self |
54
|
|
|
{ |
55
|
|
|
if(!$this->supportsEncoding($keyword)) |
56
|
|
|
{ |
57
|
|
|
throw new Mailcode_Exception( |
58
|
|
|
'Cannot set encoding status, command does not support target encoding.', |
59
|
|
|
sprintf( |
60
|
|
|
'Tried setting the encoding [%s], but the command only supports the following encodings: [%s].', |
61
|
|
|
$keyword, |
62
|
|
|
implode(', ', $this->getSupportedEncodings()) |
63
|
|
|
), |
64
|
|
|
EncodableInterface::ERROR_UNSUPPORTED_ENCODING |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->requireParams() |
69
|
|
|
->getInfo() |
70
|
|
|
->setKeywordEnabled($keyword, $enabled); |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getActiveEncodings() : array |
76
|
|
|
{ |
77
|
|
|
$keywords = $this->requireParams() |
78
|
|
|
->getInfo() |
79
|
|
|
->getKeywords(); |
80
|
|
|
|
81
|
|
|
$result = array(); |
82
|
|
|
|
83
|
|
|
// We are using the keywords list from the command's parameters |
84
|
|
|
// here, so they are in the exact order in which they were specified. |
85
|
|
|
foreach ($keywords as $keyword) |
86
|
|
|
{ |
87
|
|
|
$name = $keyword->getKeyword(); |
88
|
|
|
|
89
|
|
|
if($this->supportsEncoding($name) && $this->isEncodingEnabled($name)) |
90
|
|
|
{ |
91
|
|
|
$result[] = $name; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $result; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function getSupportedEncodings() : array |
99
|
|
|
{ |
100
|
|
|
$encodings = array(); |
101
|
|
|
|
102
|
|
|
if($this instanceof IDNEncodeInterface) |
103
|
|
|
{ |
104
|
|
|
$encodings[] = Mailcode_Commands_Keywords::TYPE_IDN_ENCODE; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if($this instanceof IDNDecodeInterface) |
108
|
|
|
{ |
109
|
|
|
$encodings[] = Mailcode_Commands_Keywords::TYPE_IDN_DECODE; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if($this instanceof Mailcode_Interfaces_Commands_Validation_URLEncode) |
113
|
|
|
{ |
114
|
|
|
$encodings[] = Mailcode_Commands_Keywords::TYPE_URLENCODE; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if($this instanceof Mailcode_Interfaces_Commands_Validation_URLDecode) |
118
|
|
|
{ |
119
|
|
|
$encodings[] = Mailcode_Commands_Keywords::TYPE_URLDECODE; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $encodings; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function getEncodingToken(string $keyword) : ?Mailcode_Parser_Statement_Tokenizer_Token_Keyword |
126
|
|
|
{ |
127
|
|
|
return $this->requireParams() |
128
|
|
|
->getInfo() |
129
|
|
|
->getKeywordsCollection() |
130
|
|
|
->getByName($keyword); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|