Skip to content

Commit 14f8930

Browse files
author
Jonathan Gaillard
committed
Merge pull request #43 from nubs/master
Add String::explode and Arrays::flatten filters
2 parents 49094c9 + 6c37f59 commit 14f8930

File tree

6 files changed

+140
-0
lines changed

6 files changed

+140
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ $value = \DominionEnterprises\Filter\Arrays::ofArrays($value, ['id' => [['uint']
152152
Aliased in the filterer as `ofArray`, this filter verifies that the argument is an array that passes the given specification. This is
153153
essentially a flipped version of `Filterer::filter` that allows for testing nested associative arrays.
154154

155+
#### Arrays::flatten
156+
Aliased in the filterer as `flatten`, this filter flattens a multi-dimensional array to a single dimension. The order of values will be
157+
maintained, but the keys themselves will not. For example:
158+
```php
159+
$value = \DominionEnterprises\Filter\Arrays::flatten([[1, 2], [3, [4, 5]]]);
160+
assert($value === [1, 2, 3, 4, 5]);
161+
```
162+
155163
#### Bool::filter
156164
Aliased in the filterer as `bool`, this filter verifies that the argument is a boolean value or a string that maps to one. The second parameter
157165
can be set to `true` to allow null values through without an error (they will stay null and not get converted to false). The last parameters
@@ -188,6 +196,14 @@ The following checks that `$value` is a non-empty string.
188196
\DominionEnterprises\Filter\String::filter($value);
189197
```
190198

199+
#### String::explode
200+
Aliased in the filterer as `explode`, this filter is essentially a wrapper around the built-in [`explode`](http://www.php.net/explode) method
201+
with the value first in order to work with the `Filterer`. It also defaults to using `,` as a delimiter. For example:
202+
```php
203+
$value = \DominionEnterprises\Filter\String::explode('abc,def,ghi');
204+
assert($value === ['abc', 'def', 'ghi']);
205+
```
206+
191207
#### Url::filter
192208
Aliased in the filterer as `url`, this filter verifies that the argument is a URL string according to
193209
[RFC2396](http://www.faqs.org/rfcs/rfc2396).

src/Filter/Arrays.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,29 @@ public static function ofArray(array $value, array $spec)
165165

166166
return $result;
167167
}
168+
169+
/**
170+
* Given a multi-dimensional array, flatten the array to a single level.
171+
*
172+
* The order of the values will be maintained, but the keys will not.
173+
*
174+
* For example, given the array [[1, 2], [3, [4, 5]]], this would result in the array [1, 2, 3, 4, 5].
175+
*
176+
* @param array $value The array to flatten.
177+
*
178+
* @return array The single-dimension array.
179+
*/
180+
public static function flatten(array $value)
181+
{
182+
$result = array();
183+
184+
array_walk_recursive(
185+
$value,
186+
function($item) use(&$result) {
187+
$result[] = $item;
188+
}
189+
);
190+
191+
return $result;
192+
}
168193
}

src/Filter/String.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,29 @@ public static function filter($value, $allowNull = false, $minLength = 1, $maxLe
5757

5858
return $value;
5959
}
60+
61+
/**
62+
* Explodes a string into an array using the given delimiter.
63+
*
64+
* For example, given the string 'foo,bar,baz', this would return the array ['foo', 'bar', 'baz'].
65+
*
66+
* @param string $value The string to explode.
67+
* @param string $delimiter The non-empty delimiter to explode on.
68+
* @return array The exploded values.
69+
*
70+
* @throws \Exception if the value is not a string.
71+
* @throws \InvalidArgumentException if the delimiter does not pass validation.
72+
*/
73+
public static function explode($value, $delimiter = ',')
74+
{
75+
if (!is_string($value)) {
76+
throw new \Exception("Value '" . var_export($value, true) . "' is not a string");
77+
}
78+
79+
if (!is_string($delimiter) || empty($delimiter)) {
80+
throw new \InvalidArgumentException("Delimiter '" . var_export($delimiter, true) . "' is not a non-empty string");
81+
}
82+
83+
return explode($delimiter, $value);
84+
}
6085
}

src/Filterer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ final class Filterer
2323
'ofArray' => '\DominionEnterprises\Filter\Arrays::ofArray',
2424
'url' => '\DominionEnterprises\Filter\Url::filter',
2525
'email' => '\DominionEnterprises\Filter\Email::filter',
26+
'explode' => '\DominionEnterprises\Filter\String::explode',
27+
'flatten' => '\DominionEnterprises\Filter\Arrays::flatten',
2628
);
2729

2830
/**

tests/Filter/ArraysTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,4 +325,15 @@ public function ofArray_unknown()
325325
$this->assertSame($expected, $e->getMessage());
326326
}
327327
}
328+
329+
/**
330+
* Verifies the basic behavior of the flatten filter.
331+
*
332+
* @test
333+
* @covers ::flatten
334+
*/
335+
public function flatten()
336+
{
337+
$this->assertSame(array(1, 2, 3, 4, 5), A::flatten(array(array(1, 2), array(array(3, array(4, 5))))));
338+
}
328339
}

tests/Filter/StringTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,65 @@ public function filter_maxLengthNegative()
131131
{
132132
S::filter('a', false, 1, -1);
133133
}
134+
135+
/**
136+
* Verifies basic explode functionality.
137+
*
138+
* @test
139+
* @covers ::explode
140+
*/
141+
public function explode()
142+
{
143+
$this->assertSame(array('a', 'bcd', 'e'), S::explode('a,bcd,e'));
144+
}
145+
146+
/**
147+
* Verifies explode with a custom delimiter.
148+
*
149+
* @test
150+
* @covers ::explode
151+
*/
152+
public function explode_customDelimiter()
153+
{
154+
$this->assertSame(array('a', 'b', 'c', 'd,e'), S::explode('a b c d,e', ' '));
155+
}
156+
157+
/**
158+
* Verifies explode filter with a non-string value.
159+
*
160+
* @test
161+
* @expectedException Exception
162+
* @expectedExceptionMessage Value 'true' is not a string
163+
* @covers ::explode
164+
*/
165+
public function explode_nonStringValue()
166+
{
167+
S::explode(true);
168+
}
169+
170+
/**
171+
* Verifies explode filter with a non-string delimiter.
172+
*
173+
* @test
174+
* @expectedException Exception
175+
* @expectedExceptionMessage Delimiter '4' is not a non-empty string
176+
* @covers ::explode
177+
*/
178+
public function explode_nonStringDelimiter()
179+
{
180+
S::explode('test', 4);
181+
}
182+
183+
/**
184+
* Verifies explode filter with an empty delimiter.
185+
*
186+
* @test
187+
* @expectedException Exception
188+
* @expectedExceptionMessage Delimiter '''' is not a non-empty string
189+
* @covers ::explode
190+
*/
191+
public function explode_emptyDelimiter()
192+
{
193+
S::explode('test', '');
194+
}
134195
}

0 commit comments

Comments
 (0)