1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Validation\Errors; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2019 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Limoncello\Validation\Contracts\Errors\ErrorInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @package Limoncello\Validation |
25
|
|
|
*/ |
26
|
|
|
class Error implements ErrorInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var string|null |
30
|
|
|
*/ |
31
|
|
|
private $name; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var mixed |
35
|
|
|
*/ |
36
|
|
|
private $value; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
private $code; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $messageTemplate; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var array|null |
50
|
|
|
*/ |
51
|
|
|
private $messageParams; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param null|string $name |
55
|
|
|
* @param mixed $value |
56
|
|
|
* @param int $code |
57
|
|
|
* @param string $messageTemplate |
58
|
|
|
* @param array $messageParams |
59
|
|
|
*/ |
60
|
17 |
|
public function __construct(?string $name, $value, int $code, string $messageTemplate, array $messageParams) |
61
|
|
|
{ |
62
|
17 |
|
assert($this->checkEachValueConvertibleToString($messageParams)); |
|
|
|
|
63
|
|
|
|
64
|
17 |
|
$this->name = $name; |
65
|
17 |
|
$this->value = $value; |
66
|
17 |
|
$this->code = $code; |
67
|
17 |
|
$this->messageTemplate = $messageTemplate; |
68
|
17 |
|
$this->messageParams = $messageParams; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @inheritdoc |
73
|
|
|
*/ |
74
|
12 |
|
public function getParameterName(): ?string |
75
|
|
|
{ |
76
|
12 |
|
return $this->name; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritdoc |
81
|
|
|
*/ |
82
|
4 |
|
public function getParameterValue() |
83
|
|
|
{ |
84
|
4 |
|
return $this->value; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritdoc |
89
|
|
|
*/ |
90
|
8 |
|
public function getMessageCode(): int |
91
|
|
|
{ |
92
|
8 |
|
return $this->code; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritdoc |
97
|
|
|
*/ |
98
|
1 |
|
public function getMessageTemplate(): string |
99
|
|
|
{ |
100
|
1 |
|
return $this->messageTemplate; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @inheritdoc |
105
|
|
|
*/ |
106
|
4 |
|
public function getMessageParameters(): array |
107
|
|
|
{ |
108
|
4 |
|
return $this->messageParams; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param iterable $messageParams |
113
|
|
|
* |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
17 |
|
protected function checkEachValueConvertibleToString(iterable $messageParams): bool |
117
|
|
|
{ |
118
|
17 |
|
$result = true; |
119
|
|
|
|
120
|
17 |
|
foreach ($messageParams as $param) { |
121
|
11 |
|
$result = $result && ( |
122
|
11 |
|
is_scalar($param) === true || |
123
|
11 |
|
$param === null || |
124
|
11 |
|
(is_object($param) === true && method_exists($param, '__toString')) |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
17 |
|
return true; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: