1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* It's free open-source software released under the MIT License. |
5
|
|
|
* |
6
|
|
|
* @author Anatoly Nekhay <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2021, Anatoly Nekhay |
8
|
|
|
* @license https://github.com/sunrise-php/hydrator/blob/master/LICENSE |
9
|
|
|
* @link https://github.com/sunrise-php/hydrator |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Sunrise\Hydrator\Exception; |
15
|
|
|
|
16
|
|
|
use LogicException; |
17
|
|
|
use ReflectionFunctionAbstract; |
18
|
|
|
use ReflectionMethod; |
19
|
|
|
use ReflectionParameter; |
20
|
|
|
use ReflectionProperty; |
21
|
|
|
use Sunrise\Hydrator\Type; |
22
|
|
|
|
23
|
|
|
use function sprintf; |
24
|
|
|
|
25
|
|
|
class InvalidObjectException extends LogicException implements ExceptionInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @since 3.2.0 |
29
|
|
|
*/ |
30
|
2 |
|
final public static function uninstantiableObject(string $className): self |
31
|
|
|
{ |
32
|
2 |
|
return new self(sprintf( |
33
|
2 |
|
'The uninstantiable class %s cannot be hydrated.', |
34
|
2 |
|
$className, |
35
|
2 |
|
)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @since 3.2.0 |
40
|
|
|
*/ |
41
|
3 |
|
final public static function unsupportedType(Type $type): self |
42
|
|
|
{ |
43
|
|
|
/** @var mixed $holder */ |
44
|
3 |
|
$holder = $type->getHolder(); |
45
|
|
|
|
46
|
3 |
|
if ($holder instanceof ReflectionProperty) { |
47
|
3 |
|
return self::unsupportedPropertyType($type, $holder); |
48
|
|
|
} |
49
|
|
|
if ($holder instanceof ReflectionParameter) { |
50
|
|
|
return self::unsupportedParameterType($type, $holder); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return new self(sprintf( |
54
|
|
|
'The type {%s} is not supported.', |
55
|
|
|
$type->getName(), |
56
|
|
|
)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @since 3.2.0 |
61
|
|
|
*/ |
62
|
3 |
|
final public static function unsupportedPropertyType(Type $type, ReflectionProperty $property): self |
63
|
|
|
{ |
64
|
3 |
|
return new self(sprintf( |
65
|
3 |
|
'The property {%s::$%s} is associated with an unsupported type {%s}.', |
66
|
3 |
|
$property->getDeclaringClass()->getName(), |
67
|
3 |
|
$property->getName(), |
68
|
3 |
|
$type->getName(), |
69
|
3 |
|
)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @since 3.2.0 |
74
|
|
|
*/ |
75
|
2 |
|
final public static function unsupportedParameterType(Type $type, ReflectionParameter $parameter): self |
76
|
|
|
{ |
77
|
2 |
|
$holder = $parameter->getDeclaringFunction(); |
78
|
|
|
|
79
|
2 |
|
return $holder instanceof ReflectionMethod ? |
80
|
1 |
|
self::unsupportedMethodParameterType($type, $parameter, $holder) : |
81
|
2 |
|
self::unsupportedFunctionParameterType($type, $parameter, $holder); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @since 3.2.0 |
86
|
|
|
*/ |
87
|
1 |
|
final public static function unsupportedMethodParameterType( |
88
|
|
|
Type $type, |
89
|
|
|
ReflectionParameter $parameter, |
90
|
|
|
ReflectionMethod $method |
91
|
|
|
): self { |
92
|
1 |
|
return new self(sprintf( |
93
|
1 |
|
'The parameter {%s::%s($%s[%d])} is associated with an unsupported type {%s}.', |
94
|
1 |
|
$method->getDeclaringClass()->getName(), |
95
|
1 |
|
$method->getName(), |
96
|
1 |
|
$parameter->getName(), |
97
|
1 |
|
$parameter->getPosition(), |
98
|
1 |
|
$type->getName(), |
99
|
1 |
|
)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @since 3.2.0 |
104
|
|
|
*/ |
105
|
1 |
|
final public static function unsupportedFunctionParameterType( |
106
|
|
|
Type $type, |
107
|
|
|
ReflectionParameter $parameter, |
108
|
|
|
ReflectionFunctionAbstract $function |
109
|
|
|
): self { |
110
|
1 |
|
return new self(sprintf( |
111
|
1 |
|
'The parameter {%s($%s[%d])} is associated with an unsupported type {%s}.', |
112
|
1 |
|
$function->getName(), |
113
|
1 |
|
$parameter->getName(), |
114
|
1 |
|
$parameter->getPosition(), |
115
|
1 |
|
$type->getName(), |
116
|
1 |
|
)); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|