Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ZipEntryMatcher.php « Model « PhpZip « src « zip « nelexa « vendor - github.com/CarnetApp/CarnetNextcloud.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7ce6406173a084ed2525206b991fb3a43cc0ee93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php

namespace PhpZip\Model;

/**
 * @author Ne-Lexa alexey@nelexa.ru
 * @license MIT
 */
class ZipEntryMatcher implements \Countable
{
    /**
     * @var ZipModel
     */
    protected $zipModel;

    /**
     * @var array
     */
    protected $matches = [];

    /**
     * ZipEntryMatcher constructor.
     * @param ZipModel $zipModel
     */
    public function __construct(ZipModel $zipModel)
    {
        $this->zipModel = $zipModel;
    }

    /**
     * @param string|array $entries
     * @return ZipEntryMatcher
     */
    public function add($entries)
    {
        $entries = (array)$entries;
        $entries = array_map(function ($entry) {
            return $entry instanceof ZipEntry ? $entry->getName() : $entry;
        }, $entries);
        $this->matches = array_unique(
            array_merge(
                $this->matches,
                array_keys(
                    array_intersect_key(
                        $this->zipModel->getEntries(),
                        array_flip($entries)
                    )
                )
            )
        );
        return $this;
    }

    /**
     * @param string $regexp
     * @return ZipEntryMatcher
     */
    public function match($regexp)
    {
        array_walk($this->zipModel->getEntries(), function (
            /** @noinspection PhpUnusedParameterInspection */
            $entry,
            $entryName
        ) use ($regexp) {
            if (preg_match($regexp, $entryName)) {
                $this->matches[] = $entryName;
            }
        });
        $this->matches = array_unique($this->matches);
        return $this;
    }

    /**
     * @return ZipEntryMatcher
     */
    public function all()
    {
        $this->matches = array_keys($this->zipModel->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, function ($entryName) use ($callable) {
                call_user_func($callable, $entryName);
            });
        }
    }

    /**
     * @return array
     */
    public function getMatches()
    {
        return $this->matches;
    }

    public function delete()
    {
        array_walk($this->matches, function ($entry) {
            $this->zipModel->deleteEntry($entry);
        });
        $this->matches = [];
    }

    /**
     * @param string|null $password
     * @param int|null $encryptionMethod
     */
    public function setPassword($password, $encryptionMethod = null)
    {
        array_walk($this->matches, function ($entry) use ($password, $encryptionMethod) {
            $entry = $this->zipModel->getEntry($entry);
            if (!$entry->isDirectory()) {
                $this->zipModel->getEntryForChanges($entry)->setPassword($password, $encryptionMethod);
            }
        });
    }

    /**
     * @param int $encryptionMethod
     */
    public function setEncryptionMethod($encryptionMethod)
    {
        array_walk($this->matches, function ($entry) use ($encryptionMethod) {
            $entry = $this->zipModel->getEntry($entry);
            if (!$entry->isDirectory()) {
                $this->zipModel->getEntryForChanges($entry)->setEncryptionMethod($encryptionMethod);
            }
        });
    }

    public function disableEncryption()
    {
        array_walk($this->matches, function ($entry) {
            $entry = $this->zipModel->getEntry($entry);
            if (!$entry->isDirectory()) {
                $entry = $this->zipModel->getEntryForChanges($entry);
                $entry->clearEncryption();
            }
        });
    }

    /**
     * Count elements of an object
     * @link http://php.net/manual/en/countable.count.php
     * @return int The custom count as an integer.
     * </p>
     * <p>
     * The return value is cast to an integer.
     * @since 5.1.0
     */
    public function count()
    {
        return count($this->matches);
    }
}