Passed
Push — master ( d3eb9e...ba3bb3 )
by Sebastian
03:58
created

Mailcode_Parser_Statement::prepareStatement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Parser_Statement} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Parser
7
 * @see Mailcode_Parser_Statement
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
use AppUtils\OperationResult;
15
16
/**
17
 * Mailcode statement parser: parses arbitrary statements
18
 * to check for validation issues.
19
 *
20
 * @package Mailcode
21
 * @subpackage Parser
22
 * @author Sebastian Mordziol <[email protected]>
23
 */
24
class Mailcode_Parser_Statement
25
{
26
    public const VALIDATION_EMPTY = 48801;
27
    public const VALIDATION_UNQUOTED_STRING_LITERALS = 48802;
28
    
29
   /**
30
    * @var string
31
    */
32
    protected $statement;
33
    
34
   /**
35
    * @var OperationResult
36
    */
37
    protected $result;
38
    
39
   /**
40
    * @var Mailcode_Parser_Statement_Tokenizer
41
    */
42
    protected $tokenizer;
43
    
44
   /**
45
    * @var Mailcode_Parser_Statement_Info|NULL
46
    */
47
    protected $info;
48
49
    /**
50
     * @var bool
51
     */
52
    protected $freeform = false;
53
54
    /**
55
     * @var Mailcode_Commands_Command|null
56
     */
57
    private $sourceCommand;
58
59
    public function __construct(string $statement, bool $freeform=false, ?Mailcode_Commands_Command $sourceCommand=null)
60
    {
61
        $this->sourceCommand = $sourceCommand;
62
        $this->statement = $this->prepareStatement($statement);
63
        $this->result = new OperationResult($this);
64
        $this->tokenizer = new Mailcode_Parser_Statement_Tokenizer($this);
65
        $this->freeform = $freeform;
66
67
        $this->validate();
68
    }
69
70
    private function prepareStatement(string $statement) : string
71
    {
72
        return str_replace(
73
            array(
74
                Mailcode_Parser::LITERAL_BRACKET_LEFT_REPLACEMENT,
75
                Mailcode_Parser::LITERAL_BRACKET_RIGHT_REPLACEMENT
76
            ),
77
            array(
78
                '{',
79
                '}'
80
            ),
81
            $statement
82
        );
83
    }
84
85
    public function getSourceCommand() : ?Mailcode_Commands_Command
86
    {
87
        return $this->sourceCommand;
88
    }
89
90
    public function getStatementString() : string
91
    {
92
        return $this->statement;
93
    }
94
    
95
    public function isValid() : bool
96
    {
97
        $this->validate();
98
99
        return $this->result->isValid();
100
    }
101
    
102
    public function getValidationResult() : OperationResult
103
    {
104
        return $this->result;
105
    }
106
    
107
    public function getInfo() : Mailcode_Parser_Statement_Info
108
    {
109
        if($this->info instanceof Mailcode_Parser_Statement_Info)
110
        {
111
            return $this->info; 
112
        }
113
        
114
        $this->info = new Mailcode_Parser_Statement_Info($this->tokenizer);
115
        
116
        return $this->info;
117
    }
118
    
119
    protected function validate() : void
120
    {
121
        if($this->freeform)
122
        {
123
            return;
124
        }
125
126
        if(!$this->tokenizer->hasTokens())
127
        {
128
            $this->result->makeError(
129
                t('Empty statement'),
130
                self::VALIDATION_EMPTY
131
            );
132
            
133
            return;
134
        }
135
        
136
        $unknown = $this->tokenizer->getFirstUnknown();
137
        
138
        if($unknown)
139
        {
140
            $this->result->makeError(
141
               t('Unquoted string literal found:').' ('.htmlspecialchars($unknown->getMatchedText()).')',
142
                self::VALIDATION_UNQUOTED_STRING_LITERALS
143
            );
144
        }
145
    }
146
    
147
    public function getNormalized() : string
148
    {
149
        return $this->tokenizer->getNormalized();
150
    }
151
}
152