Skip to content

Commit 100abc6

Browse files
committed
use PHP intl extension instead of mbstring
1 parent 98663dc commit 100abc6

File tree

3 files changed

+44
-125
lines changed

3 files changed

+44
-125
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: 43 additions & 82 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)
@@ -76,7 +76,6 @@
7676
* @method static void nullOrChoicesNotEmpty($values, $choices, $message = null, $propertyPath = null)
7777
* @method static void nullOrMethodExists($value, $object, $message = null, $propertyPath = null)
7878
* @method static void nullOrIsObject($value, $message = null, $propertyPath = null)
79-
* @method static void nullOrDate($value, $format, $message = null, $propertyPath = null)
8079
* @method static void allEq($value, $value2, $message = null, $propertyPath = null)
8180
* @method static void allSame($value, $value2, $message = null, $propertyPath = null)
8281
* @method static void allNotEq($value1, $value2, $message = null, $propertyPath = null)
@@ -92,13 +91,13 @@
9291
* @method static void allNotNull($value, $message = null, $propertyPath = null)
9392
* @method static void allString($value, $message = null, $propertyPath = null)
9493
* @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")
94+
* @method static void allLength($value, $length, $message = null, $propertyPath = null)
95+
* @method static void allMinLength($value, $minLength, $message = null, $propertyPath = null)
96+
* @method static void allMaxLength($value, $maxLength, $message = null, $propertyPath = null)
97+
* @method static void allBetweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null)
98+
* @method static void allStartsWith($string, $needle, $message = null, $propertyPath = null)
99+
* @method static void allEndsWith($string, $needle, $message = null, $propertyPath = null)
100+
* @method static void allContains($string, $needle, $message = null, $propertyPath = null)
102101
* @method static void allChoice($value, $choices, $message = null, $propertyPath = null)
103102
* @method static void allInArray($value, $choices, $message = null, $propertyPath = null)
104103
* @method static void allNumeric($value, $message = null, $propertyPath = null)
@@ -132,7 +131,6 @@
132131
* @method static void allChoicesNotEmpty($values, $choices, $message = null, $propertyPath = null)
133132
* @method static void allMethodExists($value, $object, $message = null, $propertyPath = null)
134133
* @method static void allIsObject($value, $message = null, $propertyPath = null)
135-
* @method static void allDate($value, $format, $message = null, $propertyPath = null)
136134
* METHODEND
137135
*/
138136
class Assertion
@@ -192,7 +190,6 @@ class Assertion
192190
const INVALID_LESS_OR_EQUAL = 211;
193191
const INVALID_GREATER = 212;
194192
const INVALID_GREATER_OR_EQUAL = 212;
195-
const INVALID_DATE = 213;
196193

197194
/**
198195
* Exception to throw when an assertion failed.
@@ -543,23 +540,22 @@ public static function regex($value, $pattern, $message = null, $propertyPath =
543540
* @param int $length
544541
* @param string|null $message
545542
* @param string|null $propertyPath
546-
* @param string $encoding
547543
* @return void
548544
* @throws \Assert\AssertionFailedException
549545
*/
550-
public static function length($value, $length, $message = null, $propertyPath = null, $encoding = 'utf8')
546+
public static function length($value, $length, $message = null, $propertyPath = null)
551547
{
552548
static::string($value, $message, $propertyPath);
553549

554-
if (mb_strlen($value, $encoding) !== $length) {
550+
if (grapheme_strlen($value) !== $length) {
555551
$message = sprintf(
556552
$message ?: 'Value "%s" has to be %d exactly characters long, but length is %d.',
557553
self::stringify($value),
558554
$length,
559-
mb_strlen($value, $encoding)
555+
grapheme_strlen($value)
560556
);
561557

562-
$constraints = array('length' => $length, 'encoding' => $encoding);
558+
$constraints = array('length' => $length);
563559
throw static::createException($value, $message, static::INVALID_LENGTH, $propertyPath, $constraints);
564560
}
565561
}
@@ -571,23 +567,22 @@ public static function length($value, $length, $message = null, $propertyPath =
571567
* @param int $minLength
572568
* @param string|null $message
573569
* @param string|null $propertyPath
574-
* @param string $encoding
575570
* @return void
576571
* @throws \Assert\AssertionFailedException
577572
*/
578-
public static function minLength($value, $minLength, $message = null, $propertyPath = null, $encoding = 'utf8')
573+
public static function minLength($value, $minLength, $message = null, $propertyPath = null)
579574
{
580575
static::string($value, $message, $propertyPath);
581576

582-
if (mb_strlen($value, $encoding) < $minLength) {
577+
if (grapheme_strlen($value) < $minLength) {
583578
$message = sprintf(
584579
$message ?: 'Value "%s" is too short, it should have more than %d characters, but only has %d characters.',
585580
self::stringify($value),
586581
$minLength,
587-
mb_strlen($value, $encoding)
582+
grapheme_strlen($value)
588583
);
589584

590-
$constraints = array('min_length' => $minLength, 'encoding' => $encoding);
585+
$constraints = array('min_length' => $minLength);
591586
throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints);
592587
}
593588
}
@@ -599,23 +594,22 @@ public static function minLength($value, $minLength, $message = null, $propertyP
599594
* @param integer $maxLength
600595
* @param string|null $message
601596
* @param string|null $propertyPath
602-
* @param string $encoding
603597
* @return void
604598
* @throws \Assert\AssertionFailedException
605599
*/
606-
public static function maxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8')
600+
public static function maxLength($value, $maxLength, $message = null, $propertyPath = null)
607601
{
608602
static::string($value, $message, $propertyPath);
609603

610-
if (mb_strlen($value, $encoding) > $maxLength) {
604+
if (grapheme_strlen($value) > $maxLength) {
611605
$message = sprintf(
612606
$message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.',
613607
self::stringify($value),
614608
$maxLength,
615-
mb_strlen($value, $encoding)
609+
grapheme_strlen($value)
616610
);
617611

618-
$constraints = array('max_length' => $maxLength, 'encoding' => $encoding);
612+
$constraints = array('max_length' => $maxLength);
619613
throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints);
620614
}
621615
}
@@ -628,35 +622,34 @@ public static function maxLength($value, $maxLength, $message = null, $propertyP
628622
* @param integer $maxLength
629623
* @param string|null $message
630624
* @param string|null $propertyPath
631-
* @param string $encoding
632625
* @return void
633626
* @throws \Assert\AssertionFailedException
634627
*/
635-
public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8')
628+
public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null)
636629
{
637630
static::string($value, $message, $propertyPath);
638631

639-
if (mb_strlen($value, $encoding) < $minLength) {
632+
if (grapheme_strlen($value) < $minLength) {
640633
$message = sprintf(
641634
$message ?: 'Value "%s" is too short, it should have more than %d characters, but only has %d characters.',
642635
self::stringify($value),
643636
$minLength,
644-
mb_strlen($value, $encoding)
637+
grapheme_strlen($value)
645638
);
646639

647-
$constraints = array('min_length' => $minLength, 'encoding' => $encoding);
640+
$constraints = array('min_length' => $minLength);
648641
throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints);
649642
}
650643

651-
if (mb_strlen($value, $encoding) > $maxLength) {
644+
if (grapheme_strlen($value) > $maxLength) {
652645
$message = sprintf(
653646
$message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.',
654647
self::stringify($value),
655648
$maxLength,
656-
mb_strlen($value, $encoding)
649+
grapheme_strlen($value)
657650
);
658651

659-
$constraints = array('max_length' => $maxLength, 'encoding' => $encoding);
652+
$constraints = array('max_length' => $maxLength);
660653
throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints);
661654
}
662655
}
@@ -668,22 +661,21 @@ public static function betweenLength($value, $minLength, $maxLength, $message =
668661
* @param string $needle
669662
* @param string|null $message
670663
* @param string|null $propertyPath
671-
* @param string $encoding
672664
* @return void
673665
* @throws \Assert\AssertionFailedException
674666
*/
675-
public static function startsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
667+
public static function startsWith($string, $needle, $message = null, $propertyPath = null)
676668
{
677669
static::string($string, $message, $propertyPath);
678670

679-
if (mb_strpos($string, $needle, null, $encoding) !== 0) {
671+
if (grapheme_strpos($string, $needle, null) !== 0) {
680672
$message = sprintf(
681673
$message ?: 'Value "%s" does not start with "%s".',
682674
self::stringify($string),
683675
self::stringify($needle)
684676
);
685677

686-
$constraints = array('needle' => $needle, 'encoding' => $encoding);
678+
$constraints = array('needle' => $needle);
687679
throw static::createException($string, $message, static::INVALID_STRING_START, $propertyPath, $constraints);
688680
}
689681
}
@@ -695,24 +687,23 @@ public static function startsWith($string, $needle, $message = null, $propertyPa
695687
* @param string $needle
696688
* @param string|null $message
697689
* @param string|null $propertyPath
698-
* @param string $encoding
699690
* @return void
700691
* @throws \Assert\AssertionFailedException
701692
*/
702-
public static function endsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
693+
public static function endsWith($string, $needle, $message = null, $propertyPath = null)
703694
{
704695
static::string($string, $message, $propertyPath);
705696

706-
$stringPosition = mb_strlen($string, $encoding) - mb_strlen($needle, $encoding);
697+
$stringPosition = grapheme_strlen($string) - grapheme_strlen($needle);
707698

708-
if (mb_strripos($string, $needle, null, $encoding) !== $stringPosition) {
699+
if (grapheme_strripos($string, $needle, null) !== $stringPosition) {
709700
$message = sprintf(
710701
$message ?: 'Value "%s" does not end with "%s".',
711702
self::stringify($string),
712703
self::stringify($needle)
713704
);
714705

715-
$constraints = array('needle' => $needle, 'encoding' => $encoding);
706+
$constraints = array('needle' => $needle);
716707
throw static::createException($string, $message, static::INVALID_STRING_END, $propertyPath, $constraints);
717708
}
718709
}
@@ -724,22 +715,21 @@ public static function endsWith($string, $needle, $message = null, $propertyPath
724715
* @param string $needle
725716
* @param string|null $message
726717
* @param string|null $propertyPath
727-
* @param string $encoding
728718
* @return void
729719
* @throws \Assert\AssertionFailedException
730720
*/
731-
public static function contains($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
721+
public static function contains($string, $needle, $message = null, $propertyPath = null)
732722
{
733723
static::string($string, $message, $propertyPath);
734724

735-
if (mb_strpos($string, $needle, null, $encoding) === false) {
725+
if (grapheme_strpos($string, $needle, null) === false) {
736726
$message = sprintf(
737727
$message ?: 'Value "%s" does not contain "%s".',
738728
self::stringify($string),
739729
self::stringify($needle)
740730
);
741731

742-
$constraints = array('needle' => $needle, 'encoding' => $encoding);
732+
$constraints = array('needle' => $needle);
743733
throw static::createException($string, $message, static::INVALID_STRING_CONTAINS, $propertyPath, $constraints);
744734
}
745735
}
@@ -1646,35 +1636,6 @@ public static function greaterOrEqualThan($value, $limit, $message = null, $prop
16461636
}
16471637
}
16481638

1649-
/**
1650-
* Assert that date is valid and corresponds to the given format.
1651-
*
1652-
* @param string $value
1653-
* @param string $format supports all of the options date(), except for the following:
1654-
* N, w, W, t, L, o, B, a, A, g, h, I, O, P, Z, c, r.
1655-
* @param string|null $message
1656-
* @param string|null $propertyPath
1657-
*
1658-
* @link http://php.net/manual/function.date.php#refsect1-function.date-parameters
1659-
*/
1660-
public static function date($value, $format, $message = null, $propertyPath = null)
1661-
{
1662-
static::string($value, $message, $propertyPath);
1663-
static::string($format, $message, $propertyPath);
1664-
1665-
$dateTime = \DateTime::createFromFormat($format, $value);
1666-
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-
);
1673-
1674-
throw static::createException($value, $message, static::INVALID_DATE, $propertyPath, array('format' => $format));
1675-
}
1676-
}
1677-
16781639
/**
16791640
* Make a string version of a value.
16801641
*

tests/Assert/Tests/AssertTest.php

Lines changed: 0 additions & 42 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');
@@ -1059,24 +1053,6 @@ public function invalidGreaterProvider()
10591053
);
10601054
}
10611055

1062-
/**
1063-
* @dataProvider validDateProvider
1064-
*/
1065-
public function testValidDate($value, $format)
1066-
{
1067-
Assertion::date($value, $format);
1068-
}
1069-
1070-
public function validDateProvider()
1071-
{
1072-
return array(
1073-
array('2012-03-13', 'Y-m-d'),
1074-
array('29.02.2012 12:03:36.432563', 'd.m.Y H:i:s.u'),
1075-
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'),
1076-
array('1439486158', 'U')
1077-
);
1078-
}
1079-
10801056
/**
10811057
* @dataProvider invalidGreaterProvider
10821058
*/
@@ -1117,24 +1093,6 @@ public function testGreaterOrEqualThanThrowsException($value, $limit)
11171093
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_GREATER_OR_EQUAL);
11181094
Assertion::greaterOrEqualThan($value, $limit);
11191095
}
1120-
1121-
/**
1122-
* @dataProvider invalidDateProvider
1123-
*/
1124-
public function testInvalidDate($value, $format)
1125-
{
1126-
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_DATE);
1127-
Assertion::date($value, $format);
1128-
}
1129-
1130-
public function invalidDateProvider()
1131-
{
1132-
return array(
1133-
array('this is not the date', 'Y-m-d'),
1134-
array('2011-02-29', 'Y-m-d'),
1135-
array('2012.02.29 12:60:36.432563', 'Y.m.d H:i:s.u')
1136-
);
1137-
}
11381096
}
11391097

11401098
class ChildStdClass extends \stdClass

0 commit comments

Comments
 (0)