Skip to content

Commit 3783e2d

Browse files
committed
use PHP intl extension instead of mbstring
1 parent 98663dc commit 3783e2d

File tree

3 files changed

+58
-72
lines changed

3 files changed

+58
-72
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"keywords": ["assert", "assertion", "validation"],
99
"require": {
1010
"php": ">=5.3",
11-
"ext-mbstring": "*"
11+
"ext-intl": "*"
1212
},
1313
"require-dev": {
1414
"phpunit/phpunit": "@stable"

lib/Assert/Assertion.php

Lines changed: 57 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@
3636
* @method static void nullOrNotNull($value, $message = null, $propertyPath = null)
3737
* @method static void nullOrString($value, $message = null, $propertyPath = null)
3838
* @method static void nullOrRegex($value, $pattern, $message = null, $propertyPath = null)
39-
* @method static void nullOrLength($value, $length, $message = null, $propertyPath = null, $encoding = "utf8")
40-
* @method static void nullOrMinLength($value, $minLength, $message = null, $propertyPath = null, $encoding = "utf8")
41-
* @method static void nullOrMaxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8")
42-
* @method static void nullOrBetweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8")
43-
* @method static void nullOrStartsWith($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8")
44-
* @method static void nullOrEndsWith($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8")
45-
* @method static void nullOrContains($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8")
39+
* @method static void nullOrLength($value, $length, $message = null, $propertyPath = null)
40+
* @method static void nullOrMinLength($value, $minLength, $message = null, $propertyPath = null)
41+
* @method static void nullOrMaxLength($value, $maxLength, $message = null, $propertyPath = null)
42+
* @method static void nullOrBetweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null)
43+
* @method static void nullOrStartsWith($string, $needle, $message = null, $propertyPath = null)
44+
* @method static void nullOrEndsWith($string, $needle, $message = null, $propertyPath = null)
45+
* @method static void nullOrContains($string, $needle, $message = null, $propertyPath = null)
4646
* @method static void nullOrChoice($value, $choices, $message = null, $propertyPath = null)
4747
* @method static void nullOrInArray($value, $choices, $message = null, $propertyPath = null)
4848
* @method static void nullOrNumeric($value, $message = null, $propertyPath = null)
@@ -92,13 +92,13 @@
9292
* @method static void allNotNull($value, $message = null, $propertyPath = null)
9393
* @method static void allString($value, $message = null, $propertyPath = null)
9494
* @method static void allRegex($value, $pattern, $message = null, $propertyPath = null)
95-
* @method static void allLength($value, $length, $message = null, $propertyPath = null, $encoding = "utf8")
96-
* @method static void allMinLength($value, $minLength, $message = null, $propertyPath = null, $encoding = "utf8")
97-
* @method static void allMaxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8")
98-
* @method static void allBetweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8")
99-
* @method static void allStartsWith($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8")
100-
* @method static void allEndsWith($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8")
101-
* @method static void allContains($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8")
95+
* @method static void allLength($value, $length, $message = null, $propertyPath = null)
96+
* @method static void allMinLength($value, $minLength, $message = null, $propertyPath = null)
97+
* @method static void allMaxLength($value, $maxLength, $message = null, $propertyPath = null)
98+
* @method static void allBetweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null)
99+
* @method static void allStartsWith($string, $needle, $message = null, $propertyPath = null)
100+
* @method static void allEndsWith($string, $needle, $message = null, $propertyPath = null)
101+
* @method static void allContains($string, $needle, $message = null, $propertyPath = null)
102102
* @method static void allChoice($value, $choices, $message = null, $propertyPath = null)
103103
* @method static void allInArray($value, $choices, $message = null, $propertyPath = null)
104104
* @method static void allNumeric($value, $message = null, $propertyPath = null)
@@ -132,7 +132,6 @@
132132
* @method static void allChoicesNotEmpty($values, $choices, $message = null, $propertyPath = null)
133133
* @method static void allMethodExists($value, $object, $message = null, $propertyPath = null)
134134
* @method static void allIsObject($value, $message = null, $propertyPath = null)
135-
* @method static void allDate($value, $format, $message = null, $propertyPath = null)
136135
* METHODEND
137136
*/
138137
class Assertion
@@ -543,23 +542,22 @@ public static function regex($value, $pattern, $message = null, $propertyPath =
543542
* @param int $length
544543
* @param string|null $message
545544
* @param string|null $propertyPath
546-
* @param string $encoding
547545
* @return void
548546
* @throws \Assert\AssertionFailedException
549547
*/
550-
public static function length($value, $length, $message = null, $propertyPath = null, $encoding = 'utf8')
548+
public static function length($value, $length, $message = null, $propertyPath = null)
551549
{
552550
static::string($value, $message, $propertyPath);
553551

554-
if (mb_strlen($value, $encoding) !== $length) {
552+
if (grapheme_strlen($value) !== $length) {
555553
$message = sprintf(
556554
$message ?: 'Value "%s" has to be %d exactly characters long, but length is %d.',
557555
self::stringify($value),
558556
$length,
559-
mb_strlen($value, $encoding)
557+
grapheme_strlen($value)
560558
);
561559

562-
$constraints = array('length' => $length, 'encoding' => $encoding);
560+
$constraints = array('length' => $length);
563561
throw static::createException($value, $message, static::INVALID_LENGTH, $propertyPath, $constraints);
564562
}
565563
}
@@ -571,23 +569,22 @@ public static function length($value, $length, $message = null, $propertyPath =
571569
* @param int $minLength
572570
* @param string|null $message
573571
* @param string|null $propertyPath
574-
* @param string $encoding
575572
* @return void
576573
* @throws \Assert\AssertionFailedException
577574
*/
578-
public static function minLength($value, $minLength, $message = null, $propertyPath = null, $encoding = 'utf8')
575+
public static function minLength($value, $minLength, $message = null, $propertyPath = null)
579576
{
580577
static::string($value, $message, $propertyPath);
581578

582-
if (mb_strlen($value, $encoding) < $minLength) {
579+
if (grapheme_strlen($value) < $minLength) {
583580
$message = sprintf(
584581
$message ?: 'Value "%s" is too short, it should have more than %d characters, but only has %d characters.',
585582
self::stringify($value),
586583
$minLength,
587-
mb_strlen($value, $encoding)
584+
grapheme_strlen($value)
588585
);
589586

590-
$constraints = array('min_length' => $minLength, 'encoding' => $encoding);
587+
$constraints = array('min_length' => $minLength);
591588
throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints);
592589
}
593590
}
@@ -599,23 +596,22 @@ public static function minLength($value, $minLength, $message = null, $propertyP
599596
* @param integer $maxLength
600597
* @param string|null $message
601598
* @param string|null $propertyPath
602-
* @param string $encoding
603599
* @return void
604600
* @throws \Assert\AssertionFailedException
605601
*/
606-
public static function maxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8')
602+
public static function maxLength($value, $maxLength, $message = null, $propertyPath = null)
607603
{
608604
static::string($value, $message, $propertyPath);
609605

610-
if (mb_strlen($value, $encoding) > $maxLength) {
606+
if (grapheme_strlen($value) > $maxLength) {
611607
$message = sprintf(
612608
$message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.',
613609
self::stringify($value),
614610
$maxLength,
615-
mb_strlen($value, $encoding)
611+
grapheme_strlen($value)
616612
);
617613

618-
$constraints = array('max_length' => $maxLength, 'encoding' => $encoding);
614+
$constraints = array('max_length' => $maxLength);
619615
throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints);
620616
}
621617
}
@@ -628,35 +624,34 @@ public static function maxLength($value, $maxLength, $message = null, $propertyP
628624
* @param integer $maxLength
629625
* @param string|null $message
630626
* @param string|null $propertyPath
631-
* @param string $encoding
632627
* @return void
633628
* @throws \Assert\AssertionFailedException
634629
*/
635-
public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8')
630+
public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null)
636631
{
637632
static::string($value, $message, $propertyPath);
638633

639-
if (mb_strlen($value, $encoding) < $minLength) {
634+
if (grapheme_strlen($value) < $minLength) {
640635
$message = sprintf(
641636
$message ?: 'Value "%s" is too short, it should have more than %d characters, but only has %d characters.',
642637
self::stringify($value),
643638
$minLength,
644-
mb_strlen($value, $encoding)
639+
grapheme_strlen($value)
645640
);
646641

647-
$constraints = array('min_length' => $minLength, 'encoding' => $encoding);
642+
$constraints = array('min_length' => $minLength);
648643
throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints);
649644
}
650645

651-
if (mb_strlen($value, $encoding) > $maxLength) {
646+
if (grapheme_strlen($value) > $maxLength) {
652647
$message = sprintf(
653648
$message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.',
654649
self::stringify($value),
655650
$maxLength,
656-
mb_strlen($value, $encoding)
651+
grapheme_strlen($value)
657652
);
658653

659-
$constraints = array('max_length' => $maxLength, 'encoding' => $encoding);
654+
$constraints = array('max_length' => $maxLength);
660655
throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints);
661656
}
662657
}
@@ -668,22 +663,21 @@ public static function betweenLength($value, $minLength, $maxLength, $message =
668663
* @param string $needle
669664
* @param string|null $message
670665
* @param string|null $propertyPath
671-
* @param string $encoding
672666
* @return void
673667
* @throws \Assert\AssertionFailedException
674668
*/
675-
public static function startsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
669+
public static function startsWith($string, $needle, $message = null, $propertyPath = null)
676670
{
677671
static::string($string, $message, $propertyPath);
678672

679-
if (mb_strpos($string, $needle, null, $encoding) !== 0) {
673+
if (grapheme_strpos($string, $needle, null) !== 0) {
680674
$message = sprintf(
681675
$message ?: 'Value "%s" does not start with "%s".',
682676
self::stringify($string),
683677
self::stringify($needle)
684678
);
685679

686-
$constraints = array('needle' => $needle, 'encoding' => $encoding);
680+
$constraints = array('needle' => $needle);
687681
throw static::createException($string, $message, static::INVALID_STRING_START, $propertyPath, $constraints);
688682
}
689683
}
@@ -695,24 +689,23 @@ public static function startsWith($string, $needle, $message = null, $propertyPa
695689
* @param string $needle
696690
* @param string|null $message
697691
* @param string|null $propertyPath
698-
* @param string $encoding
699692
* @return void
700693
* @throws \Assert\AssertionFailedException
701694
*/
702-
public static function endsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
695+
public static function endsWith($string, $needle, $message = null, $propertyPath = null)
703696
{
704697
static::string($string, $message, $propertyPath);
705698

706-
$stringPosition = mb_strlen($string, $encoding) - mb_strlen($needle, $encoding);
699+
$stringPosition = grapheme_strlen($string) - grapheme_strlen($needle);
707700

708-
if (mb_strripos($string, $needle, null, $encoding) !== $stringPosition) {
701+
if (grapheme_strripos($string, $needle, null) !== $stringPosition) {
709702
$message = sprintf(
710703
$message ?: 'Value "%s" does not end with "%s".',
711704
self::stringify($string),
712705
self::stringify($needle)
713706
);
714707

715-
$constraints = array('needle' => $needle, 'encoding' => $encoding);
708+
$constraints = array('needle' => $needle);
716709
throw static::createException($string, $message, static::INVALID_STRING_END, $propertyPath, $constraints);
717710
}
718711
}
@@ -724,22 +717,21 @@ public static function endsWith($string, $needle, $message = null, $propertyPath
724717
* @param string $needle
725718
* @param string|null $message
726719
* @param string|null $propertyPath
727-
* @param string $encoding
728720
* @return void
729721
* @throws \Assert\AssertionFailedException
730722
*/
731-
public static function contains($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
723+
public static function contains($string, $needle, $message = null, $propertyPath = null)
732724
{
733725
static::string($string, $message, $propertyPath);
734726

735-
if (mb_strpos($string, $needle, null, $encoding) === false) {
727+
if (grapheme_strpos($string, $needle, null) === false) {
736728
$message = sprintf(
737729
$message ?: 'Value "%s" does not contain "%s".',
738730
self::stringify($string),
739731
self::stringify($needle)
740732
);
741733

742-
$constraints = array('needle' => $needle, 'encoding' => $encoding);
734+
$constraints = array('needle' => $needle);
743735
throw static::createException($string, $message, static::INVALID_STRING_CONTAINS, $propertyPath, $constraints);
744736
}
745737
}
@@ -1657,23 +1649,23 @@ public static function greaterOrEqualThan($value, $limit, $message = null, $prop
16571649
*
16581650
* @link http://php.net/manual/function.date.php#refsect1-function.date-parameters
16591651
*/
1660-
public static function date($value, $format, $message = null, $propertyPath = null)
1661-
{
1662-
static::string($value, $message, $propertyPath);
1663-
static::string($format, $message, $propertyPath);
1652+
public static function date($value, $format, $message = null, $propertyPath = null)
1653+
{
1654+
static::string($value, $message, $propertyPath);
1655+
static::string($format, $message, $propertyPath);
16641656

1665-
$dateTime = \DateTime::createFromFormat($format, $value);
1657+
$dateTime = \DateTime::createFromFormat($format, $value);
16661658

1667-
if (false === $dateTime || $value !== $dateTime->format($format)) {
1668-
$message = sprintf(
1669-
$message ?: 'Date "%s" is invalid or does not match format "%s".',
1670-
self::stringify($value),
1671-
self::stringify($format)
1672-
);
1659+
if (false === $dateTime || $value !== $dateTime->format($format)) {
1660+
$message = sprintf(
1661+
$message ?: 'Date "%s" is invalid or does not match format "%s".',
1662+
self::stringify($value),
1663+
self::stringify($format)
1664+
);
16731665

1674-
throw static::createException($value, $message, static::INVALID_DATE, $propertyPath, array('format' => $format));
1675-
}
1676-
}
1666+
throw static::createException($value, $message, static::INVALID_DATE, $propertyPath, array('format' => $format));
1667+
}
1668+
}
16771669

16781670
/**
16791671
* Make a string version of a value.

tests/Assert/Tests/AssertTest.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -681,12 +681,6 @@ public function testLengthFailed()
681681
Assertion::length("asdf", 3);
682682
}
683683

684-
public function testLengthFailedForWrongEncoding()
685-
{
686-
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_LENGTH);
687-
Assertion::length("", 1, null, null, 'ASCII');
688-
}
689-
690684
public function testLengthValidForGivenEncoding()
691685
{
692686
Assertion::length("", 1, null, null, 'utf8');

0 commit comments

Comments
 (0)