Skip to content

Commit ab45ae1

Browse files
authored
Added Japanese support for getting string length. (#1)
* fix: Added Japanese support for getting str length * add: text test * fix: code style * add: require mbstring * fix: use utf-8
1 parent 9eafa8a commit ab45ae1

4 files changed

Lines changed: 55 additions & 4 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
},
99
"require": {
1010
"php": ">=7.3",
11-
"ext-json": "*"
11+
"ext-json": "*",
12+
"ext-mbstring": "*"
1213
},
1314
"require-dev": {
1415
"phpstan/phpstan": "^0.12.0",

src/Inputs/HasPlaceholder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function setPlaceholder(PlainText $placeholder): self
2929
*/
3030
public function placeholder(string $placeholder): self
3131
{
32-
if (strlen($placeholder) > 150) {
32+
if (mb_strlen($placeholder, 'UTF-8') > 150) {
3333
throw new Exception('Placeholder cannot exceed 150 characters');
3434
}
3535

src/Partials/Text.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ public static function validateString(?string $text, ?int $max = null, int $min
5151
throw new Exception('Text element must have a "text" value');
5252
}
5353

54-
if (strlen($text) < $min) {
54+
if (mb_strlen($text, 'UTF-8') < $min) {
5555
throw new Exception('Text element must have a "text" value with a length of at least %d', [$min]);
5656
}
5757

58-
if (is_int($max) && strlen($text) > $max) {
58+
if (is_int($max) && mb_strlen($text, 'UTF-8') > $max) {
5959
throw new Exception('Text element must have a "text" value with a length of at most %d', [$max]);
6060
}
6161
}

tests/Partials/TextTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SlackPhp\BlockKit\Tests\Partials;
6+
7+
use SlackPhp\BlockKit\Exception;
8+
use SlackPhp\BlockKit\Partials\Text;
9+
use SlackPhp\BlockKit\Tests\TestCase;
10+
11+
/**
12+
* @covers \SlackPhp\BlockKit\Partials\Text
13+
*/
14+
class TextTest extends TestCase
15+
{
16+
private const MAX_LENGTH = 5;
17+
private const MIN_LENGTH = 2;
18+
19+
public function testCanValidateEnglishText()
20+
{
21+
$text = new class () extends Text {
22+
};
23+
24+
$text->validateString('abc', self::MAX_LENGTH);
25+
26+
$this->expectException(Exception::class);
27+
28+
$this->expectExceptionMessage('Text element must have a "text" value with a length of at most 5');
29+
$text->validateString('abcdefg', self::MAX_LENGTH);
30+
31+
$this->expectExceptionMessage('Text element must have a "text" value with a length of at least 2');
32+
$text->validateString('a', self::MAX_LENGTH, self::MIN_LENGTH);
33+
}
34+
35+
public function testCanValidateJapaneseText()
36+
{
37+
$text = new class () extends Text {
38+
};
39+
40+
$text->validateString('いろは', self::MAX_LENGTH);
41+
42+
$this->expectException(Exception::class);
43+
44+
$this->expectExceptionMessage('Text element must have a "text" value with a length of at most 5');
45+
$text->validateString('いろはにほへと', self::MAX_LENGTH);
46+
47+
$this->expectExceptionMessage('Text element must have a "text" value with a length of at least 2');
48+
$text->validateString('', self::MAX_LENGTH, self::MIN_LENGTH);
49+
}
50+
}

0 commit comments

Comments
 (0)