141629e050b6e402cdc74873f7038e3006bc1e14
[lhc/web/wiklou.git] / includes / site / SiteArray.php
1 <?php
2
3 /**
4 * Implementation of SiteList using GenericArrayObject.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @since 1.21
22 *
23 * @file
24 * @ingroup Site
25 *
26 * @license GNU GPL v2+
27 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
28 */
29 class SiteArray extends GenericArrayObject implements SiteList {
30
31 /**
32 * Internal site identifiers pointing to their sites offset value.
33 *
34 * @since 1.21
35 *
36 * @var array of integer
37 */
38 protected $byInternalId = array();
39
40 /**
41 * Global site identifiers pointing to their sites offset value.
42 *
43 * @since 1.21
44 *
45 * @var array of string
46 */
47 protected $byGlobalId = array();
48
49 /**
50 * @see GenericArrayObject::getObjectType
51 *
52 * @since 1.21
53 *
54 * @return string
55 */
56 public function getObjectType() {
57 return 'Site';
58 }
59
60 /**
61 * @see GenericArrayObject::preSetElement
62 *
63 * @since 1.21
64 *
65 * @param int|string $index
66 * @param Site $site
67 *
68 * @return boolean
69 */
70 protected function preSetElement( $index, $site ) {
71 if ( $this->hasSite( $site->getGlobalId() ) ) {
72 $this->removeSite( $site->getGlobalId() );
73 }
74
75 $this->byGlobalId[$site->getGlobalId()] = $index;
76 $this->byInternalId[$site->getInternalId()] = $index;
77
78 return true;
79 }
80
81 /**
82 * @see ArrayObject::offsetUnset()
83 *
84 * @since 1.21
85 *
86 * @param mixed $index
87 */
88 public function offsetUnset( $index ) {
89 /**
90 * @var Site $site
91 */
92 $site = $this->offsetGet( $index );
93
94 if ( $site !== false ) {
95 unset( $this->byGlobalId[$site->getGlobalId()] );
96 unset( $this->byInternalId[$site->getInternalId()] );
97 }
98
99 parent::offsetUnset( $index );
100 }
101
102 /**
103 * @see SiteList::getGlobalIdentifiers
104 *
105 * @since 1.21
106 *
107 * @return array
108 */
109 public function getGlobalIdentifiers() {
110 return array_keys( $this->byGlobalId );
111 }
112
113 /**
114 * @see SiteList::hasSite
115 *
116 * @param string $globalSiteId
117 *
118 * @return boolean
119 */
120 public function hasSite( $globalSiteId ) {
121 return array_key_exists( $globalSiteId, $this->byGlobalId );
122 }
123
124 /**
125 * @see SiteList::getSite
126 *
127 * @since 1.21
128 *
129 * @param string $globalSiteId
130 *
131 * @return Site
132 */
133 public function getSite( $globalSiteId ) {
134 return $this->offsetGet( $this->byGlobalId[$globalSiteId] );
135 }
136
137 /**
138 * @see SiteList::removeSite
139 *
140 * @since 1.21
141 *
142 * @param string $globalSiteId
143 */
144 public function removeSite( $globalSiteId ) {
145 $this->offsetUnset( $this->byGlobalId[$globalSiteId] );
146 }
147
148 /**
149 * @see SiteList::isEmpty
150 *
151 * @since 1.21
152 *
153 * @return boolean
154 */
155 public function isEmpty() {
156 return $this->byGlobalId === array();
157 }
158
159 /**
160 * @see SiteList::hasInternalId
161 *
162 * @param integer $id
163 *
164 * @return boolean
165 */
166 public function hasInternalId( $id ) {
167 return array_key_exists( $id, $this->byInternalId );
168 }
169
170 /**
171 * @see SiteList::getSiteByInternalId
172 *
173 * @since 1.21
174 *
175 * @param integer $id
176 *
177 * @return Site
178 */
179 public function getSiteByInternalId( $id ) {
180 return $this->offsetGet( $this->byInternalId[$id] );
181 }
182
183 /**
184 * @see SiteList::removeSiteByInternalId
185 *
186 * @since 1.21
187 *
188 * @param integer $id
189 */
190 public function removeSiteByInternalId( $id ) {
191 $this->offsetUnset( $this->byInternalId[$id] );
192 }
193
194 /**
195 * @see SiteList::setSite
196 *
197 * @since 1.21
198 *
199 * @param Site $site
200 */
201 public function setSite( Site $site ) {
202 $this[] = $site;
203 }
204
205 }