Skip to content

Commit cc7a08c

Browse files
committed
Merge pull request #21 from gaillard/master
Add ofArray filter
2 parents 4cc00eb + ef872e7 commit cc7a08c

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

src/DominionEnterprises/Filter/Arrays.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,25 @@ public static function ofArrays(array $values, array $spec)
144144

145145
return $results;
146146
}
147+
148+
/**
149+
* Filter $value by using a Filterer $spec and Filterer's default options.
150+
*
151+
* @param array $value array to be filtered. Use the Arrays::filter() before this method to ensure counts when you pass into Filterer
152+
* @param array $spec spec to apply to $value, specified the same as in @see Filterer::filter.
153+
* Eg ['key' => ['required' => true, ['string', false], ['unit']], 'key2' => ...]
154+
*
155+
* @return array the filtered $value
156+
*
157+
* @throws \Exception if $value fails filtering
158+
*/
159+
public static function ofArray(array $value, array $spec)
160+
{
161+
list($status, $result, $error) = Filterer::filter($spec, $value);
162+
if (!$status) {
163+
throw new \Exception($error);
164+
}
165+
166+
return $result;
167+
}
147168
}

src/DominionEnterprises/Filterer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ final class Filterer
2020
'string' => '\DominionEnterprises\Filter\String::filter',
2121
'ofScalars' => '\DominionEnterprises\Filter\Arrays::ofScalars',
2222
'ofArrays' => '\DominionEnterprises\Filter\Arrays::ofArrays',
23+
'ofArray' => '\DominionEnterprises\Filter\Arrays::ofArray',
2324
);
2425

2526
/**

tests/DominionEnterprises/Filter/ArraysTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,67 @@ public function ofArrays_fail()
230230
$this->assertSame($expected, $e->getMessage());
231231
}
232232
}
233+
234+
/**
235+
* @test
236+
* @covers \DominionEnterprises\Filter\Arrays::ofArray
237+
*/
238+
public function ofArray()
239+
{
240+
$expected = array('key1' => 1, 'key2' => 2);
241+
$spec = array('key1' => array(array('uint')), 'key2' => array(array('uint')));
242+
$this->assertSame($expected, A::ofArray(array('key1' => '1', 'key2' => '2'), $spec));
243+
}
244+
245+
/**
246+
* @test
247+
* @covers \DominionEnterprises\Filter\Arrays::ofArray
248+
*/
249+
public function ofArray_chained()
250+
{
251+
$expected = array('key' => 3.3);
252+
$spec = array('key' => array(array('trim', 'a'), array('floatval')));
253+
$this->assertSame($expected, A::ofArray(array('key' => 'a3.3'), $spec));
254+
}
255+
256+
/**
257+
* @test
258+
* @covers \DominionEnterprises\Filter\Arrays::ofArray
259+
*/
260+
public function ofArray_requiredSuccess()
261+
{
262+
$expected = array('key2' => 2);
263+
$spec = array('key1' => array(array('uint')), 'key2' => array('required' => true, array('uint')));
264+
$this->assertSame($expected, A::ofArray(array('key2' => '2'), $spec));
265+
}
266+
267+
/**
268+
* @test
269+
* @covers \DominionEnterprises\Filter\Arrays::ofArray
270+
*/
271+
public function ofArray_requiredFail()
272+
{
273+
try {
274+
A::ofArray(array('key1' => '1'), array('key1' => array(array('uint')), 'key2' => array('required' => true, array('uint'))));
275+
$this->fail();
276+
} catch (\Exception $e) {
277+
$expected = "Field 'key2' was required and not present";
278+
$this->assertSame($expected, $e->getMessage());
279+
}
280+
}
281+
282+
/**
283+
* @test
284+
* @covers \DominionEnterprises\Filter\Arrays::ofArray
285+
*/
286+
public function ofArray_unknown()
287+
{
288+
try {
289+
A::ofArray(array('key' => '1'), array('key2' => array(array('uint'))));
290+
$this->fail();
291+
} catch (\Exception $e) {
292+
$expected = "Field 'key' with value '1' is unknown";
293+
$this->assertSame($expected, $e->getMessage());
294+
}
295+
}
233296
}

0 commit comments

Comments
 (0)