ZipEntryMatcher.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 ZipModel */
  10. protected $zipModel;
  11. /** @var array */
  12. protected $matches = [];
  13. /**
  14. * ZipEntryMatcher constructor.
  15. *
  16. * @param ZipModel $zipModel
  17. */
  18. public function __construct(ZipModel $zipModel)
  19. {
  20. $this->zipModel = $zipModel;
  21. }
  22. /**
  23. * @param string|array $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->zipModel->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. public function match($regexp)
  60. {
  61. array_walk(
  62. $this->zipModel->getEntries(),
  63. function (
  64. /** @noinspection PhpUnusedParameterInspection */
  65. $entry,
  66. $entryName
  67. ) use ($regexp) {
  68. if (preg_match($regexp, $entryName)) {
  69. $this->matches[] = (string) $entryName;
  70. }
  71. }
  72. );
  73. $this->matches = array_unique($this->matches);
  74. return $this;
  75. }
  76. /**
  77. * @return ZipEntryMatcher
  78. */
  79. public function all()
  80. {
  81. $this->matches = array_keys($this->zipModel->getEntries());
  82. return $this;
  83. }
  84. /**
  85. * Callable function for all select entries.
  86. *
  87. * Callable function signature:
  88. * function(string $entryName){}
  89. *
  90. * @param callable $callable
  91. */
  92. public function invoke(callable $callable)
  93. {
  94. if (!empty($this->matches)) {
  95. array_walk(
  96. $this->matches,
  97. static function ($entryName) use ($callable) {
  98. $callable($entryName);
  99. }
  100. );
  101. }
  102. }
  103. /**
  104. * @return array
  105. */
  106. public function getMatches()
  107. {
  108. return $this->matches;
  109. }
  110. public function delete()
  111. {
  112. array_walk(
  113. $this->matches,
  114. function ($entry) {
  115. $this->zipModel->deleteEntry($entry);
  116. }
  117. );
  118. $this->matches = [];
  119. }
  120. /**
  121. * @param string|null $password
  122. * @param int|null $encryptionMethod
  123. */
  124. public function setPassword($password, $encryptionMethod = null)
  125. {
  126. array_walk(
  127. $this->matches,
  128. function ($entry) use ($password, $encryptionMethod) {
  129. $entry = $this->zipModel->getEntry($entry);
  130. if (!$entry->isDirectory()) {
  131. $this->zipModel->getEntryForChanges($entry)->setPassword($password, $encryptionMethod);
  132. }
  133. }
  134. );
  135. }
  136. /**
  137. * @param int $encryptionMethod
  138. */
  139. public function setEncryptionMethod($encryptionMethod)
  140. {
  141. array_walk(
  142. $this->matches,
  143. function ($entry) use ($encryptionMethod) {
  144. $entry = $this->zipModel->getEntry($entry);
  145. if (!$entry->isDirectory()) {
  146. $this->zipModel->getEntryForChanges($entry)->setEncryptionMethod($encryptionMethod);
  147. }
  148. }
  149. );
  150. }
  151. public function disableEncryption()
  152. {
  153. array_walk(
  154. $this->matches,
  155. function ($entry) {
  156. $entry = $this->zipModel->getEntry($entry);
  157. if (!$entry->isDirectory()) {
  158. $entry = $this->zipModel->getEntryForChanges($entry);
  159. $entry->disableEncryption();
  160. }
  161. }
  162. );
  163. }
  164. /**
  165. * Count elements of an object.
  166. *
  167. * @see http://php.net/manual/en/countable.count.php
  168. *
  169. * @return int The custom count as an integer.
  170. * </p>
  171. * <p>
  172. * The return value is cast to an integer.
  173. *
  174. * @since 5.1.0
  175. */
  176. public function count()
  177. {
  178. return \count($this->matches);
  179. }
  180. }