Passed
Push — master ( 13a961...900cb1 )
by Sebastian
03:37
created

requiresParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Commands_Command_ShowDate} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Commands
7
 * @see Mailcode_Commands_Command_ShowDate
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Mailcode command: show a date variable value.
16
 *
17
 * @package Mailcode
18
 * @subpackage Commands
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
class Mailcode_Commands_Command_ShowDate extends Mailcode_Commands_Command implements Mailcode_Commands_Command_Type_Standalone
22
{
23
    use Mailcode_Traits_Commands_Validation_Variable;
24
25
    const VALIDATION_NOT_A_FORMAT_STRING = 55401;
26
    
27
   /**
28
    * The date format string.
29
    * @var string
30
    */
31
    private $formatString = "Y/m/d";
32
    
33
   /**
34
    * @var Mailcode_Date_FormatInfo
35
    */
36
    private $formatInfo;
37
38
    public function getName() : string
39
    {
40
        return 'showdate';
41
    }
42
43
    public function getLabel() : string
44
    {
45
        return t('Show date variable');
46
    }
47
48
    public function supportsType(): bool
49
    {
50
        return false;
51
    }
52
53
    public function getDefaultType() : string
54
    {
55
        return '';
56
    }
57
58
    public function requiresParameters(): bool
59
    {
60
        return true;
61
    }
62
63
    public function supportsLogicKeywords() : bool
64
    {
65
        return false;
66
    }
67
68
    public function generatesContent() : bool
69
    {
70
        return true;
71
    }
72
73
    protected function getValidations() : array
74
    {
75
        return array(
76
            'variable',
77
            'check_format'
78
        );
79
    }
80
81
    protected function init() : void
82
    {
83
        $this->formatInfo = Mailcode_Factory::createDateInfo();
84
        $this->formatString = $this->formatInfo->getDefaultFormat();
85
        
86
        parent::init();
87
    }
88
    
89
    protected function validateSyntax_check_format() : void
90
    {
91
         $token = $this->params->getInfo()->getTokenByIndex(1);
92
         
93
         // no format specified? Use the default one.
94
         if($token === null)
95
         {
96
             return;
97
         }
98
         
99
         if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral)
100
         {
101
             $format = $token->getText();
102
             
103
             $result = $this->formatInfo->validateFormat($format);
104
             
105
             if($result->isValid())
106
             {
107
                $this->formatString = $format;
108
             }
109
             else
110
             {
111
                 $this->validationResult->makeError(
112
                     $result->getErrorMessage(), 
113
                     $result->getCode()
114
                 );
115
             }
116
             
117
             return;
118
         }
119
         
120
         $this->validationResult->makeError(
121
            t('The second parameter must be a date format string.'),
122
            self::VALIDATION_NOT_A_FORMAT_STRING
123
         );
124
    }
125
    
126
   /**
127
    * Retrieves the format string used to format the date.
128
    * 
129
    * @return string A PHP compatible date format string.
130
    */
131
    public function getFormatString() : string
132
    {
133
        return $this->formatString;
134
    }
135
}
136
137