ZipEntryMatcher.php 4.9 KB

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