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

Mailcode_Traits_Commands_IfNumber::getNumber()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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