Passed
Push — master ( 174259...689687 )
by Sebastian
04:01
created

validateSyntax_check_format()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 16
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 34
rs 9.7333
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_ShowVariable
22
{
23
    const VALIDATION_NOT_A_FORMAT_STRING = 55401;
24
    
25
   /**
26
    * The date format string.
27
    * @var string
28
    */
29
    private $formatString = "Y/m/d";
30
    
31
   /**
32
    * @var Mailcode_Date_FormatInfo
33
    */
34
    private $formatInfo;
35
    
36
    public function getName() : string
37
    {
38
        return 'showdate';
39
    }
40
    
41
    public function getLabel() : string
42
    {
43
        return t('Show date variable');
44
    }
45
    
46
    protected function init() : void
47
    {
48
        $this->formatInfo = Mailcode_Factory::createDateInfo();
49
        $this->formatString = $this->formatInfo->getDefaultFormat();
50
        
51
        parent::init();
52
    }
53
    
54
    protected function validateSyntax_check_format() : void
55
    {
56
         $token = $this->params->getInfo()->getTokenByIndex(1);
57
         
58
         // no format specified? Use the default one.
59
         if($token === null)
60
         {
61
             return;
62
         }
63
         
64
         if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral)
65
         {
66
             $format = $token->getText();
67
             
68
             $result = $this->formatInfo->validateFormat($format);
69
             
70
             if($result->isValid())
71
             {
72
                $this->formatString = $format;
73
             }
74
             else
75
             {
76
                 $this->validationResult->makeError(
77
                     $result->getErrorMessage(), 
78
                     $result->getCode()
79
                 );
80
             }
81
             
82
             return;
83
         }
84
         
85
         $this->validationResult->makeError(
86
            t('The second parameter must be a date format string.'),
87
            self::VALIDATION_NOT_A_FORMAT_STRING
88
         );
89
    }
90
    
91
   /**
92
    * Retrieves the format string used to format the date.
93
    * 
94
    * @return string A PHP compatible date format string.
95
    */
96
    public function getFormatString() : string
97
    {
98
        return $this->formatString;
99
    }
100
    
101
    protected function getValidations() : array
102
    {
103
        $validations = parent::getValidations();
104
        $validations[] = 'check_format';
105
        
106
        return $validations;
107
    }
108
}
109