Skip to content

Commit e17e64e

Browse files
committed
+ trimmed string validator
1 parent b9940ef commit e17e64e

5 files changed

+142
-0
lines changed

phpunit.xml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<file>ut/BooleanValidatorTest.php</file>
1818
<file>ut/ArrayValidatorTest.php</file>
1919
<file>ut/StringLengthValidatorTest.php</file>
20+
<file>ut/TrimmedStringValidatorTest.php</file>
2021
<file>ut/EmailValidatorTest.php</file>
2122
<file>ut/UrlValidatorTest.php</file>
2223
<file>ut/RegexValidatorTest.php</file>

src/AbstractDataProvider.php

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Date: 2015-09-15
66
* Time: 11:48
77
*/
8+
89
namespace Oasis\Mlib\Utils;
910

1011
use Oasis\Mlib\Utils\Exceptions\DataValidationException;
@@ -18,6 +19,7 @@
1819
use Oasis\Mlib\Utils\Validators\IntegerValidator;
1920
use Oasis\Mlib\Utils\Validators\ObjectValidator;
2021
use Oasis\Mlib\Utils\Validators\StringValidator;
22+
use Oasis\Mlib\Utils\Validators\TrimmedStringValidator;
2123
use Oasis\Mlib\Utils\Validators\ValidatorInterface;
2224

2325
abstract class AbstractDataProvider implements DataProviderInterface
@@ -105,6 +107,9 @@ protected function getValidatorByLegacyString($type)
105107
case self::NON_EMPTY_STRING_TYPE:
106108
return new StringValidator(false, false);
107109
break;
110+
case self::TRIMMED_STRING_TYPE:
111+
return new TrimmedStringValidator(false);
112+
break;
108113
case self::INT_TYPE:
109114
return new IntegerValidator();
110115
break;

src/DataProviderInterface.php

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ interface DataProviderInterface
1414
const FLOAT_TYPE = "requireFloat";
1515
const STRING_TYPE = "requireString";
1616
const NON_EMPTY_STRING_TYPE = "requireNonEmptyString";
17+
const TRIMMED_STRING_TYPE = "requireTrimmedString";
1718
const ARRAY_TYPE = "requireArray";
1819
const ARRAY_2D_TYPE = "requireArray2D";
1920
const BOOL_TYPE = "requireBool";
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: minhao
5+
* Date: 2016-09-02
6+
* Time: 15:16
7+
*/
8+
9+
namespace Oasis\Mlib\Utils\Validators;
10+
11+
use Oasis\Mlib\Utils\Exceptions\InvalidDataTypeException;
12+
13+
class TrimmedStringValidator implements ValidatorInterface
14+
{
15+
const TRIM_BOTH = 'both';
16+
const TRIM_LEFT = 'left';
17+
const TRIM_RIGHT = 'right';
18+
19+
/** @var bool if strict, only string is allowed */
20+
protected $strict = false;
21+
/**
22+
* @var string
23+
*/
24+
private $direction;
25+
/**
26+
* @var string
27+
*/
28+
private $characters;
29+
30+
public function __construct($strict = false, $direction = self::TRIM_BOTH, $characters = " \n\t\r\0\x0B")
31+
{
32+
$this->strict = $strict;
33+
$this->direction = $direction;
34+
$this->characters = $characters;
35+
}
36+
37+
public function validate($target)
38+
{
39+
if (!$this->strict) {
40+
if (is_bool($target)) {
41+
$target = $target ? "true" : "false";
42+
}
43+
elseif (is_scalar($target)) {
44+
$target = strval($target);
45+
}
46+
elseif (is_object($target) && method_exists($target, '__toString()')) {
47+
$target = strval($target);
48+
}
49+
}
50+
51+
if (!is_string($target)) {
52+
throw new InvalidDataTypeException("Validated value is not a string!");
53+
}
54+
55+
switch ($this->direction) {
56+
case self::TRIM_LEFT:
57+
return \ltrim($target, $this->characters);
58+
case self::TRIM_RIGHT:
59+
return \rtrim($target, $this->characters);
60+
case self::TRIM_BOTH:
61+
default:
62+
return \trim($target, $this->characters);
63+
}
64+
}
65+
}

ut/TrimmedStringValidatorTest.php

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
use Oasis\Mlib\Utils\Exceptions\InvalidDataTypeException;
4+
use Oasis\Mlib\Utils\Exceptions\StringTooLongException;
5+
use Oasis\Mlib\Utils\Exceptions\StringTooShortException;
6+
use Oasis\Mlib\Utils\Validators\StringLengthValidator;
7+
use Oasis\Mlib\Utils\Validators\TrimmedStringValidator;
8+
9+
/**
10+
* Created by PhpStorm.
11+
* User: minhao
12+
* Date: 2018-05-03
13+
* Time: 21:59
14+
*/
15+
class TrimmedStringValidatorTest extends PHPUnit_Framework_TestCase
16+
{
17+
/**
18+
* @dataProvider getValidStrings
19+
*
20+
* @param $target
21+
* @param $expectation
22+
* @param $direction
23+
* @param $chars
24+
*/
25+
public function testValidStrings($target, $expectation, $direction, $chars)
26+
{
27+
$validator = new TrimmedStringValidator(true, $direction, $chars);
28+
$this->assertEquals($expectation, $validator->validate($target));
29+
}
30+
31+
/**
32+
* @dataProvider getInvalidStrings
33+
*
34+
* @param $target
35+
*/
36+
public function testInvalidStrings($target)
37+
{
38+
$validator = new TrimmedStringValidator(true);
39+
try {
40+
$validator->validate($target);
41+
} catch (Exception $e) {
42+
$this->assertTrue($e instanceof InvalidDataTypeException);
43+
}
44+
}
45+
46+
public function getValidStrings()
47+
{
48+
return [
49+
[' abcde ', 'abcde', TrimmedStringValidator::TRIM_BOTH, " \n\r\t\0\0x0B"],
50+
['abcde ', 'abcde', TrimmedStringValidator::TRIM_BOTH, " \n\r\t\0\0x0B"],
51+
[' abcde', 'abcde', TrimmedStringValidator::TRIM_BOTH, " \n\r\t\0\0x0B"],
52+
[' abcde ', 'abcde ', TrimmedStringValidator::TRIM_LEFT, " \n\r\t\0\0x0B"],
53+
['abcde ', 'abcde ', TrimmedStringValidator::TRIM_LEFT, " \n\r\t\0\0x0B"],
54+
[' abcde', 'abcde', TrimmedStringValidator::TRIM_LEFT, " \n\r\t\0\0x0B"],
55+
[' abcde ', ' abcde', TrimmedStringValidator::TRIM_RIGHT, " \n\r\t\0\0x0B"],
56+
['abcde ', 'abcde', TrimmedStringValidator::TRIM_RIGHT, " \n\r\t\0\0x0B"],
57+
[' abcde', ' abcde', TrimmedStringValidator::TRIM_RIGHT, " \n\r\t\0\0x0B"],
58+
['abc', 'b', TrimmedStringValidator::TRIM_BOTH, "ac"],
59+
];
60+
}
61+
62+
public function getInvalidStrings()
63+
{
64+
return [
65+
[0],
66+
[CURLOPT_SSL_FALSESTART],
67+
[null],
68+
];
69+
}
70+
}

0 commit comments

Comments
 (0)