4 * Implementation of SiteList using GenericArrayObject.
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.
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.
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
26 * @license GNU GPL v2+
27 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
29 class SiteArray
extends GenericArrayObject
implements SiteList
{
32 * Internal site identifiers pointing to their sites offset value.
36 * @var array of integer
38 protected $byInternalId = array();
41 * Global site identifiers pointing to their sites offset value.
45 * @var array of string
47 protected $byGlobalId = array();
50 * @see GenericArrayObject::getObjectType
56 public function getObjectType() {
61 * @see GenericArrayObject::preSetElement
65 * @param int|string $index
70 protected function preSetElement( $index, $site ) {
71 if ( $this->hasSite( $site->getGlobalId() ) ) {
72 $this->removeSite( $site->getGlobalId() );
75 $this->byGlobalId
[$site->getGlobalId()] = $index;
76 $this->byInternalId
[$site->getInternalId()] = $index;
82 * @see ArrayObject::offsetUnset()
88 public function offsetUnset( $index ) {
92 $site = $this->offsetGet( $index );
94 if ( $site !== false ) {
95 unset( $this->byGlobalId
[$site->getGlobalId()] );
96 unset( $this->byInternalId
[$site->getInternalId()] );
99 parent
::offsetUnset( $index );
103 * @see SiteList::getGlobalIdentifiers
109 public function getGlobalIdentifiers() {
110 return array_keys( $this->byGlobalId
);
114 * @see SiteList::hasSite
116 * @param string $globalSiteId
120 public function hasSite( $globalSiteId ) {
121 return array_key_exists( $globalSiteId, $this->byGlobalId
);
125 * @see SiteList::getSite
129 * @param string $globalSiteId
133 public function getSite( $globalSiteId ) {
134 return $this->offsetGet( $this->byGlobalId
[$globalSiteId] );
138 * @see SiteList::removeSite
142 * @param string $globalSiteId
144 public function removeSite( $globalSiteId ) {
145 $this->offsetUnset( $this->byGlobalId
[$globalSiteId] );
149 * @see SiteList::isEmpty
155 public function isEmpty() {
156 return $this->byGlobalId
=== array();
160 * @see SiteList::hasInternalId
166 public function hasInternalId( $id ) {
167 return array_key_exists( $id, $this->byInternalId
);
171 * @see SiteList::getSiteByInternalId
179 public function getSiteByInternalId( $id ) {
180 return $this->offsetGet( $this->byInternalId
[$id] );
184 * @see SiteList::removeSiteByInternalId
190 public function removeSiteByInternalId( $id ) {
191 $this->offsetUnset( $this->byInternalId
[$id] );
195 * @see SiteList::setSite
201 public function setSite( Site
$site ) {