8000 [Finder] Allow arrays as method parameters · symfony/symfony@daec10f · GitHub
[go: up one dir, main page]

Skip to content

Commit daec10f

Browse files
committed
[Finder] Allow arrays as method parameters
1 parent b226859 commit daec10f

File tree

3 files changed

+157
-27
lines changed

3 files changed

+157
-27
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Finder\Comparator;
13+
14+
/**
15+
* Comparator factory.
16+
*/
17+
class ComparatorFactory
18+
{
19+
/**
20+
* Factory for a Comparator object.
21+
*
22+
* @param string $class Class to instantiate
23+
* @param string|int $parameter Parameter for the constructor
24+
*
25+
* @return Comparator
26+
*/
27+
public static function create(string $class, $parameter): Comparator
28+
{
29+
if (DateComparator::class !== $class && NumberComparator::class !== $class) {
30+
throw new \InvalidArgumentException('Invalid class "%s".', $class);
31+
}
32+
33+
return new $class($parameter);
34+
}
35+
36+
/**
37+
* Create an array of Comparator objects.
38+
*
39+
* @param string $class Class to instantiate
40+
* @param string[] $parameterList Array of parameters for each object constructor
41+
*
42+
* @return Comparator[]
43+
*/
44+
public static function createBatch(string $class, array $parameterList): array
45+
{
46+
return \array_map(
47+
function ($parameter) use ($class) {
48+
return self::create($class, $parameter);
49+
},
50+
$parameterList
51+
);
52+
}
53+
}

src/Symfony/Component/Finder/Finder.php

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Finder;
1313

14+
use Symfony\Component\Finder\Comparator\ComparatorFactory;
1415
use Symfony\Component\Finder\Comparator\DateComparator;
1516
use Symfony\Component\Finder\Comparator\NumberComparator;
1617
use Symfony\Component\Finder\Iterator\CustomFilterIterator;
@@ -107,17 +108,21 @@ public function files()
107108
*
108109
* $finder->depth('> 1') // the Finder will start matching at level 1.
109110
* $finder->depth('< 3') // the Finder will descend at most 3 levels of directories below the starting point.
111+
* $finder->depth(['>= 1', '< 3'])
110112
*
111-
* @param string|int $level The depth level expression
113+
* @param string|int|string[]|int[] $levels The depth level expression or an array of depth levels
112114
*
113115
* @return $this
114116
*
115117
* @see DepthRangeFilterIterator
116118
* @see NumberComparator
117119
*/
118-
public function depth($level)
120+
public function depth($levels)
119121
{
120-
$this->depths[] = new Comparator\NumberComparator($level);
122+
$this->depths = \array_merge(
123+
$this->depths,
124+
ComparatorFactory::createBatch(NumberComparator::class, (array) $levels)
125+
);
121126

122127
return $this;
123128
}
@@ -131,18 +136,22 @@ public function depth($level)
131136
* $finder->date('until 2 days ago');
132137
* $finder->date('> now - 2 hours');
133138
* $finder->date('>= 2005-10-15');
139+
* $finder->date(['>= 2005-10-15', '>= 2005-05-27']);
134140
*
135-
* @param string $date A date range string
141+
* @param string|string[] $dates A date range string or an array of date ranges
136142
*
137143
* @return $this
138144
*
139145
* @see strtotime
140146
* @see DateRangeFilterIterator
141147
* @see DateComparator
142148
*/
143-
public function date($date)
149+
public function date($dates)
144150
{
145-
$this->dates[] = new Comparator\DateComparator($date);
151+
$this->dates = \array_merge(
152+
$this->dates,
153+
ComparatorFactory::createBatch(DateComparator::class, (array) $dates)
154+
);
146155

147156
return $this;
148157
}
@@ -155,32 +164,33 @@ public function date($date)
155164
* $finder->name('*.php')
156165
* $finder->name('/\.php$/') // same as above
157166
* $finder->name('test.php')
167+
* $finder->name(['test.py', 'test.php'])
158168
*
159-
* @param string $pattern A pattern (a regexp, a glob, or a string)
169+
* @param string|string[] $patterns A pattern (a regexp, a glob, or a string) or an array of patterns
160170
*
161171
* @return $this
162172
*
163173
* @see FilenameFilterIterator
164174
*/
165-
public function name($pattern)
175+
public function name($patterns)
166176
{
167-
$this->names[] = $pattern;
177+
$this->names = \array_merge($this->names, (array) $patterns);
168178

169179
return $this;
170180
}
171181

172182
/**
173183
* Adds rules that files must not match.
174184
*
175-
* @param string $pattern A pattern (a regexp, a glob, or a string)
185+
* @param string|string[] $patterns A pattern (a regexp, a glob, or a string) or an array of patterns
176186
*
177187
* @return $this
178188
*
179189
* @see FilenameFilterIterator
180190
*/
181-
public function notName($pattern)
191+
public function notName($patterns)
182192
{
183-
$this->notNames[] = $pattern;
193+
$this->notNames = \array_merge($this->notNames, (array) $patterns);
184194

185195
return $this;
186196
}
@@ -192,16 +202,17 @@ public function notName($pattern)
192202
*
193203
* $finder->contains('Lorem ipsum')
194204
* $finder->contains('/Lorem ipsum/i')
205+
* $finder->contains(['dolor', '/ipsum/i'])
195206
*
196-
* @param string $pattern A pattern (string or regexp)
207+
* @param string|string[] $patterns A pattern (string or regexp) or an array of patterns
197208
*
198209
* @return $this
199210
*
200211
* @see FilecontentFilterIterator
201212
*/
202-
public function contains($pattern)
213+
public function contains($patterns)
203214
{
204-
$this->contains[] = $pattern;
215+
$this->contains = \array_merge($this->contains, (array) $patterns);
205216

206217
return $this;
207218
}
@@ -213,16 +224,17 @@ public function contains($pattern)
213224
*
214225
* $finder->notContains('Lorem ipsum')
215226
* $finder->notContains('/Lorem ipsum/i')
227+
* $finder->notContains(['lorem', '/dolor/i'])
216228
*
217-
* @param string $pattern A pattern (string or regexp)
229+
* @param string|string[] $patterns A pattern (string or regexp) or an array of patterns
218230
*
219231
* @return $this
220232
*
221233
* @see FilecontentFilterIterator
222234
*/
223-
public function notContains($pattern)
235+
public function notContains($patterns)
224236
{
225-
$this->notContains[] = $pattern;
237+
$this->notContains = \array_merge($this->notContains, (array) $patterns);
226238

227239
return $this;
228240
}
@@ -234,18 +246,19 @@ public function notContains($pattern)
234246
*
235247
* $finder->path('some/special/di A377 r')
236248
* $finder->path('/some\/special\/dir/') // same as above
249+
* $finder->path(['some dir', 'another/dir'])
237250
*
238251
* Use only / as dirname separator.
239252
*
240-
* @param string $pattern A pattern (a regexp or a string)
253+
* @param string|string[] $patterns A pattern (a regexp or a string) or an array of patterns
241254
*
242255
* @return $this
243256
*
244257
* @see FilenameFilterIterator
245258
*/
246-
public function path($pattern)
259+
public function path($patterns)
247260
{
248-
$this->paths[] = $pattern;
261+
$this->paths = \array_merge($this->paths, (array) $patterns);
249262

250263
return $this;
251264
}
@@ -257,18 +270,19 @@ public function path($pattern)
257270
*
258271
* $finder->notPath('some/special/dir')
259272
* $finder->notPath('/some\/special\/dir/') // same as above
273+
* $finder->notPath(['some/file.txt', 'another/file.log'])
260274
*
261275
* Use only / as dirname separator.
262276
*
263-
* @param string $pattern A pattern (a regexp or a string)
277+
* @param string|string[] $patterns A pattern (a regexp or a string) or an array of patterns
264278
*
265279
* @return $this
266280
*
267281
* @see FilenameFilterIterator
268282
*/
269-
public function notPath($pattern)
283+
public function notPath($patterns)
270284
{
271-
$this->notPaths[] = $pattern;
285+
$this->notPaths = \array_merge($this->notPaths, (array) $patterns);
272286

273287
return $this;
274288
}
@@ -279,17 +293,21 @@ public function notPath($pattern)
279293
* $finder->size('> 10K');
280294
* $finder->size('<= 1Ki');
281295
* $finder->size(4);
296+
* $finder->size(['> 10K', '< 20K'])
282297
*
283-
* @param string|int $size A size range string or an integer
298+
* @param string|int|string[]|int[] $sizes A size range string or an integer or an array of size ranges
284299
*
285300
* @return $this
286301
*
287302
* @see SizeRangeFilterIterator
288303
* @see NumberComparator
289304
*/
290-
public function size($size)
305+
public function size($sizes)
291306
{
292-
$this->sizes[] = new Comparator\NumberComparator($size);
307+
$this->sizes = \array_merge(
308+
$this->sizes,
309+
ComparatorFactory::createBatch(NumberComparator::class, (array) $sizes)
310+
);
293311

294312
return $this;
295313
}

src/Symfony/Component/Finder/Tests/FinderTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,14 @@ public function testDepth()
167167
$finder = $this->buildFinder();
168168
$finder->depth('< 1')->depth('>= 1');
169169
$this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
170+
171+
$finder = $this->buildFinder();
172+
$finder->depth(array('>= 1', '< 2'));
173+
$this->assertIterator($this->toAbsolute(array(
174+
'foo/bar.tmp',
175+
'qux/baz_100_1.py',
176+
'qux/baz_1_2.py',
177+
)), $finder->in(self::$tmpDir)->getIterator());
170178
}
171179

172180
public function testName()
@@ -207,6 +215,10 @@ public function testName()
207215
$finder = $this->buildFinder();
208216
$finder->name('test.p{hp,y}');
209217
$this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
218+
219+
$finder = $this->buildFinder();
220+
$finder->name(array('test.php', 'test.py'));
221+
$this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
210222
}
211223

212224
public function testNotName()
@@ -235,6 +247,16 @@ public function testNotName()
235247
'qux',
236248
)), $finder->in(self::$tmpDir)->getIterator());
237249

250+
$finder = $this->buildFinder();
251+
$finder->notName(array('*.php', '*.py'));
252+
$this->assertIterator($this->toAbsolute(array(
253+
'foo',
254+
'foo/bar.tmp',
255+
'toto',
256+
'foo bar',
257+
'qux',
258+
)), $finder->in(self::$tmpDir)->getIterator());
259+
238260
$finder = $this->buildFinder();
239261
$finder->name('test.ph*');
240262
$finder->name('test.py');
@@ -267,13 +289,21 @@ public function testSize()
267289
$finder = $this->buildFinder();
268290
$this->assertSame($finder, $finder->files()->size('< 1K')->size('> 500'));
269291
$this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
292+
293+
$finder = $this->buildFinder();
294+
$this->assertSame($finder, $finder->files()->size(array('< 1K', '> 500')));
295+
$this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
270296
}
271297

272298
public function testDate()
273299
{
274300
$finder = $this->buildFinder();
275301
$this->assertSame($finder, $finder->files()->date('until last month'));
276302
$this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php')), $finder->in(self::$tmpDir)->getIterator());
303+
304+
$finder = $this->buildFinder();
305+
$this->assertSame($finder, $finder->files()->date(array('>= 2005-10-15', 'until last month')));
306+
$this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php')), $finder->in(self::$tmpDir)->getIterator());
277307
}
278308

279309
public function testExclude()
@@ -1009,6 +1039,8 @@ public function getContainsTestData()
10091039
array('lorem', 'foobar', array('lorem.txt')),
10101040
array('', 'lorem', array('dolor.txt', 'ipsum.txt')),
10111041
array('ipsum dolor sit amet', '/^IPSUM/m', array('lorem.txt')),
1042+
array('', array('lorem', 'ipsum'), array('dolor.txt')),
1043+
array(array('lorem', 'dolor'), array(), array('lorem.txt', 'ipsum.txt', 'dolor.txt')),
10121044
);
10131045
}
10141046

@@ -1075,6 +1107,33 @@ public function getTestPathData()
10751107
'with space'.DIRECTORY_SEPARATOR.'foo.txt',
10761108
),
10771109
),
1110+
array(
1111+
'/^A/',
1112+
array('a.dat', 'abc.dat'),
1113+
array(
1114+
'A',
1115+
'A'.DIRECTORY_SEPARATOR.'B',
1116+
'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C',
1117+
'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'ab.dat',
1118+
),
1119+
),
1120+
array(
1121+
array('/^A/', 'one'),
1122+
'foobar',
1123+
array(
1124+
'A',
1125+
'A'.DIRECTORY_SEPARATOR.'B',
1126+
'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C',
1127+
'A'.DIRECTORY_SEPARATOR.'a.dat',
1128+
'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'ab.dat',
1129+
'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C'.DIRECTORY_SEPARATOR.'abc.dat',
1130+
'one',
1131+
'one'.DIRECTORY_SEPARATOR.'a',
1132+
'one'.DIRECTORY_SEPARATOR.'b',
1133+
'one'.DIRECTORY_SEPARATOR.'b'.DIRECTORY_SEPARATOR.'c.neon',
1134+
'one'.DIRECTORY_SEPARATOR.'b'.DIRECTORY_SEPARATOR.'d.neon',
1135+
),
1136+
),
10781137
);
10791138
}
10801139

0 commit comments

Comments
 (0)
0