Skip to content

Commit 1ccd655

Browse files
committed
Add Text alias for StringText with usage example
- Introduced `StringText` class for MariaDB TEXT strings, enforcing a maximum length of 65,535 characters. - Added `fromString` and `tryFromString` methods, returning `Undefined` for inputs exceeding the length limit. - Created alias `Text` as a readonly extension of `StringText`. - Updated `Usage` examples to demonstrate `StringText` and `Text` behavior. - Included detailed unit tests to validate boundaries, exception handling, alias behavior, and `Undefined` returns for invalid cases.
1 parent 1e07b1c commit 1ccd655

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

src/String/Alias/Text.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpTypedValues\String\Alias;
6+
7+
use PhpTypedValues\String\MariaDb\StringText;
8+
9+
/**
10+
* @psalm-immutable
11+
*/
12+
readonly class Text extends StringText
13+
{
14+
}

src/String/MariaDb/StringText.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpTypedValues\String\MariaDb;
6+
7+
use PhpTypedValues\Abstract\String\StrType;
8+
use PhpTypedValues\Exception\StringTypeException;
9+
use PhpTypedValues\Exception\TypeException;
10+
use PhpTypedValues\Undefined\Alias\Undefined;
11+
12+
use function mb_strlen;
13+
14+
/**
15+
* Database TEXT string.
16+
*
17+
* MySQL/MariaDB TEXT: up to 65,535 characters.
18+
*
19+
* Example "Lorem ipsum ..." (any string, including empty, length <= 65,535)
20+
*
21+
* @psalm-immutable
22+
*/
23+
readonly class StringText extends StrType
24+
{
25+
protected string $value;
26+
27+
/**
28+
* @throws StringTypeException
29+
*/
30+
public function __construct(string $value)
31+
{
32+
if (mb_strlen($value) > 65535) {
33+
throw new StringTypeException('String is too long, max 65535 chars allowed');
34+
}
35+
36+
$this->value = $value;
37+
}
38+
39+
public static function tryFromString(string $value): static|Undefined
40+
{
41+
try {
42+
return static::fromString($value);
43+
} catch (TypeException) {
44+
return Undefined::create();
45+
}
46+
}
47+
48+
/**
49+
* @throws StringTypeException
50+
*/
51+
public static function fromString(string $value): static
52+
{
53+
return new static($value);
54+
}
55+
56+
public function value(): string
57+
{
58+
return $this->value;
59+
}
60+
}

src/Usage/String.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
use PhpTypedValues\String\Alias\NonEmptyStr;
99
use PhpTypedValues\String\Alias\Str;
1010
use PhpTypedValues\String\Alias\StrType;
11+
use PhpTypedValues\String\Alias\Text;
1112
use PhpTypedValues\String\Alias\Url;
1213
use PhpTypedValues\String\Json;
1314
use PhpTypedValues\String\MariaDb\StringDecimal;
15+
use PhpTypedValues\String\MariaDb\StringText;
1416
use PhpTypedValues\String\MariaDb\StringVarChar255;
1517
use PhpTypedValues\String\StringCountryCode;
1618
use PhpTypedValues\String\StringEmail;
@@ -45,6 +47,14 @@
4547
echo StringUuidV7::tryFromString('01890f2a-5bcd-7def-8abc-1234567890ab')->toString() . \PHP_EOL;
4648
echo StringVarChar255::tryFromString('hi')->toString() . \PHP_EOL;
4749

50+
// MariaDb TEXT
51+
echo Text::fromString('lorem ipsum')->toString() . \PHP_EOL;
52+
echo StringText::fromString('lorem ipsum')->toString() . \PHP_EOL;
53+
$text = StringText::tryFromString(str_repeat('a', 10));
54+
if (!($text instanceof Undefined)) {
55+
echo $text->toString() . \PHP_EOL;
56+
}
57+
4858
// JSON
4959
echo json_encode(JsonStr::fromString('{"a": 1, "b": "hi"}')->toArray(), \JSON_THROW_ON_ERROR) . \PHP_EOL;
5060
echo json_encode(Json::fromString('{"a": 1}')->toObject(), \JSON_THROW_ON_ERROR) . \PHP_EOL;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpTypedValues\Exception\StringTypeException;
6+
use PhpTypedValues\String\Alias\Text;
7+
use PhpTypedValues\String\MariaDb\StringText;
8+
use PhpTypedValues\Undefined\Alias\Undefined;
9+
10+
it('accepts empty string and preserves value/toString', function (): void {
11+
$s = new StringText('');
12+
expect($s->value())->toBe('')
13+
->and($s->toString())->toBe('')
14+
->and((string) $s)->toBe('');
15+
});
16+
17+
it('accepts 65535 ASCII characters (boundary) and preserves value', function (): void {
18+
$str = str_repeat('x', 65535);
19+
$s = StringText::fromString($str);
20+
expect($s->value())->toBe($str)
21+
->and($s->toString())->toBe($str);
22+
});
23+
24+
it('throws on 65536 ASCII characters (above boundary)', function (): void {
25+
$str = str_repeat('y', 65536);
26+
expect(fn() => new StringText($str))
27+
->toThrow(StringTypeException::class, 'String is too long, max 65535 chars allowed');
28+
});
29+
30+
it('accepts 65535 multibyte characters (emoji) counted by mb_strlen', function (): void {
31+
$str = str_repeat('🙂', 65535);
32+
$s = new StringText($str);
33+
expect($s->value())->toBe($str)
34+
->and($s->toString())->toBe($str);
35+
});
36+
37+
it('throws on 65536 multibyte characters (emoji)', function (): void {
38+
$str = str_repeat('🙂', 65536);
39+
expect(fn() => StringText::fromString($str))
40+
->toThrow(StringTypeException::class, 'String is too long, max 65535 chars allowed');
41+
});
42+
43+
it('StringText::tryFromString returns value when length <= 65535 and Undefined when > 65535', function (): void {
44+
$ok = StringText::tryFromString(str_repeat('a', 10));
45+
$tooLong = StringText::tryFromString(str_repeat('b', 65536));
46+
47+
expect($ok)
48+
->toBeInstanceOf(StringText::class)
49+
->and($ok->value())
50+
->toBe(str_repeat('a', 10))
51+
->and($tooLong)
52+
->toBeInstanceOf(Undefined::class);
53+
});
54+
55+
it('Alias Text behaves the same as StringText', function (): void {
56+
$alias = Text::fromString('alias');
57+
expect($alias->value())->toBe('alias');
58+
});

0 commit comments

Comments
 (0)