Passed
Push — master ( 75ee28...758f58 )
by Sebastian
04:00
created

Mailcode_Traits_Commands_IfNumber   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 36
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validateSyntax_numeric_value() 0 12 2
A getValidations() 0 6 1
A getRawNumber() 0 3 1
A getNumber() 0 3 1
1
<?php
2
/**
3
 * File containing the {@see \Mailcode\Mailcode_Traits_Commands_IfNumber} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Commands
7
 * @see \Mailcode\Mailcode_Traits_Commands_IfNumber
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Mailcode command: opening IF NUMBER statement.
16
 *
17
 * @package Mailcode
18
 * @subpackage Commands
19
 * @author Sebastian Mordziol <[email protected]>
20
 * 
21
 * @property Mailcode_Parser_Statement $params
22
 * @property \AppUtils\OperationResult $validationResult
23
 * @property Mailcode_Parser_Statement_Validator $validator
24
 */
25
trait Mailcode_Traits_Commands_IfNumber
26
{
27
    use Mailcode_Traits_Commands_Validation_Variable;
28
    use Mailcode_Traits_Commands_Validation_Value;
29
30
    protected function getValidations() : array
31
    {
32
        return array(
33
            'variable',
34
            'value',
35
            'numeric_value'
36
        );
37
    }
38
39
    public function getNumber() : float
40
    {
41
        return floatval($this->getRawNumber());
42
    }
43
44
    protected function getRawNumber() : string
45
    {
46
        return str_replace(array(',', ' '), array('.', ''), trim($this->getValue(), '"'));
47
    }
48
49
    protected function validateSyntax_numeric_value(): void
50
    {
51
        $value = $this->getRawNumber();
52
53
        if(!is_numeric($value))
54
        {
55
            $this->validationResult->makeError(
56
                t(
57
                    '%1$s is not a valid numeric value.',
58
                    '"'.trim($this->getValue(), '"').'"'
59
                ),
60
                Mailcode_Commands_CommonConstants::VALIDATION_VALUE_NOT_NUMERIC
61
            );
62
        }
63
    }
64
}
65