Skip to content

Commit

Permalink
use PHP intl extension instead of mbstring
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrokeil committed Dec 28, 2015
1 parent 98663dc commit 100abc6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 125 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"keywords": ["assert", "assertion", "validation"],
"require": {
"php": ">=5.3",
"ext-mbstring": "*"
"ext-intl": "*"
},
"require-dev": {
"phpunit/phpunit": "@stable"
Expand Down
125 changes: 43 additions & 82 deletions lib/Assert/Assertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
* @method static void nullOrNotNull($value, $message = null, $propertyPath = null)
* @method static void nullOrString($value, $message = null, $propertyPath = null)
* @method static void nullOrRegex($value, $pattern, $message = null, $propertyPath = null)
* @method static void nullOrLength($value, $length, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void nullOrMinLength($value, $minLength, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void nullOrMaxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void nullOrBetweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void nullOrStartsWith($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void nullOrEndsWith($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void nullOrContains($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void nullOrLength($value, $length, $message = null, $propertyPath = null)
* @method static void nullOrMinLength($value, $minLength, $message = null, $propertyPath = null)
* @method static void nullOrMaxLength($value, $maxLength, $message = null, $propertyPath = null)
* @method static void nullOrBetweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null)
* @method static void nullOrStartsWith($string, $needle, $message = null, $propertyPath = null)
* @method static void nullOrEndsWith($string, $needle, $message = null, $propertyPath = null)
* @method static void nullOrContains($string, $needle, $message = null, $propertyPath = null)
* @method static void nullOrChoice($value, $choices, $message = null, $propertyPath = null)
* @method static void nullOrInArray($value, $choices, $message = null, $propertyPath = null)
* @method static void nullOrNumeric($value, $message = null, $propertyPath = null)
Expand Down Expand Up @@ -76,7 +76,6 @@
* @method static void nullOrChoicesNotEmpty($values, $choices, $message = null, $propertyPath = null)
* @method static void nullOrMethodExists($value, $object, $message = null, $propertyPath = null)
* @method static void nullOrIsObject($value, $message = null, $propertyPath = null)
* @method static void nullOrDate($value, $format, $message = null, $propertyPath = null)
* @method static void allEq($value, $value2, $message = null, $propertyPath = null)
* @method static void allSame($value, $value2, $message = null, $propertyPath = null)
* @method static void allNotEq($value1, $value2, $message = null, $propertyPath = null)
Expand All @@ -92,13 +91,13 @@
* @method static void allNotNull($value, $message = null, $propertyPath = null)
* @method static void allString($value, $message = null, $propertyPath = null)
* @method static void allRegex($value, $pattern, $message = null, $propertyPath = null)
* @method static void allLength($value, $length, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void allMinLength($value, $minLength, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void allMaxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void allBetweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void allStartsWith($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void allEndsWith($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void allContains($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void allLength($value, $length, $message = null, $propertyPath = null)
* @method static void allMinLength($value, $minLength, $message = null, $propertyPath = null)
* @method static void allMaxLength($value, $maxLength, $message = null, $propertyPath = null)
* @method static void allBetweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null)
* @method static void allStartsWith($string, $needle, $message = null, $propertyPath = null)
* @method static void allEndsWith($string, $needle, $message = null, $propertyPath = null)
* @method static void allContains($string, $needle, $message = null, $propertyPath = null)
* @method static void allChoice($value, $choices, $message = null, $propertyPath = null)
* @method static void allInArray($value, $choices, $message = null, $propertyPath = null)
* @method static void allNumeric($value, $message = null, $propertyPath = null)
Expand Down Expand Up @@ -132,7 +131,6 @@
* @method static void allChoicesNotEmpty($values, $choices, $message = null, $propertyPath = null)
* @method static void allMethodExists($value, $object, $message = null, $propertyPath = null)
* @method static void allIsObject($value, $message = null, $propertyPath = null)
* @method static void allDate($value, $format, $message = null, $propertyPath = null)
* METHODEND
*/
class Assertion
Expand Down Expand Up @@ -192,7 +190,6 @@ class Assertion
const INVALID_LESS_OR_EQUAL = 211;
const INVALID_GREATER = 212;
const INVALID_GREATER_OR_EQUAL = 212;
const INVALID_DATE = 213;

/**
* Exception to throw when an assertion failed.
Expand Down Expand Up @@ -543,23 +540,22 @@ public static function regex($value, $pattern, $message = null, $propertyPath =
* @param int $length
* @param string|null $message
* @param string|null $propertyPath
* @param string $encoding
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function length($value, $length, $message = null, $propertyPath = null, $encoding = 'utf8')
public static function length($value, $length, $message = null, $propertyPath = null)
{
static::string($value, $message, $propertyPath);

if (mb_strlen($value, $encoding) !== $length) {
if (grapheme_strlen($value) !== $length) {
$message = sprintf(
$message ?: 'Value "%s" has to be %d exactly characters long, but length is %d.',
self::stringify($value),
$length,
mb_strlen($value, $encoding)
grapheme_strlen($value)
);

$constraints = array('length' => $length, 'encoding' => $encoding);
$constraints = array('length' => $length);
throw static::createException($value, $message, static::INVALID_LENGTH, $propertyPath, $constraints);
}
}
Expand All @@ -571,23 +567,22 @@ public static function length($value, $length, $message = null, $propertyPath =
* @param int $minLength
* @param string|null $message
* @param string|null $propertyPath
* @param string $encoding
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function minLength($value, $minLength, $message = null, $propertyPath = null, $encoding = 'utf8')
public static function minLength($value, $minLength, $message = null, $propertyPath = null)
{
static::string($value, $message, $propertyPath);

if (mb_strlen($value, $encoding) < $minLength) {
if (grapheme_strlen($value) < $minLength) {
$message = sprintf(
$message ?: 'Value "%s" is too short, it should have more than %d characters, but only has %d characters.',
self::stringify($value),
$minLength,
mb_strlen($value, $encoding)
grapheme_strlen($value)
);

$constraints = array('min_length' => $minLength, 'encoding' => $encoding);
$constraints = array('min_length' => $minLength);
throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints);
}
}
Expand All @@ -599,23 +594,22 @@ public static function minLength($value, $minLength, $message = null, $propertyP
* @param integer $maxLength
* @param string|null $message
* @param string|null $propertyPath
* @param string $encoding
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function maxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8')
public static function maxLength($value, $maxLength, $message = null, $propertyPath = null)
{
static::string($value, $message, $propertyPath);

if (mb_strlen($value, $encoding) > $maxLength) {
if (grapheme_strlen($value) > $maxLength) {
$message = sprintf(
$message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.',
self::stringify($value),
$maxLength,
mb_strlen($value, $encoding)
grapheme_strlen($value)
);

$constraints = array('max_length' => $maxLength, 'encoding' => $encoding);
$constraints = array('max_length' => $maxLength);
throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints);
}
}
Expand All @@ -628,35 +622,34 @@ public static function maxLength($value, $maxLength, $message = null, $propertyP
* @param integer $maxLength
* @param string|null $message
* @param string|null $propertyPath
* @param string $encoding
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8')
public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null)
{
static::string($value, $message, $propertyPath);

if (mb_strlen($value, $encoding) < $minLength) {
if (grapheme_strlen($value) < $minLength) {
$message = sprintf(
$message ?: 'Value "%s" is too short, it should have more than %d characters, but only has %d characters.',
self::stringify($value),
$minLength,
mb_strlen($value, $encoding)
grapheme_strlen($value)
);

$constraints = array('min_length' => $minLength, 'encoding' => $encoding);
$constraints = array('min_length' => $minLength);
throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints);
}

if (mb_strlen($value, $encoding) > $maxLength) {
if (grapheme_strlen($value) > $maxLength) {
$message = sprintf(
$message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.',
self::stringify($value),
$maxLength,
mb_strlen($value, $encoding)
grapheme_strlen($value)
);

$constraints = array('max_length' => $maxLength, 'encoding' => $encoding);
$constraints = array('max_length' => $maxLength);
throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints);
}
}
Expand All @@ -668,22 +661,21 @@ public static function betweenLength($value, $minLength, $maxLength, $message =
* @param string $needle
* @param string|null $message
* @param string|null $propertyPath
* @param string $encoding
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function startsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
public static function startsWith($string, $needle, $message = null, $propertyPath = null)
{
static::string($string, $message, $propertyPath);

if (mb_strpos($string, $needle, null, $encoding) !== 0) {
if (grapheme_strpos($string, $needle, null) !== 0) {
$message = sprintf(
$message ?: 'Value "%s" does not start with "%s".',
self::stringify($string),
self::stringify($needle)
);

$constraints = array('needle' => $needle, 'encoding' => $encoding);
$constraints = array('needle' => $needle);
throw static::createException($string, $message, static::INVALID_STRING_START, $propertyPath, $constraints);
}
}
Expand All @@ -695,24 +687,23 @@ public static function startsWith($string, $needle, $message = null, $propertyPa
* @param string $needle
* @param string|null $message
* @param string|null $propertyPath
* @param string $encoding
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function endsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
public static function endsWith($string, $needle, $message = null, $propertyPath = null)
{
static::string($string, $message, $propertyPath);

$stringPosition = mb_strlen($string, $encoding) - mb_strlen($needle, $encoding);
$stringPosition = grapheme_strlen($string) - grapheme_strlen($needle);

if (mb_strripos($string, $needle, null, $encoding) !== $stringPosition) {
if (grapheme_strripos($string, $needle, null) !== $stringPosition) {
$message = sprintf(
$message ?: 'Value "%s" does not end with "%s".',
self::stringify($string),
self::stringify($needle)
);

$constraints = array('needle' => $needle, 'encoding' => $encoding);
$constraints = array('needle' => $needle);
throw static::createException($string, $message, static::INVALID_STRING_END, $propertyPath, $constraints);
}
}
Expand All @@ -724,22 +715,21 @@ public static function endsWith($string, $needle, $message = null, $propertyPath
* @param string $needle
* @param string|null $message
* @param string|null $propertyPath
* @param string $encoding
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function contains($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
public static function contains($string, $needle, $message = null, $propertyPath = null)
{
static::string($string, $message, $propertyPath);

if (mb_strpos($string, $needle, null, $encoding) === false) {
if (grapheme_strpos($string, $needle, null) === false) {
$message = sprintf(
$message ?: 'Value "%s" does not contain "%s".',
self::stringify($string),
self::stringify($needle)
);

$constraints = array('needle' => $needle, 'encoding' => $encoding);
$constraints = array('needle' => $needle);
throw static::createException($string, $message, static::INVALID_STRING_CONTAINS, $propertyPath, $constraints);
}
}
Expand Down Expand Up @@ -1646,35 +1636,6 @@ public static function greaterOrEqualThan($value, $limit, $message = null, $prop
}
}

/**
* Assert that date is valid and corresponds to the given format.
*
* @param string $value
* @param string $format supports all of the options date(), except for the following:
* N, w, W, t, L, o, B, a, A, g, h, I, O, P, Z, c, r.
* @param string|null $message
* @param string|null $propertyPath
*
* @link http://php.net/manual/function.date.php#refsect1-function.date-parameters
*/
public static function date($value, $format, $message = null, $propertyPath = null)
{
static::string($value, $message, $propertyPath);
static::string($format, $message, $propertyPath);

$dateTime = \DateTime::createFromFormat($format, $value);

if (false === $dateTime || $value !== $dateTime->format($format)) {
$message = sprintf(
$message ?: 'Date "%s" is invalid or does not match format "%s".',
self::stringify($value),
self::stringify($format)
);

throw static::createException($value, $message, static::INVALID_DATE, $propertyPath, array('format' => $format));
}
}

/**
* Make a string version of a value.
*
Expand Down
42 changes: 0 additions & 42 deletions tests/Assert/Tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -681,12 +681,6 @@ public function testLengthFailed()
Assertion::length("asdf", 3);
}

public function testLengthFailedForWrongEncoding()
{
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_LENGTH);
Assertion::length("", 1, null, null, 'ASCII');
}

public function testLengthValidForGivenEncoding()
{
Assertion::length("", 1, null, null, 'utf8');
Expand Down Expand Up @@ -1059,24 +1053,6 @@ public function invalidGreaterProvider()
);
}

/**
* @dataProvider validDateProvider
*/
public function testValidDate($value, $format)
{
Assertion::date($value, $format);
}

public function validDateProvider()
{
return array(
array('2012-03-13', 'Y-m-d'),
array('29.02.2012 12:03:36.432563', 'd.m.Y H:i:s.u'),
array('13.08.2015 17:08:23 Thu Thursday th 224 August Aug 8 15 17 432563 UTC UTC', 'd.m.Y H:i:s D l S z F M n y H u e T'),
array('1439486158', 'U')
);
}

/**
* @dataProvider invalidGreaterProvider
*/
Expand Down Expand Up @@ -1117,24 +1093,6 @@ public function testGreaterOrEqualThanThrowsException($value, $limit)
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_GREATER_OR_EQUAL);
Assertion::greaterOrEqualThan($value, $limit);
}

/**
* @dataProvider invalidDateProvider
*/
public function testInvalidDate($value, $format)
{
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_DATE);
Assertion::date($value, $format);
}

public function invalidDateProvider()
{
return array(
array('this is not the date', 'Y-m-d'),
array('2011-02-29', 'Y-m-d'),
array('2012.02.29 12:60:36.432563', 'Y.m.d H:i:s.u')
);
}
}

class ChildStdClass extends \stdClass
Expand Down

0 comments on commit 100abc6

Please sign in to comment.