/usr/share/php/MabeEnum/EnumMap.php is in php-enum 3.0.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | <?php
namespace MabeEnum;
use ArrayAccess;
use Countable;
use InvalidArgumentException;
use OutOfBoundsException;
use SeekableIterator;
use UnexpectedValueException;
/**
* A map of enumerators (EnumMap<T>) and mixed values.
*
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
* @copyright Copyright (c) 2017 Marc Bennewitz
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
*/
class EnumMap implements ArrayAccess, Countable, SeekableIterator
{
/**
* The classname of the enumeration type
* @var string
*/
private $enumeration;
/**
* Internal map of ordinal number and value
* @var array
*/
private $map = [];
/**
* List of ordinal numbers
* @var int[]
*/
private $ordinals = [];
/**
* Current iterator position
* @var int
*/
private $pos = 0;
/**
* Constructor
* @param string $enumeration The classname of the enumeration type
* @throws InvalidArgumentException
*/
public function __construct($enumeration)
{
if (!\is_subclass_of($enumeration, Enum::class)) {
throw new InvalidArgumentException(\sprintf(
"This EnumMap can handle subclasses of '%s' only",
Enum::class
));
}
$this->enumeration = $enumeration;
}
/**
* Get the classname of the enumeration
* @return string
*/
public function getEnumeration()
{
return $this->enumeration;
}
/**
* Get a list of map keys
* @return Enum[]
*/
public function getKeys()
{
return \array_map([$this->enumeration, 'byOrdinal'], $this->ordinals);
}
/**
* Get a list of map values
* @return mixed[]
*/
public function getValues()
{
return \array_values($this->map);
}
/**
* Search for the given value
* @param mixed $value
* @param bool $strict Use strict type comparison
* @return Enum|null The found key or NULL
*/
public function search($value, $strict = false)
{
$ord = \array_search($value, $this->map, $strict);
if ($ord !== false) {
$enumeration = $this->enumeration;
return $enumeration::byOrdinal($ord);
}
return null;
}
/**
* Test if the given enumerator exists
* @param Enum|null|boolean|int|float|string $enumerator
* @return boolean
* @see offsetExists
*/
public function contains($enumerator)
{
try {
$enumeration = $this->enumeration;
$ord = $enumeration::get($enumerator)->getOrdinal();
return array_key_exists($ord, $this->map);
} catch (InvalidArgumentException $e) {
// An invalid enumerator can't be contained in this map
return false;
}
}
/**
* Test if the given enumerator key exists and is not NULL
* @param Enum|null|boolean|int|float|string $enumerator
* @return boolean
* @see contains
*/
public function offsetExists($enumerator)
{
try {
$enumeration = $this->enumeration;
$ord = $enumeration::get($enumerator)->getOrdinal();
return isset($this->map[$ord]);
} catch (InvalidArgumentException $e) {
// An invalid enumerator can't be an offset of this map
return false;
}
}
/**
* Get mapped data for the given enumerator
* @param Enum|null|boolean|int|float|string $enumerator
* @return mixed
* @throws InvalidArgumentException On an invalid given enumerator
*/
public function offsetGet($enumerator)
{
$enumeration = $this->enumeration;
$ord = $enumeration::get($enumerator)->getOrdinal();
if (!isset($this->map[$ord]) && !array_key_exists($ord, $this->map)) {
throw new UnexpectedValueException(\sprintf(
"Enumerator '%s' could not be found",
\is_object($enumerator) ? $enumerator->getValue() : $enumerator
));
}
return $this->map[$ord];
}
/**
* Attach a new enumerator or overwrite an existing one
* @param Enum|null|boolean|int|float|string $enumerator
* @param mixed $value
* @return void
* @throws InvalidArgumentException On an invalid given enumerator
* @see attach()
*/
public function offsetSet($enumerator, $value = null)
{
$enumeration = $this->enumeration;
$ord = $enumeration::get($enumerator)->getOrdinal();
if (!array_key_exists($ord, $this->map)) {
$this->ordinals[] = $ord;
}
$this->map[$ord] = $value;
}
/**
* Detach an existing enumerator
* @param Enum|null|boolean|int|float|string $enumerator
* @return void
* @throws InvalidArgumentException On an invalid given enumerator
* @see detach()
*/
public function offsetUnset($enumerator)
{
$enumeration = $this->enumeration;
$ord = $enumeration::get($enumerator)->getOrdinal();
if (($idx = \array_search($ord, $this->ordinals, true)) !== false) {
unset($this->map[$ord], $this->ordinals[$idx]);
$this->ordinals = \array_values($this->ordinals);
}
}
/**
* Seeks to the given iterator position.
* @param int $pos
*/
public function seek($pos)
{
$pos = (int)$pos;
if (!isset($this->ordinals[$pos])) {
throw new OutOfBoundsException("Position {$pos} not found");
}
$this->pos = $pos;
}
/**
* Get the current value
* @return mixed
*/
public function current()
{
if (!isset($this->ordinals[$this->pos])) {
return null;
}
return $this->map[$this->ordinals[$this->pos]];
}
/**
* Get the current key
* @return Enum|null
*/
public function key()
{
if (!isset($this->ordinals[$this->pos])) {
return null;
}
$enumeration = $this->enumeration;
return $enumeration::byOrdinal($this->ordinals[$this->pos]);
}
/**
* Reset the iterator position to zero.
* @return void
*/
public function rewind()
{
$this->pos = 0;
}
/**
* Increment the iterator position by one.
* @return void
*/
public function next()
{
++$this->pos;
}
/**
* Test if the iterator is in a valid state
* @return boolean
*/
public function valid()
{
return isset($this->ordinals[$this->pos]);
}
/**
* Count the number of elements
*
* @return int
*/
public function count()
{
return \count($this->ordinals);
}
}
|