ZipEntryMatcher.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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;
  10. use PhpZip\Exception\ZipEntryNotFoundException;
  11. class ZipEntryMatcher implements \Countable
  12. {
  13. protected ZipContainer $zipContainer;
  14. protected array $matches = [];
  15. public function __construct(ZipContainer $zipContainer)
  16. {
  17. $this->zipContainer = $zipContainer;
  18. }
  19. /**
  20. * @param string|ZipEntry|string[]|ZipEntry[] $entries
  21. *
  22. * @return ZipEntryMatcher
  23. */
  24. public function add($entries): self
  25. {
  26. $entries = (array) $entries;
  27. $entries = array_map(
  28. static fn ($entry) => $entry instanceof ZipEntry ? $entry->getName() : (string) $entry,
  29. $entries
  30. );
  31. $this->matches = array_values(
  32. array_map(
  33. 'strval',
  34. array_unique(
  35. array_merge(
  36. $this->matches,
  37. array_keys(
  38. array_intersect_key(
  39. $this->zipContainer->getEntries(),
  40. array_flip($entries)
  41. )
  42. )
  43. )
  44. )
  45. )
  46. );
  47. return $this;
  48. }
  49. /**
  50. * @return ZipEntryMatcher
  51. * @noinspection PhpUnusedParameterInspection
  52. */
  53. public function match(string $regexp): self
  54. {
  55. array_walk(
  56. $this->zipContainer->getEntries(),
  57. function (ZipEntry $entry, string $entryName) use ($regexp): void {
  58. if (preg_match($regexp, $entryName)) {
  59. $this->matches[] = $entryName;
  60. }
  61. }
  62. );
  63. $this->matches = array_unique($this->matches);
  64. return $this;
  65. }
  66. /**
  67. * @return ZipEntryMatcher
  68. */
  69. public function all(): self
  70. {
  71. $this->matches = array_map(
  72. 'strval',
  73. array_keys($this->zipContainer->getEntries())
  74. );
  75. return $this;
  76. }
  77. /**
  78. * Callable function for all select entries.
  79. *
  80. * Callable function signature:
  81. * function(string $entryName){}
  82. */
  83. public function invoke(callable $callable): void
  84. {
  85. if (!empty($this->matches)) {
  86. array_walk(
  87. $this->matches,
  88. /** @param string $entryName */
  89. static function (string $entryName) use ($callable): void {
  90. $callable($entryName);
  91. }
  92. );
  93. }
  94. }
  95. public function getMatches(): array
  96. {
  97. return $this->matches;
  98. }
  99. public function delete(): void
  100. {
  101. array_walk(
  102. $this->matches,
  103. /** @param string $entryName */
  104. function (string $entryName): void {
  105. $this->zipContainer->deleteEntry($entryName);
  106. }
  107. );
  108. $this->matches = [];
  109. }
  110. /**
  111. * @param ?string $password
  112. * @param ?int $encryptionMethod
  113. *
  114. * @throws ZipEntryNotFoundException
  115. */
  116. public function setPassword(?string $password, ?int $encryptionMethod = null): void
  117. {
  118. array_walk(
  119. $this->matches,
  120. /** @param string $entryName */
  121. function (string $entryName) use ($password, $encryptionMethod): void {
  122. $entry = $this->zipContainer->getEntry($entryName);
  123. if (!$entry->isDirectory()) {
  124. $entry->setPassword($password, $encryptionMethod);
  125. }
  126. }
  127. );
  128. }
  129. /**
  130. * @throws ZipEntryNotFoundException
  131. */
  132. public function setEncryptionMethod(int $encryptionMethod): void
  133. {
  134. array_walk(
  135. $this->matches,
  136. /** @param string $entryName */
  137. function (string $entryName) use ($encryptionMethod): void {
  138. $entry = $this->zipContainer->getEntry($entryName);
  139. if (!$entry->isDirectory()) {
  140. $entry->setEncryptionMethod($encryptionMethod);
  141. }
  142. }
  143. );
  144. }
  145. /**
  146. * @throws ZipEntryNotFoundException
  147. */
  148. public function disableEncryption(): void
  149. {
  150. array_walk(
  151. $this->matches,
  152. function (string $entryName): void {
  153. $entry = $this->zipContainer->getEntry($entryName);
  154. if (!$entry->isDirectory()) {
  155. $entry->disableEncryption();
  156. }
  157. }
  158. );
  159. }
  160. /**
  161. * Count elements of an object.
  162. *
  163. * @see http://php.net/manual/en/countable.count.php
  164. *
  165. * @return int the custom count as an integer
  166. */
  167. public function count(): int
  168. {
  169. return \count($this->matches);
  170. }
  171. }