-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInvalidDataException.php
112 lines (91 loc) · 2.64 KB
/
InvalidDataException.php
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
declare(strict_types=1);
namespace romanzipp\DTO\Exceptions;
use romanzipp\DTO\Property;
class InvalidDataException extends \InvalidArgumentException
{
/**
* @var \romanzipp\DTO\Property[]
*/
private array $properties = [];
/**
* @param Property $property
* @param mixed $value
*
* @return self
*/
public static function invalidType(Property $property, $value): self
{
$type = gettype($value);
if (is_object($value)) {
$type = get_class($value);
}
$exception = new self("The type `{$type}` is not allowed for property `{$property->getName()}`");
$exception->setProperties([$property]);
return $exception;
}
public static function requiredPropertyMissing(Property $property): self
{
$exception = new self("The required property `{$property->getName()}` is missing");
$exception->setProperties([$property]);
return $exception;
}
public static function nullNotAllowed(Property $property): self
{
$exception = new self("`NULL` is not allowed for property `{$property->getName()}`");
$exception->setProperties([$property]);
return $exception;
}
/**
* @param string[] $keys
*
* @return self
*/
public static function notFlexible(array $keys): self
{
if (count($keys) > 0) {
return new self(
sprintf('The provided values `%s` are not declared as properties', implode('`, `', $keys))
);
}
return new self('Some provided values are not declared as properties');
}
/**
* @param \romanzipp\DTO\Exceptions\InvalidDataException[] $exceptions
*
* @return self
*/
public static function any(array $exceptions): self
{
if (1 === count($exceptions)) {
return array_shift($exceptions);
}
$messages = [];
$properties = [];
foreach ($exceptions as $exception) {
$messages[] = $exception->getMessage();
$properties = array_merge($properties, $exception->getProperties());
}
$exception = new self(
implode(PHP_EOL, $messages)
);
$exception->setProperties($properties);
return $exception;
}
/**
* @param \romanzipp\DTO\Property[] $properties
*
* @return void
*/
public function setProperties(array $properties): void
{
$this->properties = $properties;
}
/**
* @return \romanzipp\DTO\Property[]
*/
public function getProperties(): array
{
return $this->properties;
}
}