Ne-Lexa /
php-zip
| 1 | <?php |
||
| 2 | |||
| 3 | namespace PhpZip\Model\Extra; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Represents a collection of Extra Fields as they may |
||
| 7 | * be present at several locations in ZIP files. |
||
| 8 | */ |
||
| 9 | class ExtraFieldsCollection implements \ArrayAccess, \Countable, \Iterator |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * The map of Extra Fields. |
||
| 13 | * Maps from Header ID to Extra Field. |
||
| 14 | * Must not be null, but may be empty if no Extra Fields are used. |
||
| 15 | * The map is sorted by Header IDs in ascending order. |
||
| 16 | * |
||
| 17 | * @var ZipExtraField[] |
||
| 18 | */ |
||
| 19 | protected $collection = []; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Returns the number of Extra Fields in this collection. |
||
| 23 | * |
||
| 24 | * @return int |
||
| 25 | */ |
||
| 26 | 2 | public function count() |
|
| 27 | { |
||
| 28 | 2 | return \count($this->collection); |
|
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Returns the Extra Field with the given Header ID or null |
||
| 33 | * if no such Extra Field exists. |
||
| 34 | * |
||
| 35 | * @param int $headerId the requested Header ID |
||
| 36 | * |
||
| 37 | * @return ZipExtraField|null the Extra Field with the given Header ID or |
||
| 38 | * if no such Extra Field exists |
||
| 39 | */ |
||
| 40 | 145 | public function get($headerId) |
|
| 41 | { |
||
| 42 | 145 | $this->validateHeaderId($headerId); |
|
| 43 | |||
| 44 | 145 | return isset($this->collection[$headerId]) ? $this->collection[$headerId] : null; |
|
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param int $headerId |
||
| 49 | */ |
||
| 50 | 160 | private function validateHeaderId($headerId) |
|
| 51 | { |
||
| 52 | 160 | if ($headerId < 0 || $headerId > 0xffff) { |
|
| 53 | throw new \InvalidArgumentException('$headerId out of range'); |
||
| 54 | } |
||
| 55 | 160 | } |
|
| 56 | |||
| 57 | /** |
||
| 58 | * Stores the given Extra Field in this collection. |
||
| 59 | * |
||
| 60 | * @param ZipExtraField $extraField the Extra Field to store in this collection |
||
| 61 | * |
||
| 62 | * @return ZipExtraField the Extra Field previously associated with the Header ID of |
||
| 63 | * of the given Extra Field or null if no such Extra Field existed |
||
| 64 | */ |
||
| 65 | 31 | public function add(ZipExtraField $extraField) |
|
| 66 | { |
||
| 67 | 31 | $headerId = $extraField->getHeaderId(); |
|
| 68 | |||
| 69 | 31 | $this->validateHeaderId($headerId); |
|
| 70 | 31 | $this->collection[$headerId] = $extraField; |
|
| 71 | |||
| 72 | 31 | return $extraField; |
|
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param ZipExtraField[] $extraFields |
||
| 77 | */ |
||
| 78 | 5 | public function addAll(array $extraFields) |
|
| 79 | { |
||
| 80 | 5 | foreach ($extraFields as $extraField) { |
|
| 81 | 5 | $this->add($extraField); |
|
| 82 | } |
||
| 83 | 5 | } |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @param ExtraFieldsCollection $collection |
||
| 87 | */ |
||
| 88 | 5 | public function addCollection(self $collection) |
|
| 89 | { |
||
| 90 | 5 | $this->addAll($collection->collection); |
|
| 91 | 5 | } |
|
| 92 | |||
| 93 | /** |
||
| 94 | * @return ZipExtraField[] |
||
| 95 | */ |
||
| 96 | 1 | public function getAll() |
|
| 97 | { |
||
| 98 | 1 | return $this->collection; |
|
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Returns Extra Field exists. |
||
| 103 | * |
||
| 104 | * @param int $headerId the requested Header ID |
||
| 105 | * |
||
| 106 | * @return bool |
||
| 107 | */ |
||
| 108 | 4 | public function has($headerId) |
|
| 109 | { |
||
| 110 | 4 | return isset($this->collection[$headerId]); |
|
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Removes the Extra Field with the given Header ID. |
||
| 115 | * |
||
| 116 | * @param int $headerId the requested Header ID |
||
| 117 | * |
||
| 118 | * @return ZipExtraField|null the Extra Field with the given Header ID or null |
||
| 119 | * if no such Extra Field exists |
||
| 120 | */ |
||
| 121 | 143 | public function remove($headerId) |
|
| 122 | { |
||
| 123 | 143 | $this->validateHeaderId($headerId); |
|
| 124 | |||
| 125 | 143 | if (isset($this->collection[$headerId])) { |
|
| 126 | 4 | $ef = $this->collection[$headerId]; |
|
| 127 | 4 | unset($this->collection[$headerId]); |
|
| 128 | |||
| 129 | 4 | return $ef; |
|
| 130 | } |
||
| 131 | |||
| 132 | 141 | return null; |
|
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Whether a offset exists. |
||
| 137 | * |
||
| 138 | * @see http://php.net/manual/en/arrayaccess.offsetexists.php |
||
| 139 | * |
||
| 140 | * @param int $offset an offset to check for |
||
| 141 | * |
||
| 142 | * @return bool true on success or false on failure |
||
| 143 | */ |
||
| 144 | 19 | public function offsetExists($offset) |
|
| 145 | { |
||
| 146 | 19 | return isset($this->collection[(int) $offset]); |
|
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Offset to retrieve. |
||
| 151 | * |
||
| 152 | * @see http://php.net/manual/en/arrayaccess.offsetget.php |
||
| 153 | * |
||
| 154 | * @param int $offset the offset to retrieve |
||
| 155 | * |
||
| 156 | * @return ZipExtraField|null |
||
| 157 | */ |
||
| 158 | 154 | public function offsetGet($offset) |
|
| 159 | { |
||
| 160 | 154 | return isset($this->collection[$offset]) ? $this->collection[$offset] : null; |
|
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Offset to set. |
||
| 165 | * |
||
| 166 | * @see http://php.net/manual/en/arrayaccess.offsetset.php |
||
| 167 | * |
||
| 168 | * @param mixed $offset the offset to assign the value to |
||
| 169 | * @param ZipExtraField $value the value to set |
||
| 170 | */ |
||
| 171 | public function offsetSet($offset, $value) |
||
| 172 | { |
||
| 173 | if (!$value instanceof ZipExtraField) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 174 | throw new \InvalidArgumentException('value is not instanceof ' . ZipExtraField::class); |
||
| 175 | } |
||
| 176 | $this->add($value); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Offset to unset. |
||
| 181 | * |
||
| 182 | * @see http://php.net/manual/en/arrayaccess.offsetunset.php |
||
| 183 | * |
||
| 184 | * @param mixed $offset the offset to unset |
||
| 185 | */ |
||
| 186 | public function offsetUnset($offset) |
||
| 187 | { |
||
| 188 | $this->remove($offset); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Return the current element. |
||
| 193 | * |
||
| 194 | * @see http://php.net/manual/en/iterator.current.php |
||
| 195 | * |
||
| 196 | * @return ZipExtraField |
||
| 197 | */ |
||
| 198 | 15 | public function current() |
|
| 199 | { |
||
| 200 | 15 | return current($this->collection); |
|
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Move forward to next element. |
||
| 205 | * |
||
| 206 | * @see http://php.net/manual/en/iterator.next.php |
||
| 207 | */ |
||
| 208 | 15 | public function next() |
|
| 209 | { |
||
| 210 | 15 | next($this->collection); |
|
| 211 | 15 | } |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Return the key of the current element. |
||
| 215 | * |
||
| 216 | * @see http://php.net/manual/en/iterator.key.php |
||
| 217 | * |
||
| 218 | * @return int scalar on success, or null on failure |
||
| 219 | */ |
||
| 220 | public function key() |
||
| 221 | { |
||
| 222 | return key($this->collection); |
||
|
0 ignored issues
–
show
|
|||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Checks if current position is valid. |
||
| 227 | * |
||
| 228 | * @see http://php.net/manual/en/iterator.valid.php |
||
| 229 | * |
||
| 230 | * @return bool The return value will be casted to boolean and then evaluated. |
||
| 231 | * Returns true on success or false on failure. |
||
| 232 | */ |
||
| 233 | 134 | public function valid() |
|
| 234 | { |
||
| 235 | 134 | return key($this->collection) !== null; |
|
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Rewind the Iterator to the first element. |
||
| 240 | * |
||
| 241 | * @see http://php.net/manual/en/iterator.rewind.php |
||
| 242 | */ |
||
| 243 | 134 | public function rewind() |
|
| 244 | { |
||
| 245 | 134 | reset($this->collection); |
|
| 246 | 134 | } |
|
| 247 | |||
| 248 | public function clear() |
||
| 249 | { |
||
| 250 | $this->collection = []; |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | public function __toString() |
||
| 257 | { |
||
| 258 | $formats = []; |
||
| 259 | |||
| 260 | foreach ($this->collection as $key => $value) { |
||
| 261 | $formats[] = (string) $value; |
||
| 262 | } |
||
| 263 | |||
| 264 | return implode("\n", $formats); |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * If clone extra fields. |
||
| 269 | */ |
||
| 270 | 147 | public function __clone() |
|
| 271 | { |
||
| 272 | 147 | foreach ($this->collection as $k => $v) { |
|
| 273 | 20 | $this->collection[$k] = clone $v; |
|
| 274 | } |
||
| 275 | 147 | } |
|
| 276 | } |
||
| 277 |