Passed
Push — master ( 77b992...62880d )
by Sebastian
02:14
created

Mailcode_Variables_Collection::getGroupedByHash()   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_Variables_Collection} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Variables
7
 * @see Mailcode_Variables_Collection
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
abstract class Mailcode_Variables_Collection
22
{
23
   /**
24
    * Stores variables by their hash.
25
    * 
26
    * @var array[string]Mailcode_Variables_Variable
0 ignored issues
show
Documentation Bug introduced by
The doc comment array[string]Mailcode_Variables_Variable at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
27
    */
28
    protected $variables = array();
29
    
30
    public function __construct()
31
    {
32
        $this->init();
33
    }
34
    
35
    protected function init() : void
36
    {
37
        
38
    }
39
    
40
    public function add(Mailcode_Variables_Variable $variable) : Mailcode_Variables_Collection
41
    {
42
        $hash = $variable->getHash();
43
        
44
        $this->variables[$hash] = $variable;
45
        
46
        return $this;
47
    }
48
    
49
    public function hasVariables() : bool
50
    {
51
        return !empty($this->variables);
52
    }
53
    
54
    public function countVariables() : int
55
    {
56
        return count($this->variables);
57
    }
58
    
59
   /**
60
    * Checks whether the collection contains a variable with the specified name.
61
    * 
62
    * @param string $fullName The variable name, with or without $ sign.
63
    * @return bool
64
    */
65
    public function hasVariableName(string $fullName) : bool
66
    {
67
        $fullName = $this->fixName($fullName);
68
        
69
        foreach($this->variables as $variable)
70
        {
71
            if($variable->getFullName() === $fullName)
72
            {
73
                return true;
74
            }
75
        }
76
        
77
        return false;
78
    }
79
    
80
   /**
81
    * Retrieves a collection of all variable instances for
82
    * the specified name (there can be several with differing
83
    * matched texts because of spacing).
84
    * 
85
    * @param string $fullName
86
    * @return Mailcode_Variables_Collection
87
    */
88
    public function getByFullName(string $fullName) : Mailcode_Variables_Collection
89
    {
90
        $fullName = $this->fixName($fullName);
91
        
92
        $collection = new Mailcode_Variables_Collection_Regular();
93
        
94
        foreach($this->variables as $variable)
95
        {
96
            if($variable->getFullName() === $fullName)
97
            {
98
                $collection->add($variable);
99
            }
100
        }
101
        
102
        return $collection;
103
    }
104
    
105
   /**
106
    * Prepends the $ sign to a variable name if it does not have it.
107
    * 
108
    * @param string $fullName
109
    * @return string
110
    */
111
    protected function fixName(string $fullName) : string
112
    {
113
        if(substr($fullName, 0, 1) === '$')
114
        {
115
            return $fullName;
116
        }
117
        
118
        return '$'.$fullName;
119
    }
120
    
121
   /**
122
    * Retrieves all variables, grouped by their hash - meaning
123
    * unique matched strings.
124
    * 
125
    * @return Mailcode_Variables_Variable[]
126
    */
127
    public function getGroupedByHash()
128
    {
129
        return $this->sortVariables($this->variables);
130
    }
131
    
132
   /**
133
    * Retrieves all variables, grouped by their name. 
134
    * 
135
    * @return Mailcode_Variables_Variable[]
136
    */
137
    public function getGroupedByName()
138
    {
139
        $entries = array();
140
        
141
        foreach($this->variables as $variable)
142
        {
143
            $entries[$variable->getFullName()] = $variable;
144
        }
145
        
146
        return $this->sortVariables($entries);
147
    }
148
    
149
   /**
150
    * Retrieves the full names of the variables that are present in the collection.
151
    * @return string[]
152
    */
153
    public function getNames() : array
154
    {
155
        $result = array();
156
        
157
        foreach($this->variables as $variable)
158
        {
159
            $name = $variable->getFullName();
160
            
161
            if(!in_array($name, $result))
162
            {
163
                $result[] = $name;
164
            }
165
        }
166
        
167
        return $result;
168
    }
169
    
170
   /**
171
    * Takes a list of variables and sorts them, throwing away
172
    * the source array's keys.
173
    * 
174
    * @param array $entries
175
    * @return Mailcode_Variables_Variable[]
176
    */
177
    protected function sortVariables(array $entries)
178
    {
179
        $result = array_values($entries);
180
        
181
        usort($entries, function(Mailcode_Variables_Variable $a, Mailcode_Variables_Variable $b)
182
        {
183
            return strnatcasecmp($a->getFullName(), $b->getFullName());
184
        });
185
        
186
        return $result;
187
    }
188
189
   /**
190
    *  Merges the variables collection with the target collection
191
    *  by inheriting all that collection's variables.
192
    *  
193
    * @param Mailcode_Variables_Collection $collection
194
    * @return Mailcode_Variables_Collection
195
    */
196
    public function mergeWith(Mailcode_Variables_Collection $collection) : Mailcode_Variables_Collection
197
    {
198
        $variables = $collection->getGroupedByHash();
199
        
200
        foreach($variables as $variable)
201
        {
202
            $this->add($variable);
203
        }
204
        
205
        return $this;
206
    }
207
}
208