|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File containing the {@see \Mailcode\Traits\Commands\Validation\CountTrait} trait. |
|
4
|
|
|
* |
|
5
|
|
|
* @see \Mailcode\Traits\Commands\Validation\CountTrait |
|
6
|
|
|
* @subpackage Validation |
|
7
|
|
|
* @package Mailcode |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Mailcode\Traits\Commands\Validation; |
|
13
|
|
|
|
|
14
|
|
|
use Mailcode\Interfaces\Commands\Validation\CountInterface; |
|
15
|
|
|
use Mailcode\Mailcode; |
|
16
|
|
|
use Mailcode\Mailcode_Commands_Keywords; |
|
17
|
|
|
use Mailcode\Mailcode_Factory; |
|
18
|
|
|
use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token_Variable; |
|
19
|
|
|
use Mailcode\Mailcode_Variables_Variable; |
|
20
|
|
|
use function Mailcode\t; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Command validation drop-in: checks for the presence |
|
24
|
|
|
* of the `count` parameter in the command statement. |
|
25
|
|
|
* |
|
26
|
|
|
* @package Mailcode |
|
27
|
|
|
* @subpackage Validation |
|
28
|
|
|
* @author Olaf Böcker <[email protected]> |
|
29
|
|
|
* |
|
30
|
|
|
* @see CountInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
trait CountTrait |
|
33
|
|
|
{ |
|
34
|
|
|
private bool $countEnabled = false; |
|
35
|
|
|
|
|
36
|
|
|
private ?Mailcode_Parser_Statement_Tokenizer_Token_Variable $countToken = null; |
|
37
|
|
|
|
|
38
|
|
|
protected function validateSyntax_check_count(): void |
|
39
|
|
|
{ |
|
40
|
|
|
$this->countToken = null; |
|
41
|
|
|
|
|
42
|
|
|
$token = $this |
|
43
|
|
|
->requireParams() |
|
|
|
|
|
|
44
|
|
|
->getInfo() |
|
45
|
|
|
->getTokenByParamName(CountInterface::PARAMETER_NAME); |
|
46
|
|
|
|
|
47
|
|
|
if($token === null) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->countEnabled = false; |
|
50
|
|
|
return; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if ($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Variable) { |
|
54
|
|
|
$this->countToken = $token; |
|
55
|
|
|
$this->countEnabled = true; |
|
56
|
|
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$this->validationResult->makeError( |
|
60
|
|
|
t('Invalid count subject:') . ' ' . t('Expected a variable.'), |
|
61
|
|
|
CountInterface::VALIDATION_COUNT_CODE_WRONG_TYPE |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function isCountEnabled(): bool |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->countEnabled; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function getCountVariable(): ?Mailcode_Variables_Variable |
|
71
|
|
|
{ |
|
72
|
|
|
if($this->countToken !== null) |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->countToken->getVariable(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return null; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param Mailcode_Variables_Variable|string|null $variable Set to null to remove the count. |
|
82
|
|
|
* @return $this |
|
83
|
|
|
*/ |
|
84
|
|
|
public function setCount($variable) : self |
|
85
|
|
|
{ |
|
86
|
|
|
$this->countEnabled = false; |
|
87
|
|
|
$this->countToken = null; |
|
88
|
|
|
|
|
89
|
|
|
$info = $this->requireParams()->getInfo(); |
|
90
|
|
|
|
|
91
|
|
|
if(isset($this->countToken)) { |
|
92
|
|
|
$info->removeToken($this->countToken); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if(is_string($variable)) { |
|
96
|
|
|
$variable = Mailcode_Factory::var()->fullName($variable); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if($variable !== null) { |
|
100
|
|
|
$this->countEnabled = true; |
|
101
|
|
|
$this->countToken = $info->addVariable($variable); |
|
102
|
|
|
$info->setParamName($this->countToken, CountInterface::PARAMETER_NAME); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|