|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spatie\DataTransferObject; |
|
6
|
|
|
|
|
7
|
|
|
use TypeError; |
|
8
|
|
|
|
|
9
|
|
|
class DataTransferObjectError extends TypeError |
|
10
|
|
|
{ |
|
11
|
|
|
use HasErrorProperties; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct($message, $properties = []) |
|
14
|
|
|
{ |
|
15
|
|
|
parent::__construct($message); |
|
|
|
|
|
|
16
|
|
|
$this->setProperties($properties); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public static function unknownProperties(array $properties, string $className): DataTransferObjectError |
|
20
|
|
|
{ |
|
21
|
|
|
$propertyNames = implode('`, `', $properties); |
|
22
|
|
|
|
|
23
|
|
|
return new self("Public properties `{$propertyNames}` not found on {$className}", [ |
|
24
|
|
|
'error' => __FUNCTION__, |
|
25
|
|
|
'class' => $className, |
|
26
|
|
|
'properties' => $properties, |
|
27
|
|
|
]); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public static function invalidType( |
|
31
|
|
|
string $class, |
|
32
|
|
|
string $field, |
|
33
|
|
|
array $expectedTypes, |
|
34
|
|
|
$value |
|
35
|
|
|
): DataTransferObjectError { |
|
36
|
|
|
$currentType = gettype($value); |
|
37
|
|
|
|
|
38
|
|
|
if ($value === null) { |
|
39
|
|
|
$value = 'null'; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if (is_object($value)) { |
|
43
|
|
|
$value = get_class($value); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if (is_array($value)) { |
|
47
|
|
|
$value = 'array'; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$expectedTypesList = implode(', ', $expectedTypes); |
|
51
|
|
|
|
|
52
|
|
|
return new self("Invalid type: expected `{$class}::{$field}` to be of type `{$expectedTypesList}`, instead got value `{$value}`, which is {$currentType}.", [ |
|
53
|
|
|
'error' => __FUNCTION__, |
|
54
|
|
|
'class' => $class, |
|
55
|
|
|
'property' => $field, |
|
56
|
|
|
'value' => $value, |
|
57
|
|
|
'type' => $currentType, |
|
58
|
|
|
'expectedTypes' => $expectedTypes, |
|
59
|
|
|
]); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public static function uninitialized(string $class, string $field): DataTransferObjectError |
|
63
|
|
|
{ |
|
64
|
|
|
return new self("Non-nullable property `{$class}::{$field}` has not been initialized.", [ |
|
65
|
|
|
'error' => __FUNCTION__, |
|
66
|
|
|
'class' => $class, |
|
67
|
|
|
'property' => $field, |
|
68
|
|
|
]); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public static function immutable(string $property): DataTransferObjectError |
|
72
|
|
|
{ |
|
73
|
|
|
return new self("Cannot change the value of property {$property} on an immutable data transfer object", [ |
|
74
|
|
|
'error' => __FUNCTION__, |
|
75
|
|
|
'property' => $property, |
|
76
|
|
|
]); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: