ZipEntryMatcher.php 4.1 KB

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