-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathTypeCheckCallback.php
More file actions
74 lines (63 loc) 路 1.52 KB
/
Copy pathTypeCheckCallback.php
File metadata and controls
74 lines (63 loc) 路 1.52 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
63
64
65
66
67
68
69
70
71
72
73
74
<?php
declare(strict_types=1);
namespace Arrayy\TypeCheck;
class TypeCheckCallback implements TypeCheckInterface
{
/**
* @var callable
*
* @phpstan-var callable(mixed): bool
*/
protected $callable;
/**
* @var bool
*/
protected $isNullable = false;
/**
* @param callable $callable
* @param bool $isNullable
*
* @phpstan-param callable(mixed): bool $callable
*/
public function __construct(callable $callable, bool $isNullable = false)
{
$this->callable = $callable;
$this->isNullable = $isNullable;
}
/**
* @param mixed $value
*
* @return bool
*/
public function checkType(&$value): bool
{
if ($this->isNullable && $value === null) {
return true;
}
if (\call_user_func($this->callable, $value)) {
return true;
}
$this->throwException('', $value, \gettype($value));
return false;
}
/**
* @return array
*
* @phpstan-return list<never>
*/
public function getTypes(): array
{
return [];
}
/**
* @param string $expectedTypes
* @param mixed $value
* @param string $type
*
* @return \TypeError
*/
public function throwException($expectedTypes, $value, $type): \Throwable
{
throw new \TypeError('Invalid type: callable failed, got value `' . \print_r($value, true) . "` with type {{$type}}.");
}
}