1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Incoming |
4
|
|
|
* |
5
|
|
|
* @author Trevor Suarez (Rican7) |
6
|
|
|
* @copyright (c) Trevor Suarez |
7
|
|
|
* @link https://github.com/Rican7/incoming |
8
|
|
|
* @license MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Incoming\Structure\Exception; |
14
|
|
|
|
15
|
|
|
use InvalidArgumentException; |
16
|
|
|
use Throwable; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* An exception to be thrown when an invalid type is given to a receiver that |
20
|
|
|
* expects a type that is convertible or usable as structured data. |
21
|
|
|
*/ |
22
|
|
|
class InvalidStructuralTypeException extends InvalidArgumentException |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constants |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The default exception message. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
const DEFAULT_MESSAGE = 'Invalid structural type'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The message extension format for providing type information. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
const MESSAGE_EXTENSION_TYPE_FORMAT = ' `%s`'; |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Properties |
46
|
|
|
*/ |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The exception message. |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $message = self::DEFAULT_MESSAGE; |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Methods |
58
|
|
|
*/ |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Create an exception instance with type information. |
62
|
|
|
* |
63
|
|
|
* The type is automatically inspected based on the passed value. |
64
|
|
|
* |
65
|
|
|
* @param mixed $value The value to inspect type information of. |
66
|
|
|
* @param int $code The exception code. |
67
|
|
|
* @param Throwable|null $previous A previous exception used for chaining. |
68
|
|
|
* @return static The newly created exception. |
69
|
|
|
*/ |
70
|
9 |
|
public static function withTypeInfo($value, int $code = 0, Throwable $previous = null): self |
71
|
|
|
{ |
72
|
9 |
|
$message = self::DEFAULT_MESSAGE; |
73
|
|
|
|
74
|
9 |
|
$message .= sprintf( |
75
|
9 |
|
self::MESSAGE_EXTENSION_TYPE_FORMAT, |
76
|
9 |
|
is_object($value) ? get_class($value) : gettype($value) |
77
|
|
|
); |
78
|
|
|
|
79
|
9 |
|
return new static($message, $code, $previous); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|