Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a2dbbd1

Browse files
committed
[Finder] Allow arrays as method parameters
1 parent 4849c01 commit a2dbbd1

File tree

2 files changed

+100
-27
lines changed

2 files changed

+100
-27
lines changed

src/Symfony/Component/Finder/Finder.php

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,20 @@ public function files()
107107
*
108108
* $finder->depth('> 1') // the Finder will start matching at level 1.
109109
* $finder->depth('< 3') // the Finder will descend at most 3 levels of directories below the starting point.
110+
* $finder->depth(['>= 1', '< 3'])
110111
*
111-
* @param string|int $level The depth level expression
112+
* @param string|int|string[]|int[] $levels The depth level expression or an array of depth levels
112113
*
113114
* @return $this
114115
*
115116
* @see DepthRangeFilterIterator
116117
* @see NumberComparator
117118
*/
118-
public function depth($level)
119+
public function depth($levels)
119120
{
120-
$this->depths[] = new Comparator\NumberComparator($level);
121+
foreach ((array) $levels as $level) {
122+
$this->depths[] = new Comparator\NumberComparator($level);
123+
}
121124

122125
return $this;
123126
}
@@ -131,18 +134,21 @@ public function depth($level)
131134
* $finder->date('until 2 days ago');
132135
* $finder->date('> now - 2 hours');
133136
* $finder->date('>= 2005-10-15');
137+
* $finder->date(['>= 2005-10-15', '>= 2005-05-27']);
134138
*
135-
* @param string $date A date range string
139+
* @param string|string[] $dates A date range string or an array of date ranges
136140
*
137141
* @return $this
138142
*
139143
* @see strtotime
140144
* @see DateRangeFilterIterator
141145
* @see DateComparator
142146
*/
143-
public function date($date)
147+
public function date($dates)
144148
{
145-
$this->dates[] = new Comparator\DateComparator($date);
149+
foreach ((array) $dates as $date) {
150+
$this->dates[] = new Comparator\DateComparator($date);
151+
}
146152

147153
return $this;
148154
}
@@ -155,32 +161,33 @@ public function date($date)
155161
* $finder->name('*.php')
156162
* $finder->name('/\.php$/') // same as above
157163
* $finder->name('test.php')
164+
* $finder->name(['test.py', 'test.php'])
158165
*
159-
* @param string $pattern A pattern (a regexp, a glob, or a string)
166+
* @param string|string[] $patterns A pattern (a regexp, a glob, or a string) or an array of patterns
160167
*
161168
* @return $this
162169
*
163170
* @see FilenameFilterIterator
164171
*/
165-
public function name($pattern)
172+
public function name($patterns)
166173
{
167-
$this->names[] = $pattern;
174+
$this->names = \array_merge($this->names, (array) $patterns);
168175

169176
return $this;
170177
}
171178

172179
/**
173180
* Adds rules that files must not match.
174181
*
175-
* @param string $pattern A pattern (a regexp, a glob, or a string)
182+
* @param string|string[] $patterns A pattern (a regexp, a glob, or a string) or an array of patterns
176183
*
177184
* @return $this
178185
*
179186
* @see FilenameFilterIterator
180187
*/
181-
public function notName($pattern)
188+
public function notName($patterns)
182189
{
183-
$this->notNames[] = $pattern;
190+
$this->notNames = \array_merge($this->notNames, (array) $patterns);
184191

185192
return $this;
186193
}
@@ -192,16 +199,17 @@ public function notName($pattern)
192199
*
193200
* $finder->contains('Lorem ipsum')
194201
* $finder->contains('/Lorem ipsum/i')
202+
* $finder->contains(['dolor', '/ipsum/i'])
195203
*
196-
* @param string $pattern A pattern (string or regexp)
204+
* @param string|string[] $patterns A pattern (string or regexp) or an array of patterns
197205
*
198206
* @return $this
199207
*
200208
* @see FilecontentFilterIterator
201209
*/
202-
public function contains($pattern)
210+
public function contains($patterns)
203211
{
204-
$this->contains[] = $pattern;
212+
$this->contains = \array_merge($this->contains, (array) $patterns);
205213

206214
return $this;
207215
}
@@ -213,16 +221,17 @@ public function contains($pattern)
213221
*
214222
* $finder->notContains('Lorem ipsum')
215223
* $finder->notContains('/Lorem ipsum/i')
224+
* $finder->notContains(['lorem', '/dolor/i'])
216225
*
217-
* @param string $pattern A pattern (string or regexp)
226+
* @param string|string[] $patterns A pattern (string or regexp) or an array of patterns
218227
*
219228
* @return $this
220229
*
221230
* @see FilecontentFilterIterator
222231
*/
223-
public function notContains($pattern)
232+
public function notContains($patterns)
224233
{
225-
$this->notContains[] = $pattern;
234+
$this->notContains = \array_merge($this->notContains, (array) $patterns);
226235

227236
return $this;
228237
}
@@ -234,18 +243,19 @@ public function notContains($pattern)
234243
*
235244
* $finder->path('some/special/dir')
236245
* $finder->path('/some\/special\/dir/') // same as above
246+
* $finder->path(['some dir', 'another/dir'])
237247
*
238248
* Use only / as dirname separator.
239249
*
240-
* @param string $pattern A pattern (a regexp or a string)
250+
* @param string|string[] $patterns A pattern (a regexp or a string) or an array of patterns
241251
*
242252
* @return $this
243253
*
244254
* @see FilenameFilterIterator
245255
*/
246-
public function path($pattern)
256+
public function path($patterns)
247257
{
248-
$this->paths[] = $pattern;
258+
$this->paths = \array_merge($this->paths, (array) $patterns);
249259

250260
return $this;
251261
}
@@ -257,18 +267,19 @@ public function path($pattern)
257267
*
258268
* $finder->notPath('some/special/dir')
259269
* $finder->notPath('/some\/special\/dir/') // same as above
270+
* $finder->notPath(['some/file.txt', 'another/file.log'])
260271
*
261272
* Use only / as dirname separator.
262273
*
263-
* @param string $pattern A pattern (a regexp or a string)
274+
* @param string|string[] $patterns A pattern (a regexp or a string) or an array of patterns
264275
*
265276
* @return $this
266277
*
267278
* @see FilenameFilterIterator
268279
*/
269-
public function notPath($pattern)
280+
public function notPath($patterns)
270281
{
271-
$this->notPaths[] = $pattern;
282+
$this->notPaths = \array_merge($this->notPaths, (array) $patterns);
272283

273284
return $this;
274285
}
@@ -279,17 +290,20 @@ public function notPath($pattern)
279290
* $finder->size('> 10K');
280291
* $finder->size('<= 1Ki');
281292
* $finder->size(4);
293+
* $finder->size(['> 10K', '< 20K'])
282294
*
283-
* @param string|int $size A size range string or an integer
295+
* @param string|int|string[]|int[] $sizes A size range string or an integer or an array of size ranges
284296
*
285297
* @return $this
286298
*
287299
* @see SizeRangeFilterIterator
288300
* @see NumberComparator
289301
*/
290-
public function size($size)
302+
public function size($sizes)
291303
{
292-
$this->sizes[] = new Comparator\NumberComparator($size);
304+
foreach ((array) $sizes as $size) {
305+
$this->sizes[] = new Comparator\NumberComparator($size);
306+
}
293307

294308
return $this;
295309
}

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', 'dolor'), array(), array('lorem.txt', 'ipsum.txt', 'dolor.txt')),
1043+
array('', array('lorem', 'ipsum'), array('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)