Skip to content

Commit 3b08c6b

Browse files
committed
Merge pull request #32 from gaillard/master
Default support and coversDefaultClass
2 parents 5d3a068 + e7419b4 commit 3b08c6b

File tree

9 files changed

+207
-122
lines changed

9 files changed

+207
-122
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ A filtering implementation for verifying correct data and performing typical mod
1717
* Class function
1818
* Built-in function
1919
* Optional/Required support, field and global level
20+
* Default support
2021
* Chaining filters
2122
* Optionally returns unknown fields
2223
* Filter alias support
@@ -39,6 +40,7 @@ list($status, $result, $error, $unknowns) = DominionEnterprises\Filterer::filter
3940
'field one' => [[$trimFunc], ['substr', 0, 3], [[$appendFilter, 'filter'], 'boo']],
4041
'field two' => ['required' => true, ['floatval']],
4142
'field three' => ['required' => false, ['float']],
43+
'field four' => ['required' => true, 'default' => 1, ['uint']],
4244
],
4345
['field one' => ' abcd', 'field two' => '3.14']
4446
);
@@ -52,11 +54,13 @@ prints
5254

5355
```php
5456
bool(true)
55-
array(2) {
57+
array(3) {
5658
'field one' =>
5759
string(6) "abcboo"
5860
'field two' =>
5961
double(3.14)
62+
'field four' =>
63+
int(1)
6064
}
6165
NULL
6266
array(0) {

src/Filterer.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ final class Filterer
4444
* 'field one' => [[$trimFunc], ['substr', 0, 3], [[$appendFilter, 'filter'], 'boo']],
4545
* 'field two' => ['required' => true, ['floatval']],
4646
* 'field three' => ['required' => false, ['float']],
47+
* 'field four' => ['required' => true, 'default' => 1, ['uint']],
4748
* ],
4849
* ['field one' => ' abcd', 'field two' => '3.14']
4950
* );
@@ -56,11 +57,13 @@ final class Filterer
5657
* prints:
5758
* <pre>
5859
* bool(true)
59-
* array(2) {
60+
* array(3) {
6061
* 'field one' =>
6162
* string(6) "abcboo"
6263
* 'field two' =>
6364
* double(3.14)
65+
* 'field four' =>
66+
* int(1)
6467
* }
6568
* NULL
6669
* array(0) {
@@ -72,7 +75,8 @@ final class Filterer
7275
* value to filter as its first argument. Two examples would be the string 'trim' or an object function specified like [$obj, 'filter'],
7376
* see is_callable() documentation. The rest of the members are extra arguments to the callable. The result of one filter will be the
7477
* first argument to the next filter. In addition to the filters, the specification values may contain a 'required' key (default false)
75-
* that controls the same behavior as the 'defaultRequired' option below but on a per field basis.
78+
* that controls the same behavior as the 'defaultRequired' option below but on a per field basis. A 'default' specification value
79+
* may be used to substitute in a default to the $input when the key is not present (whether 'required' is specified or not).
7680
* @param array $input the input the apply the $spec on.
7781
* @param array $options 'allowUnknowns' (default false) true to allow unknowns or false to treat as error, 'defaultRequired'
7882
* (default false) true to make fields required by default and treat as error on absence and false to allow their absence by default
@@ -115,6 +119,7 @@ public static function filter(array $spec, array $input, array $options = array(
115119
}
116120

117121
unset($filters['required']);//doesnt matter if required since we have this one
122+
unset($filters['default']);//doesnt matter if there is a default since we have a value
118123
foreach ($filters as $filter) {
119124
if (!is_array($filter)) {
120125
throw new \InvalidArgumentException("filter for field '{$field}' was not a array");
@@ -152,12 +157,17 @@ public static function filter(array $spec, array $input, array $options = array(
152157
throw new \InvalidArgumentException("filters for field '{$field}' was not a array");
153158
}
154159

155-
$required = array_key_exists('required', $filters) ? $filters['required'] : $defaultRequired;
160+
$required = isset($filters['required']) ? $filters['required'] : $defaultRequired;
156161

157162
if ($required !== false && $required !== true) {
158163
throw new \InvalidArgumentException("'required' for field '{$field}' was not a bool");
159164
}
160165

166+
if (array_key_exists('default', $filters)) {
167+
$inputToFilter[$field] = $filters['default'];
168+
continue;
169+
}
170+
161171
if ($required) {
162172
$errors[] = "Field '{$field}' was required and not present";
163173
}

tests/Filter/ArraysTest.php

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
namespace DominionEnterprises\Filter;
33
use DominionEnterprises\Filter\Arrays as A;
44

5+
/**
6+
* @coversDefaultClass \DominionEnterprises\Filter\Arrays
7+
*/
58
final class ArraysTest extends \PHPUnit_Framework_TestCase
69
{
710
/**
811
* @test
9-
* @covers \DominionEnterprises\Filter\Arrays::filter
12+
* @covers ::filter
1013
*/
1114
public function filter_basicPass()
1215
{
@@ -15,7 +18,7 @@ public function filter_basicPass()
1518

1619
/**
1720
* @test
18-
* @covers \DominionEnterprises\Filter\Arrays::filter
21+
* @covers ::filter
1922
* @expectedException \Exception
2023
* @expectedExceptionMessage Value '1' is not an array
2124
*/
@@ -26,7 +29,7 @@ public function filter_failNotArray()
2629

2730
/**
2831
* @test
29-
* @covers \DominionEnterprises\Filter\Arrays::filter
32+
* @covers ::filter
3033
* @expectedException \Exception
3134
* @expectedExceptionMessage $value count of 0 is less than 1
3235
*/
@@ -37,7 +40,7 @@ public function filter_failEmpty()
3740

3841
/**
3942
* @test
40-
* @covers \DominionEnterprises\Filter\Arrays::filter
43+
* @covers ::filter
4144
* @expectedException \Exception
4245
* @expectedExceptionMessage $value count of 1 is less than 2
4346
*/
@@ -48,7 +51,7 @@ public function filter_countLessThanMin()
4851

4952
/**
5053
* @test
51-
* @covers \DominionEnterprises\Filter\Arrays::filter
54+
* @covers ::filter
5255
* @expectedException \Exception
5356
* @expectedExceptionMessage $value count of 2 is greater than 1
5457
*/
@@ -59,7 +62,7 @@ public function filter_countGreaterThanMax()
5962

6063
/**
6164
* @test
62-
* @covers \DominionEnterprises\Filter\Arrays::filter
65+
* @covers ::filter
6366
* @expectedException \InvalidArgumentException
6467
* @expectedExceptionMessage $minCount was not an int
6568
*/
@@ -70,7 +73,7 @@ public function filter_minCountNotInt()
7073

7174
/**
7275
* @test
73-
* @covers \DominionEnterprises\Filter\Arrays::filter
76+
* @covers ::filter
7477
* @expectedException \InvalidArgumentException
7578
* @expectedExceptionMessage $maxCount was not an int
7679
*/
@@ -81,7 +84,7 @@ public function filter_maxCountNotInt()
8184

8285
/**
8386
* @test
84-
* @covers \DominionEnterprises\Filter\Arrays::in
87+
* @covers ::in
8588
*/
8689
public function in_passStrict()
8790
{
@@ -90,7 +93,7 @@ public function in_passStrict()
9093

9194
/**
9295
* @test
93-
* @covers \DominionEnterprises\Filter\Arrays::in
96+
* @covers ::in
9497
*/
9598
public function in_failStrict()
9699
{
@@ -104,7 +107,7 @@ public function in_failStrict()
104107

105108
/**
106109
* @test
107-
* @covers \DominionEnterprises\Filter\Arrays::in
110+
* @covers ::in
108111
*/
109112
public function in_failNotStrict()
110113
{
@@ -118,7 +121,7 @@ public function in_failNotStrict()
118121

119122
/**
120123
* @test
121-
* @covers \DominionEnterprises\Filter\Arrays::in
124+
* @covers ::in
122125
*/
123126
public function in_passNotStrict()
124127
{
@@ -127,7 +130,7 @@ public function in_passNotStrict()
127130

128131
/**
129132
* @test
130-
* @covers \DominionEnterprises\Filter\Arrays::in
133+
* @covers ::in
131134
* @expectedException \InvalidArgumentException
132135
* @expectedExceptionMessage $strict was not a bool
133136
*/
@@ -138,7 +141,7 @@ public function in_strictNotBool()
138141

139142
/**
140143
* @test
141-
* @covers \DominionEnterprises\Filter\Arrays::ofScalars
144+
* @covers ::ofScalars
142145
*/
143146
public function ofScalars()
144147
{
@@ -147,7 +150,7 @@ public function ofScalars()
147150

148151
/**
149152
* @test
150-
* @covers \DominionEnterprises\Filter\Arrays::ofScalars
153+
* @covers ::ofScalars
151154
*/
152155
public function ofScalars_chained()
153156
{
@@ -156,7 +159,7 @@ public function ofScalars_chained()
156159

157160
/**
158161
* @test
159-
* @covers \DominionEnterprises\Filter\Arrays::ofScalars
162+
* @covers ::ofScalars
160163
*/
161164
public function ofScalars_withMeaninglessKeys()
162165
{
@@ -165,7 +168,7 @@ public function ofScalars_withMeaninglessKeys()
165168

166169
/**
167170
* @test
168-
* @covers \DominionEnterprises\Filter\Arrays::ofScalars
171+
* @covers ::ofScalars
169172
*/
170173
public function ofScalars_fail()
171174
{
@@ -181,7 +184,7 @@ public function ofScalars_fail()
181184

182185
/**
183186
* @test
184-
* @covers \DominionEnterprises\Filter\Arrays::ofArrays
187+
* @covers ::ofArrays
185188
*/
186189
public function ofArrays()
187190
{
@@ -191,7 +194,7 @@ public function ofArrays()
191194

192195
/**
193196
* @test
194-
* @covers \DominionEnterprises\Filter\Arrays::ofArrays
197+
* @covers ::ofArrays
195198
*/
196199
public function ofArrays_chained()
197200
{
@@ -202,7 +205,7 @@ public function ofArrays_chained()
202205

203206
/**
204207
* @test
205-
* @covers \DominionEnterprises\Filter\Arrays::ofArrays
208+
* @covers ::ofArrays
206209
*/
207210
public function ofArrays_requiredAndUnknown()
208211
{
@@ -217,7 +220,7 @@ public function ofArrays_requiredAndUnknown()
217220

218221
/**
219222
* @test
220-
* @covers \DominionEnterprises\Filter\Arrays::ofArrays
223+
* @covers ::ofArrays
221224
*/
222225
public function ofArrays_fail()
223226
{
@@ -233,7 +236,7 @@ public function ofArrays_fail()
233236

234237
/**
235238
* @test
236-
* @covers \DominionEnterprises\Filter\Arrays::ofArray
239+
* @covers ::ofArray
237240
*/
238241
public function ofArray()
239242
{
@@ -244,7 +247,7 @@ public function ofArray()
244247

245248
/**
246249
* @test
247-
* @covers \DominionEnterprises\Filter\Arrays::ofArray
250+
* @covers ::ofArray
248251
*/
249252
public function ofArray_chained()
250253
{
@@ -255,7 +258,7 @@ public function ofArray_chained()
255258

256259
/**
257260
* @test
258-
* @covers \DominionEnterprises\Filter\Arrays::ofArray
261+
* @covers ::ofArray
259262
*/
260263
public function ofArray_requiredSuccess()
261264
{
@@ -266,7 +269,7 @@ public function ofArray_requiredSuccess()
266269

267270
/**
268271
* @test
269-
* @covers \DominionEnterprises\Filter\Arrays::ofArray
272+
* @covers ::ofArray
270273
*/
271274
public function ofArray_requiredFail()
272275
{
@@ -281,7 +284,7 @@ public function ofArray_requiredFail()
281284

282285
/**
283286
* @test
284-
* @covers \DominionEnterprises\Filter\Arrays::ofArray
287+
* @covers ::ofArray
285288
*/
286289
public function ofArray_unknown()
287290
{

0 commit comments

Comments
 (0)