Test Failed
Push — master ( 36c5b6...af11e6 )
by Sebastian
04:51
created

getInternalFormat()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 10
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Translator_Syntax_ApacheVelocity_ShowDate} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Translator
7
 * @see Mailcode_Translator_Syntax_ApacheVelocity_ShowDate
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
use AppUtils\ConvertHelper;
15
16
/**
17
 * Translates the "ShowDate" command to Apache Velocity.
18
 *
19
 * @package Mailcode
20
 * @subpackage Translator
21
 * @author Sebastian Mordziol <[email protected]>
22
 */
23
class Mailcode_Translator_Syntax_ApacheVelocity_ShowDate extends Mailcode_Translator_Syntax_ApacheVelocity implements Mailcode_Translator_Command_ShowDate
24
{
25
    public const ERROR_UNKNOWN_DATE_FORMAT_CHARACTER = 55501;
26
27
   /**
28
    * The date format used in the date variable. This is used to convert
29
    * the native date to the format specified in the variable command.
30
    */
31
    public const DEFAULT_INTERNAL_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
32
33
   /**
34
    * @var string[]string
35
    */
36
    private $charTable = array(
37
        'd' => 'dd',
38
        'j' => 'd',
39
        'm' => 'MM',
40
        'n' => 'M',
41
        'Y' => 'yyyy',
42
        'y' => 'yy',
43
        'H' => 'H',
44
        'i' => 'm',
45
        's' => 's',
46
        '.' => '.',
47
        ':' => ':',
48
        '-' => '-',
49
        '/' => '/',
50
        ' ' => ' '
51
    );
52
53
    public function getInternalFormat(Mailcode_Commands_Command_ShowDate $command) : string
54
    {
55
        $internalFormat = $command->getTranslationParam('internal_format');
56
57
        if(is_string($internalFormat) && !empty($internalFormat))
58
        {
59
            return $internalFormat;
60
        }
61
62
        return self::DEFAULT_INTERNAL_FORMAT;
63
    }
64
65
    public function translate(Mailcode_Commands_Command_ShowDate $command): string
66
    {
67
        $internalFormat = $this->getInternalFormat($command);
68
        $varName = ltrim($command->getVariableName(), '$');
69
        $javaFormat = $this->translateFormat($command->getFormatString());
70
71
        $statement = sprintf(
72
            'date.format("%s", $date.toDate("%s", $%s))',
73
            $javaFormat,
74
            $internalFormat,
75
            $varName
76
        );
77
78
        return $this->addURLEncoding($command, $statement);
79
    }
80
81
    private function translateFormat(string $formatString) : string
82
    {
83
        $chars = ConvertHelper::string2array($formatString);
84
        $result = array();
85
        
86
        foreach($chars as $char)
87
        {
88
            if(!isset($this->charTable[$char]))
89
            {
90
                throw new Mailcode_Translator_Exception(
91
                    'Unknown date format string character',
92
                    sprintf(
93
                        'No translation for character %s available.',
94
                        ConvertHelper::hidden2visible($char)
95
                    ),
96
                    self::ERROR_UNKNOWN_DATE_FORMAT_CHARACTER
97
                );
98
                
99
            }
100
            
101
            $result[] = $this->charTable[$char]; 
102
        }
103
        
104
        return implode('', $result);
105
    }
106
}
107