This repository has been archived by the owner on Nov 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
DirectiveCollection.php
162 lines (142 loc) · 3.86 KB
/
DirectiveCollection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace Nemiro\Apache
{
/*
* Copyright © Aleksey Nemiro, 2015. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Represents collection of Apache sections or directives.
*
* @author Aleksey Nemiro <[email protected]>
* @copyright © Aleksey Nemiro, 2015. All rights reserved.
*/
class DirectiveCollection implements \ArrayAccess
{
/**
* The list of sections or directives.
*
* @var Directive[]|DirectiveGroup[]
*/
public $Items;
function __construct()
{
$this->Items = array();
}
/**
* Determines whether the DirectiveCollection contains the element with specified name.
*
* @param \string $name The key to locate in the DirectiveCollection.
* @return \bool
*/
public function ContainsDirective($name)
{
if (is_null($name) || (gettype($name) != 'string' && gettype($name) != 'integer') || (string)$name == '')
{
throw new \InvalidArgumentException('Name is required. The name must be a string. Value can not be null or empty.');
}
return array_key_exists($name, $this->Items);
}
/**
* Adds a new section, directive or group to the collection.
*
* @param Directive|DirectiveGroup[] $item The section, directive or group to add.
* @return Directive|DirectiveGroup
*/
public function Add($item)
{
if ($this->ContainsDirective($item->Name))
{
throw new \ErrorException(sprintf('Directive `%s` already exists.', $item->Name));
}
$this->Items[$item->Name] = $item;
return end($this->Items);
}
/**
* Returns elements count.
*
* @return \int
*/
public function Count()
{
return $this->Items != NULL ? count($this->Items) : 0;
}
/**
* Returns the first element of the collection.
*
* @return Directive|DirectiveGroup
*/
public function First()
{
reset($this->Items);
return current($this->Items);
}
/**
* Returns the last element of the collection.
*
* @return Directive|DirectiveGroup
*/
public function Last()
{
return end($this->Items);
}
#region ArrayAccess Members
/**
* Whether a offset exists
* Whether or not an offset exists.
*
* @param \string|\int $offset An offset to check for.
* @return \bool
*/
function offsetExists($offset)
{
return isset($this->Items[$offset]);
}
/**
* Offset to retrieve
* Returns the value at specified offset.
*
* @param mixed $offset The offset to retrieve.
* @return Directive|DirectiveGroup
*/
function offsetGet($offset)
{
return isset($this->Items[$offset]) ? $this->Items[$offset] : NULL;
}
/**
* Offset to set
* Assigns a value to the specified offset.
*
* @param mixed $offset The offset to assign the value to.
* @param mixed $value The value to set.
* @return void
*/
function offsetSet($offset, $value)
{
$this->Items[$offset] = $value;
}
/**
* Offset to unset
* Unsets an offset.
*
* @param mixed $offset The offset to unset.
* @return void
*/
function offsetUnset($offset)
{
unset($this->Items[$offset]);
}
#endregion
}
}