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