Passed
Push — master ( d9af9b...707ba4 )
by Sebastian
03:07
created

getGroupedByUniqueName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 10
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
    * @var Mailcode_Variables_Variable[]
25
    */
26
    protected $variables = array();
27
    
28
    public function __construct()
29
    {
30
        $this->init();
31
    }
32
    
33
    protected function init() : void
34
    {
35
        
36
    }
37
    
38
    public function add(Mailcode_Variables_Variable $variable) : Mailcode_Variables_Collection
39
    {
40
        $this->variables[] = $variable;
41
        
42
        return $this;
43
    }
44
    
45
    public function hasVariables() : bool
46
    {
47
        return !empty($this->variables);
48
    }
49
    
50
    public function countVariables() : int
51
    {
52
        return count($this->variables);
53
    }
54
    
55
   /**
56
    * Checks whether the collection contains a variable with the specified name.
57
    * 
58
    * @param string $fullName The variable name, with or without $ sign.
59
    * @return bool
60
    */
61
    public function hasVariableName(string $fullName) : bool
62
    {
63
        $fullName = $this->fixName($fullName);
64
        
65
        foreach($this->variables as $variable)
66
        {
67
            if($variable->getFullName() === $fullName)
68
            {
69
                return true;
70
            }
71
        }
72
        
73
        return false;
74
    }
75
    
76
   /**
77
    * Retrieves a collection of all variable instances for
78
    * the specified name (there can be several with differing
79
    * matched texts because of spacing).
80
    * 
81
    * @param string $fullName
82
    * @return Mailcode_Variables_Collection
83
    */
84
    public function getByFullName(string $fullName) : Mailcode_Variables_Collection
85
    {
86
        $fullName = $this->fixName($fullName);
87
        
88
        $collection = new Mailcode_Variables_Collection_Regular();
89
        
90
        foreach($this->variables as $variable)
91
        {
92
            if($variable->getFullName() === $fullName)
93
            {
94
                $collection->add($variable);
95
            }
96
        }
97
        
98
        return $collection;
99
    }
100
    
101
   /**
102
    * Prepends the $ sign to a variable name if it does not have it.
103
    * 
104
    * @param string $fullName
105
    * @return string
106
    */
107
    protected function fixName(string $fullName) : string
108
    {
109
        if(substr($fullName, 0, 1) === '$')
110
        {
111
            return $fullName;
112
        }
113
        
114
        return '$'.$fullName;
115
    }
116
    
117
   /**
118
    * Retrieves all variables, grouped by their hash - meaning
119
    * unique matched strings.
120
    * 
121
    * @return Mailcode_Variables_Variable[]
122
    */
123
    public function getGroupedByHash() : array
124
    {
125
        $entries = array();
126
        
127
        foreach($this->variables as $variable)
128
        {
129
            $entries[$variable->getHash()] = $variable;
130
        }
131
        
132
        return $this->sortVariables($entries);
133
    }
134
    
135
   /**
136
    * Retrieves all variables, grouped by their name. 
137
    * 
138
    * @return Mailcode_Variables_Variable[]
139
    */
140
    public function getGroupedByName() : array
141
    {
142
        $entries = array();
143
        
144
        foreach($this->variables as $variable)
145
        {
146
            $entries[$variable->getFullName()] = $variable;
147
        }
148
        
149
        return $this->sortVariables($entries);
150
    }
151
152
    /**
153
     * Retrieves all variables, grouped by the unique commands
154
     * they are tied to.
155
     *
156
     * @return Mailcode_Variables_Variable[]
157
     * @throws Mailcode_Exception
158
     */
159
    public function getGroupedByUniqueName() : array
160
    {
161
        $entries = array();
162
163
        foreach($this->variables as $variable)
164
        {
165
            $entries[$variable->getUniqueName()] = $variable;
166
        }
167
168
        return $this->sortVariables($entries);
169
    }
170
    
171
   /**
172
    * Retrieves all variables, in the order they were addded.
173
    * @return Mailcode_Variables_Variable[]
174
    */
175
    public function getAll()
176
    {
177
        return $this->variables;
178
    }
179
    
180
   /**
181
    * Retrieves the full names of the variables that are present in the collection.
182
    * @return string[]
183
    */
184
    public function getNames() : array
185
    {
186
        $result = array();
187
        
188
        foreach($this->variables as $variable)
189
        {
190
            $name = $variable->getFullName();
191
            
192
            if(!in_array($name, $result))
193
            {
194
                $result[] = $name;
195
            }
196
        }
197
        
198
        return $result;
199
    }
200
    
201
   /**
202
    * Takes a list of variables and sorts them, throwing away
203
    * the source array's keys.
204
    * 
205
    * @param Mailcode_Variables_Variable[] $entries
206
    * @return Mailcode_Variables_Variable[]
207
    */
208
    protected function sortVariables(array $entries)
209
    {
210
        $result = array_values($entries);
211
        
212
        usort($entries, function(Mailcode_Variables_Variable $a, Mailcode_Variables_Variable $b)
213
        {
214
            return strnatcasecmp($a->getFullName(), $b->getFullName());
215
        });
216
        
217
        return $result;
218
    }
219
220
   /**
221
    *  Merges the variables collection with the target collection
222
    *  by inheriting all that collection's variables.
223
    *  
224
    * @param Mailcode_Variables_Collection $collection
225
    * @return Mailcode_Variables_Collection
226
    */
227
    public function mergeWith(Mailcode_Variables_Collection $collection) : Mailcode_Variables_Collection
228
    {
229
        $variables = $collection->getGroupedByHash();
230
        
231
        foreach($variables as $variable)
232
        {
233
            $this->add($variable);
234
        }
235
        
236
        return $this;
237
    }
238
239
    public function getFirst() : ?Mailcode_Variables_Variable
240
    {
241
        $variables = $this->getAll();
242
        
243
        if(!empty($variables))
244
        {
245
            return array_shift($variables);
246
        }
247
        
248
        return null;
249
    }
250
}
251