Skip to content

Commit d06f871

Browse files
committed
Merge pull request jsonrainbow#99 from xanagi/multibyte_strlen
Use mb_strlen instead of strlen when mbstring extension is available.
2 parents f6c768d + 32105d0 commit d06f871

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

src/JsonSchema/Constraints/String.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ class String extends Constraint
2323
public function check($element, $schema = null, $path = null, $i = null)
2424
{
2525
// Verify maxLength
26-
if (isset($schema->maxLength) && strlen($element) > $schema->maxLength) {
26+
if (isset($schema->maxLength) && $this->strlen($element) > $schema->maxLength) {
2727
$this->addError($path, "must be at most " . $schema->maxLength . " characters long");
2828
}
2929

3030
//verify minLength
31-
if (isset($schema->minLength) && strlen($element) < $schema->minLength) {
31+
if (isset($schema->minLength) && $this->strlen($element) < $schema->minLength) {
3232
$this->addError($path, "must be at least " . $schema->minLength . " characters long");
3333
}
3434

@@ -39,4 +39,13 @@ public function check($element, $schema = null, $path = null, $i = null)
3939

4040
$this->checkFormat($element, $schema, $path, $i);
4141
}
42+
43+
private function strlen($string)
44+
{
45+
if (extension_loaded('mbstring')) {
46+
return mb_strlen($string, mb_detect_encoding($string));
47+
} else {
48+
return strlen($string);
49+
}
50+
}
4251
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the JsonSchema package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace JsonSchema\Tests\Constraints;
11+
12+
class MinLengthMaxLengthMultiByteTest extends BaseTestCase
13+
{
14+
protected function setUp()
15+
{
16+
if (! extension_loaded('mbstring')) {
17+
$this->markTestSkipped('mbstring extension is not available');
18+
}
19+
}
20+
21+
public function getInvalidTests()
22+
{
23+
return array(
24+
array(
25+
'{
26+
"value":"☀"
27+
}',
28+
'{
29+
"type":"object",
30+
"properties":{
31+
"value":{"type":"string","minLength":2,"maxLength":4}
32+
}
33+
}'
34+
),
35+
array(
36+
'{
37+
"value":"☀☁☂☃☺"
38+
}',
39+
'{
40+
"type":"object",
41+
"properties":{
42+
"value":{"type":"string","minLength":2,"maxLength":4}
43+
}
44+
}'
45+
)
46+
);
47+
}
48+
49+
public function getValidTests()
50+
{
51+
return array(
52+
array(
53+
'{
54+
"value":"☀☁"
55+
}',
56+
'{
57+
"type":"object",
58+
"properties":{
59+
"value":{"type":"string","minLength":2,"maxLength":4}
60+
}
61+
}'
62+
),
63+
array(
64+
'{
65+
"value":"☀☁☂☃"
66+
}',
67+
'{
68+
"type":"object",
69+
"properties":{
70+
"value":{"type":"string","minLength":2,"maxLength":4}
71+
}
72+
}'
73+
)
74+
);
75+
}
76+
}

0 commit comments

Comments
 (0)