-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStringValidator.php
More file actions
70 lines (58 loc) · 1.64 KB
/
Copy pathStringValidator.php
File metadata and controls
70 lines (58 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
declare( strict_types = 1 );
namespace ValueValidators;
use Exception;
use ValueValidators\PackagePrivate\ValueValidatorBase;
/**
* ValueValidator that validates a string value.
*
* @since 0.1
*
* @license GPL-2.0-or-later
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class StringValidator extends ValueValidatorBase {
/**
* @see ValueValidatorBase::doValidation
*
* @since 0.1
*
* @param string $value
*
* @throws Exception
*/
public function doValidation( $value ) {
if ( !is_string( $value ) ) {
$this->addErrorMessage( 'Not a string' ); // TODO
return;
}
$lowerBound = false;
$upperBound = false;
if ( array_key_exists( 'length', $this->options ) ) {
$lowerBound = $this->options['length'];
$upperBound = $this->options['length'];
} else {
if ( array_key_exists( 'minlength', $this->options ) ) {
$lowerBound = $this->options['minlength'];
}
if ( array_key_exists( 'maxlength', $this->options ) ) {
$upperBound = $this->options['maxlength'];
}
}
if ( $lowerBound !== false || $upperBound !== false ) {
$rangeValidator = new RangeValidator();
$rangeValidator->setRange( $lowerBound, $upperBound );
$this->runSubValidator( strlen( $value ), $rangeValidator, 'length' );
}
if ( array_key_exists( 'regex', $this->options ) ) {
$match = preg_match( $this->options['regex'], $value );
if ( $match === false ) {
throw new Exception( 'The regex argument must be a valid Perl regular expression.' );
} elseif ( $match === 0 ) {
$this->addErrorMessage(
'String does not match the regular expression ' . $this->options['regex']
);
}
}
}
}