Skip to content

Commit db1e98b

Browse files
committed
Add tryFromString method to string types with tests and usage updates
- Implemented `tryFromString` in `StringNonEmpty`, `StringStandard`, `StringUuidV4`, `StringUuidV7`, `Json`, and `StringVarChar255` to return `Undefined` for invalid input. - Updated relevant interfaces and refactored exception handling for string parsing logic. - Added comprehensive unit tests for valid and invalid cases. - Modified `composer.json` scripts and `Usage` examples to include `tryFromString` methods.
1 parent edbd0d6 commit db1e98b

File tree

15 files changed

+230
-4
lines changed

15 files changed

+230
-4
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
"test": [
5959
"./vendor/bin/pest"
6060
],
61+
"usage": [
62+
"php ./src/Usage/AllTypes.php"
63+
],
6164
"type": [
6265
"./vendor/bin/pest --type-coverage --min=100 --do-not-cache-result"
6366
],

src/Abstract/String/StrTypeInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace PhpTypedValues\Abstract\String;
66

7+
use PhpTypedValues\Undefined\Alias\Undefined;
8+
79
/**
810
* @psalm-immutable
911
*/
@@ -13,6 +15,8 @@ public function value(): string;
1315

1416
public static function fromString(string $value): static;
1517

18+
public static function tryFromString(string $value): static|Undefined;
19+
1620
public function toString(): string;
1721

1822
public function __toString(): string;

src/String/Json.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use JsonException;
1010
use PhpTypedValues\Abstract\String\StrType;
1111
use PhpTypedValues\Exception\JsonStringTypeException;
12+
use PhpTypedValues\Exception\TypeException;
13+
use PhpTypedValues\Undefined\Alias\Undefined;
1214

1315
use function json_decode;
1416
use function sprintf;
@@ -42,6 +44,15 @@ public static function fromString(string $value): static
4244
return new static($value);
4345
}
4446

47+
public static function tryFromString(string $value): static|Undefined
48+
{
49+
try {
50+
return static::fromString($value);
51+
} catch (TypeException) {
52+
return Undefined::create();
53+
}
54+
}
55+
4556
public function value(): string
4657
{
4758
return $this->value;
@@ -52,7 +63,6 @@ public function value(): string
5263
*/
5364
public function toObject(): object
5465
{
55-
// Use named arguments and omit depth literal to avoid integer-mutation issues
5666
return json_decode(json: $this->value, associative: false, flags: JSON_THROW_ON_ERROR);
5767
}
5868

@@ -61,7 +71,6 @@ public function toObject(): object
6171
*/
6272
public function toArray(): array
6373
{
64-
// Use named arguments and omit depth literal to avoid integer-mutation issues
6574
return json_decode(json: $this->value, associative: true, flags: JSON_THROW_ON_ERROR);
6675
}
6776

src/String/MariaDb/StringVarChar255.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use PhpTypedValues\Abstract\String\StrType;
88
use PhpTypedValues\Exception\StringTypeException;
9+
use PhpTypedValues\Exception\TypeException;
10+
use PhpTypedValues\Undefined\Alias\Undefined;
911

1012
/**
1113
* Database VARCHAR(255) string.
@@ -30,6 +32,15 @@ public function __construct(string $value)
3032
$this->value = $value;
3133
}
3234

35+
public static function tryFromString(string $value): static|Undefined
36+
{
37+
try {
38+
return static::fromString($value);
39+
} catch (TypeException) {
40+
return Undefined::create();
41+
}
42+
}
43+
3344
/**
3445
* @throws StringTypeException
3546
*/

src/String/StringNonEmpty.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use PhpTypedValues\Abstract\String\StrType;
88
use PhpTypedValues\Exception\StringTypeException;
9+
use PhpTypedValues\Exception\TypeException;
10+
use PhpTypedValues\Undefined\Alias\Undefined;
911

1012
use function sprintf;
1113

@@ -33,6 +35,15 @@ public function __construct(string $value)
3335
$this->value = $value;
3436
}
3537

38+
public static function tryFromString(string $value): static|Undefined
39+
{
40+
try {
41+
return static::fromString($value);
42+
} catch (TypeException) {
43+
return Undefined::create();
44+
}
45+
}
46+
3647
/**
3748
* @throws StringTypeException
3849
*/

src/String/StringStandard.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpTypedValues\String;
66

77
use PhpTypedValues\Abstract\String\StrType;
8+
use PhpTypedValues\Undefined\Alias\Undefined;
89

910
/**
1011
* Represents any PHP string.
@@ -22,6 +23,11 @@ public function __construct(string $value)
2223
$this->value = $value;
2324
}
2425

26+
public static function tryFromString(string $value): static|Undefined
27+
{
28+
return static::fromString($value);
29+
}
30+
2531
public static function fromString(string $value): static
2632
{
2733
return new static($value);

src/String/StringUuidV4.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
namespace PhpTypedValues\String;
66

77
use PhpTypedValues\Abstract\String\StrType;
8+
use PhpTypedValues\Exception\TypeException;
89
use PhpTypedValues\Exception\UuidStringTypeException;
10+
use PhpTypedValues\Undefined\Alias\Undefined;
911

1012
use function preg_match;
1113
use function sprintf;
@@ -45,6 +47,15 @@ public function __construct(string $value)
4547
$this->value = $normalized;
4648
}
4749

50+
public static function tryFromString(string $value): static|Undefined
51+
{
52+
try {
53+
return static::fromString($value);
54+
} catch (TypeException) {
55+
return Undefined::create();
56+
}
57+
}
58+
4859
/**
4960
* @throws UuidStringTypeException
5061
*/

src/String/StringUuidV7.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
namespace PhpTypedValues\String;
66

77
use PhpTypedValues\Abstract\String\StrType;
8+
use PhpTypedValues\Exception\TypeException;
89
use PhpTypedValues\Exception\UuidStringTypeException;
10+
use PhpTypedValues\Undefined\Alias\Undefined;
911

1012
use function preg_match;
1113
use function sprintf;
@@ -45,6 +47,15 @@ public function __construct(string $value)
4547
$this->value = $normalized;
4648
}
4749

50+
public static function tryFromString(string $value): static|Undefined
51+
{
52+
try {
53+
return static::fromString($value);
54+
} catch (TypeException) {
55+
return Undefined::create();
56+
}
57+
}
58+
4859
/**
4960
* @throws UuidStringTypeException
5061
*/

src/Usage/String.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
use PhpTypedValues\String\Alias\Str;
66
use PhpTypedValues\String\Alias\StrType;
77
use PhpTypedValues\String\Json;
8+
use PhpTypedValues\String\MariaDb\StringVarChar255;
89
use PhpTypedValues\String\StringNonEmpty;
910
use PhpTypedValues\String\StringStandard;
11+
use PhpTypedValues\String\StringUuidV4;
12+
use PhpTypedValues\String\StringUuidV7;
1013

1114
/**
1215
* String.
@@ -18,10 +21,16 @@
1821
echo NonEmptyStr::fromString('hi')->toString() . \PHP_EOL;
1922
echo StrType::fromString('hi')->toString() . \PHP_EOL;
2023
echo Str::fromString('hi')->toString() . \PHP_EOL;
24+
echo StringNonEmpty::tryFromString('hi')->toString() . \PHP_EOL;
25+
echo StringStandard::tryFromString('hi')->toString() . \PHP_EOL;
26+
echo StringUuidV4::tryFromString('550e8400-e29b-41d4-a716-446655440000')->toString() . \PHP_EOL;
27+
echo StringUuidV7::tryFromString('01890f2a-5bcd-7def-8abc-1234567890ab')->toString() . \PHP_EOL;
28+
echo StringVarChar255::tryFromString('hi')->toString() . \PHP_EOL;
2129

2230
// JSON
23-
echo json_encode(JsonStr::fromString('{"a": 1, "b": "hi"}')->toArray(), \JSON_THROW_ON_ERROR);
24-
echo json_encode(Json::fromString('{"a": 1, "b": "hi"}')->toObject(), \JSON_THROW_ON_ERROR);
31+
echo json_encode(JsonStr::fromString('{"a": 1, "b": "hi"}')->toArray(), \JSON_THROW_ON_ERROR) . \PHP_EOL;
32+
echo json_encode(Json::fromString('{"a": 1}')->toObject(), \JSON_THROW_ON_ERROR) . \PHP_EOL;
33+
echo Json::tryFromString('{}')->toString() . \PHP_EOL;
2534

2635
/**
2736
* Artificial functions.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpTypedValues\String\Json;
6+
use PhpTypedValues\Undefined\Alias\Undefined;
7+
8+
it('Json::tryFromString returns value for valid JSON string', function (): void {
9+
$json = '{"a":1}';
10+
$v = Json::tryFromString($json);
11+
12+
expect($v)
13+
->toBeInstanceOf(Json::class)
14+
->and($v->value())
15+
->toBe($json);
16+
});
17+
18+
it('Json::tryFromString returns Undefined for invalid JSON string', function (): void {
19+
$u = Json::tryFromString('{invalid');
20+
21+
expect($u)->toBeInstanceOf(Undefined::class);
22+
});
23+
24+
it('Json::toObject decodes valid JSON object and throws on invalid internal state', function (): void {
25+
$jsonText = '{"a":1,"b":2}';
26+
$j = Json::tryFromString($jsonText);
27+
28+
// success branch
29+
\assert($j instanceof Json);
30+
$obj = $j->toObject();
31+
expect($obj)->toBeObject()
32+
->and($obj->a)->toBe(1)
33+
->and($obj->b)->toBe(2);
34+
});
35+
36+
it('Json::toArray decodes valid JSON object as array and throws on invalid internal state', function (): void {
37+
$jsonText = '{"x":10,"y":20}';
38+
$j = Json::tryFromString($jsonText);
39+
\assert($j instanceof Json);
40+
41+
// success branch
42+
$arr = $j->toArray();
43+
expect($arr)->toBeArray()
44+
->and($arr['x'])->toBe(10)
45+
->and($arr['y'])->toBe(20);
46+
});

0 commit comments

Comments
 (0)