-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBools.php
More file actions
34 lines (27 loc) · 711 Bytes
/
Bools.php
File metadata and controls
34 lines (27 loc) · 711 Bytes
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
<?php declare(strict_types=1);
namespace h4kuna\DataType\Basic;
use h4kuna\DataType\Exceptions\InvalidTypeException;
use Nette\StaticClass;
final class Bools
{
use StaticClass;
public static function nullable(mixed $value): ?bool
{
return $value === null ? null : self::from($value);
}
public static function from(mixed $value): bool
{
if ($value === true) {
return true;
} elseif ($value === '' || $value === null || $value === false) {
return false;
} elseif (is_numeric($value)) {
return match ((float) $value) {
1.0 => true,
0.0 => false,
default => throw InvalidTypeException::invalidBool($value),
};
}
throw InvalidTypeException::invalidBool($value);
}
}