Passed
Pull Request — master (#18)
by Sebastian
07:37
created

Mailcode_Commands_Command_ShowNumber::isAbsolute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Commands_Command_ShowNumber} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Commands
7
 * @see Mailcode_Commands_Command_ShowNumber
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
use Mailcode\Interfaces\Commands\Validation\AbsoluteKeywordInterface;
15
use Mailcode\Traits\Commands\Validation\AbsoluteKeywordTrait;
16
17
/**
18
 * Mailcode command: show a date variable value.
19
 *
20
 * @package Mailcode
21
 * @subpackage Commands
22
 * @author Sebastian Mordziol <[email protected]>
23
 */
24
class Mailcode_Commands_Command_ShowNumber
25
    extends Mailcode_Commands_ShowBase
26
    implements AbsoluteKeywordInterface
27
{
28
    use AbsoluteKeywordTrait;
29
30
    public const VALIDATION_PADDING_SEPARATOR_OVERFLOW = 72202;
31
    public const VALIDATION_INVALID_FORMAT_NUMBER = 72203;
32
    public const VALIDATION_INVALID_THOUSANDS_SEPARATOR = 72204;
33
    public const VALIDATION_INVALID_DECIMALS_NO_DECIMALS = 72205;
34
    public const VALIDATION_INVALID_CHARACTERS = 72206;
35
    public const VALIDATION_PADDING_INVALID_CHARS = 72207;
36
    public const VALIDATION_INVALID_DECIMALS_CHARS = 72208;
37
    public const VALIDATION_INVALID_DECIMAL_SEPARATOR = 72209;
38
    public const VALIDATION_SEPARATORS_SAME_CHARACTER = 72210;
39
40
    /**
41
     * The default number format string.
42
     * @var string
43
     */
44
    private string $formatString = Mailcode_Number_Info::DEFAULT_FORMAT;
45
46
    public function getName(): string
47
    {
48
        return 'shownumber';
49
    }
50
51
    public function getLabel(): string
52
    {
53
        return t('Show number variable');
54
    }
55
56
    protected function getValidations(): array
57
    {
58
        return array(
59
            Mailcode_Interfaces_Commands_Validation_Variable::VALIDATION_NAME_VARIABLE,
60
            'check_format',
61
            AbsoluteKeywordInterface::VALIDATION_NAME
62
        );
63
    }
64
65
    protected function validateSyntax_check_format(): void
66
    {
67
        $tokens = $this->requireParams()
68
            ->getInfo()
69
            ->getStringLiterals();
70
71
        // no format specified? Use the default one.
72
        if (empty($tokens)) {
73
            return;
74
        }
75
76
        $token = array_pop($tokens);
77
        $this->parseFormatString($token->getText());
78
    }
79
80
    private function parseFormatString(string $format): void
81
    {
82
        $result = new Mailcode_Number_Info($format);
83
84
        if ($result->isValid()) {
85
            $this->formatString = $format;
86
            return;
87
        }
88
89
        $this->validationResult->makeError(
90
            $result->getErrorMessage(),
91
            $result->getCode()
92
        );
93
    }
94
95
    /**
96
     * Retrieves the format string used to format the number.
97
     *
98
     * @return string
99
     */
100
    public function getFormatString(): string
101
    {
102
        return $this->formatString;
103
    }
104
105
    /**
106
     * Retrieves information on how the number should be formatted.
107
     *
108
     * @return Mailcode_Number_Info
109
     */
110
    public function getFormatInfo(): Mailcode_Number_Info
111
    {
112
        return new Mailcode_Number_Info($this->getFormatString());
113
    }
114
}
115
116