123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <?php
- namespace PhpZip\Model;
- /**
- * @author Ne-Lexa alexey@nelexa.ru
- * @license MIT
- */
- class ZipEntryMatcher implements \Countable
- {
- /** @var ZipContainer */
- protected $zipContainer;
- /** @var array */
- protected $matches = [];
- /**
- * ZipEntryMatcher constructor.
- *
- * @param ZipContainer $zipContainer
- */
- public function __construct(ZipContainer $zipContainer)
- {
- $this->zipContainer = $zipContainer;
- }
- /**
- * @param string|ZipEntry|string[]|ZipEntry[] $entries
- *
- * @return ZipEntryMatcher
- */
- public function add($entries)
- {
- $entries = (array) $entries;
- $entries = array_map(
- static function ($entry) {
- return $entry instanceof ZipEntry ? $entry->getName() : (string) $entry;
- },
- $entries
- );
- $this->matches = array_values(
- array_map(
- 'strval',
- array_unique(
- array_merge(
- $this->matches,
- array_keys(
- array_intersect_key(
- $this->zipContainer->getEntries(),
- array_flip($entries)
- )
- )
- )
- )
- )
- );
- return $this;
- }
- /**
- * @param string $regexp
- *
- * @return ZipEntryMatcher
- *
- * @noinspection PhpUnusedParameterInspection
- */
- public function match($regexp)
- {
- array_walk(
- $this->zipContainer->getEntries(),
- /**
- * @param ZipEntry $entry
- * @param string $entryName
- */
- function (ZipEntry $entry, $entryName) use ($regexp) {
- if (preg_match($regexp, $entryName)) {
- $this->matches[] = (string) $entryName;
- }
- }
- );
- $this->matches = array_unique($this->matches);
- return $this;
- }
- /**
- * @return ZipEntryMatcher
- */
- public function all()
- {
- $this->matches = array_map(
- 'strval',
- array_keys($this->zipContainer->getEntries())
- );
- return $this;
- }
- /**
- * Callable function for all select entries.
- *
- * Callable function signature:
- * function(string $entryName){}
- *
- * @param callable $callable
- */
- public function invoke(callable $callable)
- {
- if (!empty($this->matches)) {
- array_walk(
- $this->matches,
- /** @param string $entryName */
- static function ($entryName) use ($callable) {
- $callable($entryName);
- }
- );
- }
- }
- /**
- * @return array
- */
- public function getMatches()
- {
- return $this->matches;
- }
- public function delete()
- {
- array_walk(
- $this->matches,
- /** @param string $entryName */
- function ($entryName) {
- $this->zipContainer->deleteEntry($entryName);
- }
- );
- $this->matches = [];
- }
- /**
- * @param string|null $password
- * @param int|null $encryptionMethod
- */
- public function setPassword($password, $encryptionMethod = null)
- {
- array_walk(
- $this->matches,
- /** @param string $entryName */
- function ($entryName) use ($password, $encryptionMethod) {
- $entry = $this->zipContainer->getEntry($entryName);
- if (!$entry->isDirectory()) {
- $entry->setPassword($password, $encryptionMethod);
- }
- }
- );
- }
- /**
- * @param int $encryptionMethod
- */
- public function setEncryptionMethod($encryptionMethod)
- {
- array_walk(
- $this->matches,
- /** @param string $entryName */
- function ($entryName) use ($encryptionMethod) {
- $entry = $this->zipContainer->getEntry($entryName);
- if (!$entry->isDirectory()) {
- $entry->setEncryptionMethod($encryptionMethod);
- }
- }
- );
- }
- public function disableEncryption()
- {
- array_walk(
- $this->matches,
- /** @param string $entryName */
- function ($entryName) {
- $entry = $this->zipContainer->getEntry($entryName);
- if (!$entry->isDirectory()) {
- $entry->disableEncryption();
- }
- }
- );
- }
- /**
- * Count elements of an object.
- *
- * @see http://php.net/manual/en/countable.count.php
- *
- * @return int the custom count as an integer
- *
- * @since 5.1.0
- */
- public function count()
- {
- return \count($this->matches);
- }
- }
|