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

Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

src/BaseName.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,30 @@ class BaseName extends AbstractFilter
1414
/**
1515
* Defined by Zend\Filter\FilterInterface
1616
*
17-
* Returns basename($value)
17+
* Returns basename($value).
18+
*
19+
* If the value provided is non-scalar, the value will remain unfiltered
20+
* and an E_USER_WARNING will be raised indicating it's unfilterable.
1821
*
1922
* @param string $value
20-
* @return string
23+
* @return string|mixed
2124
*/
2225
public function filter($value)
2326
{
24-
if(!is_scalar($value)){
25-
throw new Exception\InvalidArgumentException(sprintf(
26-
'%s expects parameter to be scalar, "%s" given',
27-
__METHOD__,
28-
(is_object($value) ? get_class($value) : gettype($value))
29-
));
27+
if (null === $value) {
28+
return null;
29+
}
30+
31+
if (!is_scalar($value)){
32+
trigger_error(
33+
sprintf(
34+
'%s expects parameter to be scalar, "%s" given; cannot filter',
35+
__METHOD__,
36+
(is_object($value) ? get_class($value) : gettype($value))
37+
),
38+
E_USER_WARNING
39+
);
40+
return $value;
3041
}
3142

3243
return basename((string) $value);

src/Digits.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,28 @@ class Digits extends AbstractFilter
1818
*
1919
* Returns the string $value, removing all but digit characters
2020
*
21+
* If the value provided is non-scalar, the value will remain unfiltered
22+
* and an E_USER_WARNING will be raised indicating it's unfilterable.
23+
*
2124
* @param string $value
22-
* @return string
25+
* @return string|mixed
2326
*/
2427
public function filter($value)
2528
{
26-
if(!is_scalar($value)){
27-
throw new Exception\InvalidArgumentException(sprintf(
28-
'%s expects parameter to be scalar, "%s" given',
29-
__METHOD__,
30-
(is_object($value) ? get_class($value) : gettype($value))
31-
));
29+
if (null === $value) {
30+
return null;
31+
}
32+
33+
if (!is_scalar($value)){
34+
trigger_error(
35+
sprintf(
36+
'%s expects parameter to be scalar, "%s" given; cannot filter',
37+
__METHOD__,
38+
(is_object($value) ? get_class($value) : gettype($value))
39+
),
40+
E_USER_WARNING
41+
);
42+
return $value;
3243
}
3344

3445
if (!StringUtils::hasPcreUnicodeSupport()) {

src/HtmlEntities.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,29 @@ public function setDoubleQuote($doubleQuote)
173173
* Returns the string $value, converting characters to their corresponding HTML entity
174174
* equivalents where they exist
175175
*
176+
* If the value provided is non-scalar, the value will remain unfiltered
177+
* and an E_USER_WARNING will be raised indicating it's unfilterable.
178+
*
176179
* @param string $value
177-
* @throws Exception\DomainException
178-
* @return string
180+
* @return string|mixed
181+
* @throws Exception\DomainException on encoding mismatches
179182
*/
180183
public function filter($value)
181184
{
182-
if(!is_scalar($value)){
183-
throw new Exception\InvalidArgumentException(sprintf(
184-
'%s expects parameter to be scalar, "%s" given',
185-
__METHOD__,
186-
(is_object($value) ? get_class($value) : gettype($value))
187-
));
185+
if (null === $value) {
186+
return null;
187+
}
188+
189+
if (!is_scalar($value)){
190+
trigger_error(
191+
sprintf(
192+
'%s expects parameter to be scalar, "%s" given; cannot filter',
193+
__METHOD__,
194+
(is_object($value) ? get_class($value) : gettype($value))
195+
),
196+
E_USER_WARNING
197+
);
198+
return $value;
188199
}
189200

190201
$filtered = htmlentities((string) $value, $this->getQuoteStyle(), $this->getEncoding(), $this->getDoubleQuote());

src/Int.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,28 @@ class Int extends AbstractFilter
1616
*
1717
* Returns (int) $value
1818
*
19+
* If the value provided is non-scalar, the value will remain unfiltered
20+
* and an E_USER_WARNING will be raised indicating it's unfilterable.
21+
*
1922
* @param string $value
20-
* @return int
23+
* @return int|mixed
2124
*/
2225
public function filter($value)
2326
{
24-
if(!is_scalar($value)){
25-
throw new Exception\InvalidArgumentException(sprintf(
26-
'%s expects parameter to be scalar, "%s" given',
27-
__METHOD__,
28-
(is_object($value) ? get_class($value) : gettype($value))
29-
));
27+
if (null === $value) {
28+
return null;
29+
}
30+
31+
if (!is_scalar($value)){
32+
trigger_error(
33+
sprintf(
34+
'%s expects parameter to be scalar, "%s" given; cannot filter',
35+
__METHOD__,
36+
(is_object($value) ? get_class($value) : gettype($value))
37+
),
38+
E_USER_WARNING
39+
);
40+
return $value;
3041
}
3142

3243
return (int) ((string) $value);

src/RealPath.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,28 @@ public function getExists()
6666
*
6767
* Returns realpath($value)
6868
*
69+
* If the value provided is non-scalar, the value will remain unfiltered
70+
* and an E_USER_WARNING will be raised indicating it's unfilterable.
71+
*
6972
* @param string $value
70-
* @return string
73+
* @return string|mixed
7174
*/
7275
public function filter($value)
7376
{
74-
if(!is_scalar($value)){
75-
throw new Exception\InvalidArgumentException(sprintf(
76-
'%s expects parameter to be scalar, "%s" given',
77-
__METHOD__,
78-
(is_object($value) ? get_class($value) : gettype($value))
79-
));
77+
if (null === $value) {
78+
return null;
79+
}
80+
81+
if (!is_scalar($value)){
82+
trigger_error(
83+
sprintf(
84+
'%s expects parameter to be scalar, "%s" given; cannot filter',
85+
__METHOD__,
86+
(is_object($value) ? get_class($value) : gettype($value))
87+
),
88+
E_USER_WARNING
89+
);
90+
return $value;
8091
}
8192

8293
$path = (string) $value;

src/StringToLower.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,28 @@ public function __construct($encodingOrOptions = null)
4141
*
4242
* Returns the string $value, converting characters to lowercase as necessary
4343
*
44+
* If the value provided is non-scalar, the value will remain unfiltered
45+
* and an E_USER_WARNING will be raised indicating it's unfilterable.
46+
*
4447
* @param string $value
45-
* @return string
48+
* @return string|mixed
4649
*/
4750
public function filter($value)
4851
{
49-
if(!is_scalar($value)){
50-
throw new Exception\InvalidArgumentException(sprintf(
51-
'%s expects parameter to be scalar, "%s" given',
52-
__METHOD__,
53-
(is_object($value) ? get_class($value) : gettype($value))
54-
));
52+
if (null === $value) {
53+
return null;
54+
}
55+
56+
if (!is_scalar($value)){
57+
trigger_error(
58+
sprintf(
59+
'%s expects parameter to be scalar, "%s" given; cannot filter',
60+
__METHOD__,
61+
(is_object($value) ? get_class($value) : gettype($value))
62+
),
63+
E_USER_WARNING
64+
);
65+
return $value;
5566
}
5667

5768
if ($this->options['encoding'] !== null) {

src/StringToUpper.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,30 @@ public function __construct($encodingOrOptions = null)
3939
/**
4040
* Defined by Zend\Filter\FilterInterface
4141
*
42-
* Returns the string $value, converting characters to lowercase as necessary
42+
* Returns the string $value, converting characters to uppercase as necessary
43+
*
44+
* If the value provided is non-scalar, the value will remain unfiltered
45+
* and an E_USER_WARNING will be raised indicating it's unfilterable.
4346
*
4447
* @param string $value
45-
* @return string
48+
* @return string|mixed
4649
*/
4750
public function filter($value)
4851
{
49-
if(!is_scalar($value)){
50-
throw new Exception\InvalidArgumentException(sprintf(
51-
'%s expects parameter to be scalar, "%s" given',
52-
__METHOD__,
53-
(is_object($value) ? get_class($value) : gettype($value))
54-
));
52+
if (null === $value) {
53+
return null;
54+
}
55+
56+
if (!is_scalar($value)){
57+
trigger_error(
58+
sprintf(
59+
'%s expects parameter to be scalar, "%s" given; cannot filter',
60+
__METHOD__,
61+
(is_object($value) ? get_class($value) : gettype($value))
62+
),
63+
E_USER_WARNING
64+
);
65+
return $value;
5566
}
5667

5768
if ($this->options['encoding'] !== null) {

src/StripTags.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,19 +166,29 @@ public function setAttributesAllowed($attributesAllowed)
166166
/**
167167
* Defined by Zend\Filter\FilterInterface
168168
*
169-
* @todo improve docblock descriptions
169+
* If the value provided is non-scalar, the value will remain unfiltered
170+
* and an E_USER_WARNING will be raised indicating it's unfilterable.
170171
*
172+
* @todo improve docblock descriptions
171173
* @param string $value
172-
* @return string
174+
* @return string|mixed
173175
*/
174176
public function filter($value)
175177
{
176-
if(!is_scalar($value)){
177-
throw new Exception\InvalidArgumentException(sprintf(
178-
'%s expects parameter to be scalar, "%s" given',
179-
__METHOD__,
180-
(is_object($value) ? get_class($value) : gettype($value))
181-
));
178+
if (null === $value) {
179+
return null;
180+
}
181+
182+
if (!is_scalar($value)){
183+
trigger_error(
184+
sprintf(
185+
'%s expects parameter to be scalar, "%s" given; cannot filter',
186+
__METHOD__,
187+
(is_object($value) ? get_class($value) : gettype($value))
188+
),
189+
E_USER_WARNING
190+
);
191+
return $value;
182192
}
183193

184194
$value = (string) $value;

test/BaseNameTest.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace ZendTest\Filter;
1212

1313
use Zend\Filter\BaseName as BaseNameFilter;
14+
use Zend\Stdlib\ErrorHandler;
1415

1516
/**
1617
* @category Zend
@@ -38,21 +39,31 @@ public function testBasic()
3839
}
3940

4041
/**
41-
* Ensures that an InvalidArgumentException is raised if array is used
42+
* Ensures that a warning is raised if array is used
4243
*
4344
* @return void
4445
*/
45-
public function testExceptionRaisedIfArrayUsed()
46+
public function testWarningIsRaisedIfArrayUsed()
4647
{
4748
$filter = new BaseNameFilter();
4849
$input = array('/path/to/filename', '/path/to/filename.ext');
4950

50-
try {
51-
$filter->filter($input);
52-
} catch (\Zend\Filter\Exception\InvalidArgumentException $expected) {
53-
return;
54-
}
51+
ErrorHandler::start(E_USER_WARNING);
52+
$filtered = $filter->filter($input);
53+
$err = ErrorHandler::stop();
54+
55+
$this->assertEquals($input, $filtered);
56+
$this->assertInstanceOf('ErrorException', $err);
57+
$this->assertContains('cannot filter', $err->getMessage());
58+
}
5559

56-
$this->fail('An expected InvalidArgumentException has not been raised.');
60+
/**
61+
* @return void
62+
*/
63+
public function testReturnsNullIfNullIsUsed()
64+
{
65+
$filter = new BaseNameFilter();
66+
$filtered = $filter->filter(null);
67+
$this->assertNull($filtered);
5768
}
5869
}

test/DigitsTest.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace ZendTest\Filter;
1212

1313
use Zend\Filter\Digits as DigitsFilter;
14+
use Zend\Stdlib\ErrorHandler;
1415

1516
/**
1617
* @category Zend
@@ -87,21 +88,31 @@ public function testBasic()
8788
}
8889

8990
/**
90-
* Ensures that an InvalidArgumentException is raised if array is used
91+
* Ensures that an error is raised if array is used
9192
*
9293
* @return void
9394
*/
94-
public function testExceptionRaisedIfArrayUsed()
95+
public function testWarningIsRaisedIfArrayUsed()
9596
{
9697
$filter = new DigitsFilter();
9798
$input = array('abc123', 'abc 123');
9899

99-
try {
100-
$filter->filter($input);
101-
} catch (\Zend\Filter\Exception\InvalidArgumentException $expected) {
102-
return;
103-
}
100+
ErrorHandler::start(E_USER_WARNING);
101+
$filtered = $filter->filter($input);
102+
$err = ErrorHandler::stop();
103+
104+
$this->assertEquals($input, $filtered);
105+
$this->assertInstanceOf('ErrorException', $err);
106+
$this->assertContains('cannot filter', $err->getMessage());
107+
}
104108

105-
$this->fail('An expected InvalidArgumentException has not been raised.');
109+
/**
110+
* @return void
111+
*/
112+
public function testReturnsNullIfNullIsUsed()
113+
{
114+
$filter = new DigitsFilter();
115+
$filtered = $filter->filter(null);
116+
$this->assertNull($filtered);
106117
}
107118
}

0 commit comments

Comments
 (0)