-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNotConvertable.php
More file actions
62 lines (48 loc) · 1.75 KB
/
NotConvertable.php
File metadata and controls
62 lines (48 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
declare(strict_types=1);
namespace Plook\TypeGuard;
use InvalidArgumentException;
use function basename;
use function get_debug_type;
use function sprintf;
use function str_contains;
final class NotConvertable extends InvalidArgumentException
{
public static function toBool(mixed $value): self
{
return self::withMessage(sprintf('%s is not convertable to bool', get_debug_type($value)));
}
public static function toFloat(mixed $value): self
{
return self::withMessage(sprintf('%s is not convertable to float', get_debug_type($value)));
}
public static function toInteger(mixed $value): self
{
return self::withMessage(sprintf('%s is not convertable to integer', get_debug_type($value)));
}
public static function toString(mixed $value): self
{
return self::withMessage(sprintf('%s is not convertable to string', get_debug_type($value)));
}
public static function toDateTime(mixed $value): self
{
return self::withMessage(sprintf('%s is not convertable to date time object', get_debug_type($value)));
}
public static function toDateTimeZone(mixed $value): self
{
return self::withMessage(sprintf('%s is not convertable to date time zone', get_debug_type($value)));
}
private static function withMessage(string $message): self
{
$me = new self($message);
foreach ($me->getTrace() as $trace) {
$file = $trace['file'] ?? null;
$line = $trace['line'] ?? null;
if ($file !== null && $line !== null && !str_contains($file, __DIR__)) {
$me->message = sprintf('%s in %s:%s', $message, basename($file), $line);
break;
}
}
return $me;
}
}