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