ExtraFieldsCollection.php 6.6 KB

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