Issues (92)

Commands/Validation/ListPropertyVariable.php (1 issue)

1
<?php
2
/**
3
 * File containing the class {@see \Mailcode\Mailcode_Traits_Commands_Validation_ListPropertyVariable}.
4
 *
5
 * @package Mailcode
6
 * @subpackage Validation
7
 * @see \Mailcode\Mailcode_Traits_Commands_Validation_ListPropertyVariable
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
use AppUtils\OperationResult;
15
16
/**
17
 * Command validation drop-in: used by commands that accept
18
 * a single list variable property. Ensures that the specified
19
 * variable uses the scheme `$LIST.PROPERTY`, and not a single
20
 * path variable like `$FOO`.
21
 *
22
 * @package Mailcode
23
 * @subpackage Validation
24
 * @author Sebastian Mordziol <[email protected]>
25
 *
26
 * @see Mailcode_Interfaces_Commands_Validation_ListPropertyVariable
27
 *
28
 * @property Mailcode_Parser_Statement_Validator $validator
29
 * @property OperationResult $validationResult
30
 */
31
trait Mailcode_Traits_Commands_Validation_ListPropertyVariable
32
{
33
    /**
34
     * @var Mailcode_Variables_Variable|null
35
     */
36
    protected $listVariable = null;
37
38
    abstract public function getVariable() : Mailcode_Variables_Variable;
39
40
    protected function validateSyntax_list_property_variable() : void
41
    {
42
        $var = $this->getVariable();
43
44
        // Split the variable name by the dot
45
        $parts = explode('.', undollarize($var->getFullName()));
46
47
        if(count($parts) !== 2)
48
        {
49
            $this->validationResult->makeError(
50
                t('The variable %1$s is not a list property:', '<code>'.$var->getFullName().'</code>').' '.
51
                t('Expected a name with a dot, like %1$s', '<code>$'.t('LIST.PROPERTY').'</code>'),
52
                Mailcode_Interfaces_Commands_Validation_ListPropertyVariable::VALIDATION_NOT_A_LIST_PROPERTY
53
            );
54
55
            return;
56
        }
57
58
        // Create the list variable from the path of the property variable.
59
        $this->listVariable = new Mailcode_Variables_Variable(
60
            '',
61
            $parts[0],
62
            dollarize($parts[0]),
63
            $var->getSourceCommand()
64
        );
65
    }
66
67
    /**
68
     * Retrieves the specified list variable, if any.
69
     * If the command is erroneous no list variable may
70
     * be present, in which case an exception is thrown.
71
     *
72
     * @return Mailcode_Variables_Variable
73
     * @throws Mailcode_Exception
74
     *
75
     * @see Mailcode_Interfaces_Commands_Validation_ListPropertyVariable::ERROR_NO_LIST_VARIABLE_PRESENT
76
     */
77
    public function getListVariable() : Mailcode_Variables_Variable
78
    {
79
        if(isset($this->listVariable))
80
        {
81
            return $this->listVariable;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->listVariable could return the type null which is incompatible with the type-hinted return Mailcode\Mailcode_Variables_Variable. Consider adding an additional type-check to rule them out.
Loading history...
82
        }
83
84
        throw new Mailcode_Exception(
85
            'No list variable present.',
86
            '',
87
            Mailcode_Interfaces_Commands_Validation_ListPropertyVariable::ERROR_NO_LIST_VARIABLE_PRESENT
88
        );
89
    }
90
91
    /**
92
     * @return Mailcode_Variables_Variable
93
     * @throws Mailcode_Exception
94
     */
95
    public function getListProperty() : Mailcode_Variables_Variable
96
    {
97
        return $this->getVariable();
98
    }
99
}
100