Skip to content

Commit 97d42e1

Browse files
authored
Merge pull request #72 from chadicus/fea/string-filter
Allow String::Filter to accept scalars and objects with __tostring
2 parents 7dc5af3 + 5bc3d65 commit 97d42e1

3 files changed

Lines changed: 54 additions & 11 deletions

File tree

src/Filter/Strings.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ public static function filter($value, $allowNull = false, $minLength = 1, $maxLe
4545
return null;
4646
}
4747

48+
if (is_scalar($value)) {
49+
$value = "{$value}";
50+
}
51+
52+
if (is_object($value) && method_exists($value, '__toString')) {
53+
$value = (string)$value;
54+
}
55+
4856
if (!is_string($value)) {
4957
throw new \Exception("Value '" . var_export($value, true) . "' is not a string");
5058
}

tests/Filter/ArraysTest.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,17 @@ public function ofScalarsWithMeaninglessKeys()
172172
public function ofScalarsFail()
173173
{
174174
try {
175-
Arrays::ofScalars(['1', 2, 3], [['string']]);
175+
Arrays::ofScalars(['1', [], new \StdClass], [['string']]);
176176
$this->fail();
177177
} catch (\Exception $e) {
178-
$expected = "Field '1' with value '2' failed filtering, message 'Value '2' is not a string'\n";
179-
$expected .= "Field '2' with value '3' failed filtering, message 'Value '3' is not a string'";
178+
$expected = <<<TXT
179+
Field '1' with value 'array (
180+
)' failed filtering, message 'Value 'array (
181+
)' is not a string'
182+
Field '2' with value 'stdClass::__set_state(array(
183+
))' failed filtering, message 'Value 'stdClass::__set_state(array(
184+
))' is not a string'
185+
TXT;
180186
$this->assertSame($expected, $e->getMessage());
181187
}
182188
}
@@ -224,12 +230,22 @@ public function ofArraysRequiredAndUnknown()
224230
public function ofArraysFail()
225231
{
226232
try {
227-
Arrays::ofArrays([['key' => '1'], ['key' => 2], ['key' => 3], 'key'], ['key' => [['string']]]);
233+
Arrays::ofArrays(
234+
[['key' => new \StdClass], ['key' => []], ['key' => null], 'key'],
235+
['key' => [['string']]]
236+
);
228237
$this->fail();
229238
} catch (\Exception $e) {
230-
$expected = "Field 'key' with value '2' failed filtering, message 'Value '2' is not a string'\n";
231-
$expected .= "Field 'key' with value '3' failed filtering, message 'Value '3' is not a string'\n";
232-
$expected .= "Value at position '3' was not an array";
239+
$expected = <<<TXT
240+
Field 'key' with value 'stdClass::__set_state(array(
241+
))' failed filtering, message 'Value 'stdClass::__set_state(array(
242+
))' is not a string'
243+
Field 'key' with value 'array (
244+
)' failed filtering, message 'Value 'array (
245+
)' is not a string'
246+
Field 'key' with value 'NULL' failed filtering, message 'Value 'NULL' is not a string'
247+
Value at position '3' was not an array
248+
TXT;
233249
$this->assertSame($expected, $e->getMessage());
234250
}
235251
}

tests/Filter/StringsTest.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,33 @@
77
final class StringsTest extends \PHPUnit_Framework_TestCase
88
{
99
/**
10+
* Verify basic use of filter
11+
*
1012
* @test
11-
* @expectedException Exception
12-
* @expectedExceptionMessage Value '1' is not a string
1313
* @covers ::filter
14+
* @dataProvider filterData
15+
*
16+
* @return void
17+
*/
18+
public function filter($input, $expected)
19+
{
20+
$this->assertSame($expected, Strings::filter($input));
21+
}
22+
23+
/**
24+
* Data provider for basic filter tests
25+
*
26+
* @return array
1427
*/
15-
public function filterNotString()
28+
public function filterData()
1629
{
17-
Strings::filter(1);
30+
return [
31+
'string' => ['abc', 'abc'],
32+
'int' => [1, '1'],
33+
'float' => [1.1, '1.1'],
34+
'bool' => [true, '1'],
35+
'object' => [new \SplFileInfo(__FILE__), __FILE__],
36+
];
1837
}
1938

2039
/**

0 commit comments

Comments
 (0)