1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license https://github.com/f500/equatable/blob/master/LICENSE MIT |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace F500\Equatable\Exceptions; |
10
|
|
|
|
11
|
|
|
use InvalidArgumentException as BaseException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @copyright Copyright (c) 2015 Future500 B.V. |
15
|
|
|
* @author Jasper N. Brouwer <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
final class InvalidArgumentException extends BaseException |
18
|
|
|
{ |
19
|
9 |
|
public static function invalidType(string $argument, string $expectedType, $actualValue): self |
20
|
|
|
{ |
21
|
9 |
|
return self::create( |
22
|
9 |
|
'Argument $%s must be of type %s, %s given', |
23
|
9 |
|
$argument, |
24
|
9 |
|
$expectedType, |
25
|
9 |
|
$actualValue |
26
|
|
|
); |
27
|
|
|
} |
28
|
|
|
|
29
|
15 |
|
public static function invalidValueInArray(string $argument, string $expectedType, $actualValue): self |
30
|
|
|
{ |
31
|
15 |
|
return self::create( |
32
|
15 |
|
'Each value in argument $%s must be of type %s, %s given', |
33
|
15 |
|
$argument, |
34
|
15 |
|
$expectedType, |
35
|
15 |
|
$actualValue |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
12 |
|
public static function invalidKeyInArray(string $argument, string $expectedType, $actualValue): self |
40
|
|
|
{ |
41
|
12 |
|
return self::create( |
42
|
12 |
|
'Each key in argument $%s must be of type %s, %s given', |
43
|
12 |
|
$argument, |
44
|
12 |
|
$expectedType, |
45
|
12 |
|
$actualValue |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
36 |
|
private static function create(string $message, string $argument, string $expectedType, $actualValue): self |
50
|
|
|
{ |
51
|
36 |
|
if (is_object($actualValue)) { |
52
|
9 |
|
$actualType = get_class($actualValue); |
53
|
27 |
|
} elseif (is_resource($actualValue)) { |
54
|
9 |
|
$actualType = get_resource_type($actualValue); |
55
|
|
|
} else { |
56
|
18 |
|
$actualType = gettype($actualValue); |
57
|
|
|
} |
58
|
|
|
|
59
|
36 |
|
return new self(sprintf($message, $argument, $expectedType, $actualType)); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|