ExtraFieldsCollection.php 6.5 KB

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