Test Failed
Push — master ( 9c7d00...09444e )
by
unknown
08:57
created

Mailcode_Variables::dollarizeName()   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 1
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * File containing the {@see \Mailcode\Mailcode_Variables} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Variables
7
 * @see \Mailcode\Mailcode_Variables
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Handler for all variable-related tasks.
16
 *
17
 * @package Mailcode
18
 * @subpackage Variables
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
class Mailcode_Variables
22
{
23
    public const REGEX_VARIABLE_NAME = '/\$\s*([A-Z0-9_]+)\s*\.\s*([A-Z0-9_]+)|\$\s*([A-Z0-9_]+)/six';
24
    
25
   /**
26
    * @var Mailcode_Variables_Collection_Regular
27
    */
28
    protected Mailcode_Variables_Collection_Regular $collection;
29
30
    /**
31
     * Parses the specified string to find all variable names contained within, if any.
32
     *
33
     * @param string $subject
34
     * @param Mailcode_Commands_Command|null $sourceCommand
35
     * @return Mailcode_Variables_Collection_Regular
36
     */
37
    public function parseString(string $subject, ?Mailcode_Commands_Command $sourceCommand=null) : Mailcode_Variables_Collection_Regular
38
    {
39
        $this->collection = new Mailcode_Variables_Collection_Regular();
40
        
41
        $matches = array();
42
        preg_match_all(self::REGEX_VARIABLE_NAME, $subject, $matches, PREG_PATTERN_ORDER);
43
        
44
        if(empty($matches[0]))
45
        {
46
            return $this->collection;
47
        }
48
        
49
        foreach($matches[0] as $idx => $matchedText)
50
        {
51
            if(!empty($matches[3][$idx]))
52
            {
53
                $this->addSingle($matches[3][$idx], $matchedText, $sourceCommand);
54
            }
55
            else 
56
            {
57
                $this->addPathed($matches[1][$idx], $matches[2][$idx], $matchedText, $sourceCommand);
58
            }
59
        }
60
        
61
        return $this->collection;
62
    }
63
    
64
    protected function addSingle(string $name, string $matchedText, ?Mailcode_Commands_Command $sourceCommand=null) : void
65
    {
66
        // ignore US style numbers like $451
67
        if(is_numeric($name))
68
        {
69
            return;
70
        }
71
        
72
        $this->collection->add(new Mailcode_Variables_Variable('', $name, $matchedText, $sourceCommand));
73
    }
74
    
75
    protected function addPathed(string $path, string $name, string $matchedText, ?Mailcode_Commands_Command $sourceCommand=null) : void
76
    {
77
        // ignore US style numbers like $45.12
78
        if(is_numeric($path.'.'.$name))
79
        {
80
            return;
81
        }
82
        
83
        $this->collection->add(new Mailcode_Variables_Variable($path, $name, $matchedText, $sourceCommand));
84
    }
85
86
    public static function dollarizeName(string $name) : string
87
    {
88
        return '$'.self::undollarizeName($name);
89
    }
90
91
    public static function undollarizeName(string $name) : string
92
    {
93
        return ltrim($name, '$');
94
    }
95
96
    public function createVariable(string $path, ?string $name='') : Mailcode_Variables_Variable
97
    {
98
        if(empty($name)) {
99
            $fullName = dollarize($path);
100
            $name = $path;
101
            $path = '';
102
        } else {
103
            $fullName = dollarize($path.'.'.$name);
104
        }
105
106
        return new Mailcode_Variables_Variable($path, $name, $fullName);
107
    }
108
109
    public function createVariableByName(string $name) : Mailcode_Variables_Variable
110
    {
111
        $parts = explode('.', undollarize($name));
112
113
        if(count($parts) === 1) {
114
            return $this->createVariable($parts[0]);
115
        }
116
117
        return $this->createVariable($parts[0], $parts[1]);
118
    }
119
}
120