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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
| <?php
/**
* Copyright (C) 2011+ Martin Lacroix
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* @license GNU General Public License Version 3 (GPLv3)
* @link http://www.gnu.org/licenses/
*/
/**
* PHP_VER : PHP 5.3+
* LIBRARIES : SPL
* KEYWORDS : SPLOBJECTSTORAGE EXTENDED IMPROVED
* SPLOBJECTSTORAGE ETENDU AMELIORE
*
* Class managing an extended SplObjectStorage
* New features:
* - always move the internal pointer to the last object inserted into the storage
* - compute the hashcode of any arbitrary object without inserting it into the storage
* - read the hashcode of every or all objects in the storage
* - access an object by its hashcode or position in the storage
* - directly access the data attached to an object by its hashcode or position in the storage
*
* Classe gérant un SplObjectStorage amélioré
* Nouveautés :
* - déplace automatiquement le pointeur interne sur le dernier objet inséré
* - calcule le hashcode de n'importe quel objet sans l'insérer dans le silo
* - extraction d'un ou de tous les hashcodes du silo
* - sélection d'un objet identifié par son hashcode ou sa position dans le silo
* - lecture directe des infos attachées à un objet identifié par son hashcode ou sa position dans le silo
*
* @package tools
* @version 1.0.0
* @author Martin Lacroix
*/
class SplObjectStoragePlus extends SplObjectStorage {
private $_map = array();
private $_index = -1;
/**
* @param SplObjectStorage $storage
*/
function addAll(SplObjectStorage $storage) {
$storage->rewind();
while($storage->valid()) {
$this->offsetSet($storage->current(), $storage->getInfo());
$storage->next();
}
}
/**
* Add an object to the storage
* @param object $object
* @param mixed $data
*/
function attach($object, $data = NULL) {
$this->offsetSet($object, $data);
}
/**
* Remove an object from the storage
* @param object object
*/
function detach($object) {
$this->offsetUnset($object);
}
/**
* Add an object to the storage and set the internal pointer on it
* @param object $object
* @param mixed $data
*/
function offsetSet($object, $data = NULL) {
$hash = spl_object_hash($object);
if (FALSE === array_search($hash, $this->_map, TRUE)) {
parent::attach($object, $data);
$this->_map[++$this->_index] = $hash;
$this->selectObjectByKey($this->_index);
}
}
/**
* Remove an object from the storage
* @param object object
*/
function offsetUnset($object) {
$index = array_search(spl_object_hash($object), $this->_map, TRUE);
if (FALSE !== $index) {
parent::offsetUnset($object);
unset($this->_map[$index]);
}
}
/**
* @param SplObjectStorage $storage
*/
function removeAll(SplObjectStorage $storage) {
$storage->rewind();
while($storage->valid()) {
$this->offsetUnset($storage->current());
$storage->next();
}
}
/**
* @param SplObjectStorage $storage
*/
function removeAllExcept(SplObjectStorage $storage) {
parent::rewind();
while(parent::valid()) {
$current = parent::current();
parent::next();
if ( ! $storage->contains($current)) {
$this->offsetUnset($object);
}
}
}
/**
* Returns the hashcode of the current object
* @return string
*/
function getHash() {
return spl_object_hash(parent::current());
}
/**
* Returns the object identified by its hashcode
* @param string $hash
* @return object|NULL
*/
function getObjectByHash($hash) {
$index = array_search($hash, $this->_map);
if (FALSE !== $index) {
parent::rewind();
while(parent::valid()) {
if (parent::key() === $index) {
return parent::current();
}
parent::next();
}
}
}
/**
* Returns the position of an object in the storage
* Object identified by its hashcode
* @param string $hash
* @return int|NULL
*/
function getKeyByHash($hash) {
$index = array_search($hash, $this->_map, TRUE);
return (FALSE === $index) ? NULL : $index;
}
/**
* Returns the hashcode of an object identified by its position
* @param int $key
* @return string|NULL
*/
function getHashByKey($key) {
return $this->_map[$key];
}
/**
* Returns the data associated with an object identified by its hashcode
* @param string $hash
* @return mixed|NULL
*/
function getInfoByHash($hash) {
$index = array_search($hash, $this->_map, TRUE);
if (FALSE !== $index) {
return $this->getInfoByKey($index);
}
}
/**
* Returns the data associated with an object identified by its position
* @param int $key
* @return mixed|NULL
*/
function getInfoByKey($key) {
if (isset($this->_map[$key])) {
parent::rewind();
while(parent::valid()) {
if (parent::key() === $key) {
return parent::getInfo();
}
parent::next();
}
}
}
/**
* Move the internal pointer to an object identified by its hashcode
* @param mixed $hash
* @return bool FALSE if the object doesn't exist
*/
function selectObjectByHash($hash) {
$index = array_search($hash, $this->_map, TRUE);
return (FALSE !== $index)
? $this->selectObjectByKey($index)
: FALSE;
}
/**
* Move the internal pointer to an object identified by its position in the storage
* @param int $key
* @return bool FALSE if the object doesn't exist
*/
function selectObjectByKey($key) {
if (isset($this->_map[$key])) {
parent::rewind();
while(parent::valid()) {
if (parent::key() === $key) {
return TRUE;
}
parent::next();
}
}
return FALSE;
}
/**
* Returns the hashcode of the object in parameter
* @param mixed $object
* @return string
*/
static function computeHash($object) {
return spl_object_hash($object);
}
/**
* Returns the array of hashs
* @return array Array(position in the storage => hashcode)
*/
function allHashs() {
return $this->_map;
}
}
?> |
Partager