Skip to content

Commit

Permalink
Refactored usage of StringValidator
Browse files Browse the repository at this point in the history
Removed internal casting an extended String validation for objects
Fixed unit tests
  • Loading branch information
hollodotme committed Mar 28, 2016
1 parent fda2491 commit 8a56467
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 208 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
@@ -1,8 +1,13 @@
# CHANGELOG

## Changes in Version 1.3.1
## Changes in Version 1.4.0

* Fixed issue #1
* Fixed issue #2
* Fixed issue #3

* Replaced [mbstring functions](http://php.net/manual/en/ref.mbstring.php) with [grapheme functions](http://php.net/manual/en/ref.intl.grapheme.php)
* Added test configuration for php7

## Changes in Version 1.3.0

Expand Down
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -9,6 +9,11 @@

Validating data with a fluent interfaced class

## Requirements

* PHP >= 5.5
* [intl extension](http://php.net/manual/en/book.intl.php)

## Installation

```
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -15,7 +15,8 @@
"source": "https://github.com/hollodotme/fluid-validator"
},
"require": {
"php": ">=5.5"
"php": ">=5.5",
"ext-intl": "*"
},
"require-dev": {
"satooshi/php-coveralls": "dev-master"
Expand Down
7 changes: 4 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 29 additions & 53 deletions src/FluidValidator.php
Expand Up @@ -131,6 +131,9 @@ class FluidValidator
/** @var CollectsMessages */
private $messageCollector;

/** @var StringValidator */
private $stringValidator;

/**
* @param int $mode
* @param ProvidesValuesToValidate|null $dataProvider
Expand Down Expand Up @@ -397,7 +400,20 @@ public function getValue( $var )
*/
protected function checkIsString( $value )
{
return ( new StringValidator() )->isString( $this->getValue( $value ) );
return $this->getStringValidator()->isString( $this->getValue( $value ) );
}

/**
* @return StringValidator
*/
private function getStringValidator()
{
if ( $this->stringValidator === null )
{
$this->stringValidator = new StringValidator();
}

return $this->stringValidator;
}

/**
Expand All @@ -407,7 +423,7 @@ protected function checkIsString( $value )
*/
protected function checkIsNonEmptyString( $value )
{
return ( new StringValidator() )->isNonEmptyString( $this->getValue( $value ) );
return $this->getStringValidator()->isNonEmptyString( $this->getValue( $value ) );
}

/**
Expand Down Expand Up @@ -437,7 +453,7 @@ protected function checkIsArray( $value )
*/
protected function checkIsInt( $value )
{
return ( new StringValidator() )->isInt( $this->getValue( $value ) );
return (is_int( $this->getValue( $value ) ) === true);
}

/**
Expand All @@ -450,9 +466,7 @@ protected function checkIsIntInRange( $value, array $range )
{
if ( $this->checkIsInt( $value ) )
{
$val = intval( strval( $this->getValue( $value ) ) );

return in_array( $val, $range, true );
return in_array( $this->getValue( $value ), $range, true );
}
else
{
Expand All @@ -470,9 +484,7 @@ protected function checkIsOneStringOf( $value, array $list )
{
if ( $this->checkIsString( $value ) )
{
$val = strval( $this->getValue( $value ) );

return in_array( $val, $list, true );
return in_array( $this->getValue( $value ), $list, true );
}
else
{
Expand Down Expand Up @@ -507,7 +519,7 @@ protected function checkIsSubsetOf( $values, array $list )
*/
protected function checkIsUuid( $value )
{
return ( new StringValidator() )->isUuid( $this->getValue( $value ) );
return $this->getStringValidator()->isUuid( $this->getValue( $value ) );
}

/**
Expand Down Expand Up @@ -582,16 +594,7 @@ protected function checkIsNotNull( $value )
*/
protected function checkMatchesRegex( $value, $regex )
{
if ( $this->checkIsString( $value ) )
{
$val = strval( $this->getValue( $value ) );

return boolval( preg_match( $regex, $val ) );
}
else
{
return false;
}
return $this->getStringValidator()->matchesRegex( $this->getValue( $value ), $regex );
}

/**
Expand All @@ -602,16 +605,7 @@ protected function checkMatchesRegex( $value, $regex )
*/
protected function checkHasLength( $value, $length )
{
if ( $this->checkIsString( $value ) )
{
$val = strval( $this->getValue( $value ) );

return (mb_strlen( $val, '8bit' ) == $length);
}
else
{
return false;
}
return $this->getStringValidator()->hasLength( $this->getValue( $value ), $length );
}

/**
Expand All @@ -622,16 +616,7 @@ protected function checkHasLength( $value, $length )
*/
protected function checkHasMinLength( $value, $minLength )
{
if ( $this->checkIsString( $value ) )
{
$val = strval( $this->getValue( $value ) );

return (mb_strlen( $val, '8bit' ) >= $minLength);
}
else
{
return false;
}
return $this->getStringValidator()->hasMinLength( $this->getValue( $value ), $minLength );
}

/**
Expand All @@ -642,16 +627,7 @@ protected function checkHasMinLength( $value, $minLength )
*/
protected function checkHasMaxLength( $value, $maxLength )
{
if ( $this->checkIsString( $value ) )
{
$val = strval( $this->getValue( $value ) );

return (mb_strlen( $val, '8bit' ) <= $maxLength);
}
else
{
return false;
}
return $this->getStringValidator()->hasMaxLength( $this->getValue( $value ), $maxLength );
}

/**
Expand Down Expand Up @@ -679,7 +655,7 @@ protected function checkCounts( $value, $count )
*/
protected function checkIsEmail( $value )
{
return ( new StringValidator() )->isEmail( $this->getValue( $value ) );
return $this->getStringValidator()->isEmail( $this->getValue( $value ) );
}

/**
Expand All @@ -689,7 +665,7 @@ protected function checkIsEmail( $value )
*/
protected function checkIsUrl( $value )
{
return ( new StringValidator() )->isUrl( $this->getValue( $value ) );
return $this->getStringValidator()->isUrl( $this->getValue( $value ) );
}

/**
Expand All @@ -699,7 +675,7 @@ protected function checkIsUrl( $value )
*/
protected function checkIsJson( $value )
{
return ( new StringValidator() )->isJson( $this->getValue( $value ) );
return $this->getStringValidator()->isJson( $this->getValue( $value ) );
}

/**
Expand Down

0 comments on commit 8a56467

Please sign in to comment.