1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Sample; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2020 [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 DateTime; |
22
|
|
|
use DateTimeInterface; |
23
|
|
|
use Limoncello\Validation\ArrayValidator as v; |
24
|
|
|
use Limoncello\Validation\Contracts\Errors\ErrorInterface; |
25
|
|
|
use MessageFormatter; |
26
|
|
|
use Sample\Validation\Rules as r; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @package Sample |
30
|
|
|
*/ |
31
|
|
|
class Application |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
private $isOutputToConsole; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param bool $isOutputToConsole |
40
|
|
|
*/ |
41
|
|
|
public function __construct(bool $isOutputToConsole = true) |
42
|
|
|
{ |
43
|
|
|
$this->isOutputToConsole = $isOutputToConsole; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public function run(): void |
50
|
|
|
{ |
51
|
|
|
$validator = v::validator([ |
52
|
|
|
'sku' => r::required(r::sku()), |
53
|
|
|
'amount' => r::required(r::amount(5)), |
54
|
|
|
'delivery_date' => r::nullable(r::deliveryDate()), |
55
|
|
|
'email' => r::email(), |
56
|
|
|
'address1' => r::required(r::address1()), |
57
|
|
|
'address2' => r::address2(), |
58
|
|
|
'accepted' => r::required(r::areTermsAccepted()), |
59
|
|
|
]); |
60
|
|
|
|
61
|
|
|
// Check with invalid data |
62
|
|
|
$invalidInput = [ |
63
|
|
|
'sku' => '123', |
64
|
|
|
'amount' => '10', |
65
|
|
|
'delivery_date' => '2001-01-01', |
66
|
|
|
'email' => 'john.dow', |
67
|
|
|
// 'address1' => 'Dow 1', // missed required parameter |
68
|
|
|
'accepted' => 'false', |
69
|
|
|
]; |
70
|
|
|
$this->console('Invalid data (errors)' . PHP_EOL); |
71
|
|
|
$validator->validate($invalidInput); |
72
|
|
|
$this->printErrors($validator->getErrors()); |
|
|
|
|
73
|
|
|
$this->console('Invalid data (captures)' . PHP_EOL); |
74
|
|
|
$this->printCaptures($validator->getCaptures()); |
|
|
|
|
75
|
|
|
|
76
|
|
|
// Check with valid data |
77
|
|
|
$validInput = [ |
78
|
|
|
'sku' => '1', |
79
|
|
|
'amount' => '3', |
80
|
|
|
'delivery_date' => (new DateTime('+2 days'))->format(DateTime::ISO8601), |
81
|
|
|
'email' => '[email protected]', |
82
|
|
|
'address1' => 'Dow 1', |
83
|
|
|
'address2' => null, |
84
|
|
|
'accepted' => 'true', |
85
|
|
|
]; |
86
|
|
|
$this->console(PHP_EOL . 'Valid data (errors)' . PHP_EOL); |
87
|
|
|
$validator->validate($validInput); |
88
|
|
|
$this->printErrors($validator->getErrors()); |
|
|
|
|
89
|
|
|
$this->console('Valid data (captures)' . PHP_EOL); |
90
|
|
|
$this->printCaptures($validator->getCaptures()); |
|
|
|
|
91
|
|
|
|
92
|
|
|
// The output would be |
93
|
|
|
// ------------------------------------------------------------------------------------------------------- |
94
|
|
|
// Invalid data (errors) |
95
|
|
|
// Param `sku` failed for `123` with: The value should be a valid SKU. |
96
|
|
|
// Param `amount` failed for `10` with: The value should be between 1 and 5. |
97
|
|
|
// Param `delivery_date` failed for `2001-01-01` with: The value should be a valid date time. |
98
|
|
|
// Param `email` failed for `john.dow` with: The value should be a valid email address. |
99
|
|
|
// Param `accepted` failed for `` with: The value should be equal to 1. |
100
|
|
|
// Param `address1` failed for `` with: The value is required. |
101
|
|
|
// Invalid data (captures) |
102
|
|
|
// No captures |
103
|
|
|
|
104
|
|
|
// Valid data (errors) |
105
|
|
|
// No errors |
106
|
|
|
// Valid data (captures) |
107
|
|
|
// `sku` = `1` (integer) |
108
|
|
|
// `amount` = `3` (integer) |
109
|
|
|
// `delivery_date` = `2018-01-04T15:07:33+0100` (object) |
110
|
|
|
// `email` = `[email protected]` (string) |
111
|
|
|
// `address1` = `Dow 1` (string) |
112
|
|
|
// `address2` = `` (NULL) |
113
|
|
|
// `accepted` = `1` (boolean) |
114
|
|
|
// ------------------------------------------------------------------------------------------------------- |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param iterable $errors |
119
|
|
|
* |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
|
|
private function printErrors(iterable $errors): void |
123
|
|
|
{ |
124
|
|
|
$hasErrors = false; |
125
|
|
|
|
126
|
|
|
foreach ($errors as $error) { |
127
|
|
|
$hasErrors = true; |
128
|
|
|
|
129
|
|
|
/** @var ErrorInterface $error */ |
130
|
|
|
$paramName = $error->getParameterName(); |
131
|
|
|
$entry = empty($paramName) ? 'Validation' : "Param `$paramName`"; |
132
|
|
|
$paramValue = $error->getParameterValue(); |
133
|
|
|
$errorMsg = $error->getMessageTemplate(); |
134
|
|
|
$context = $error->getMessageParameters(); |
135
|
|
|
$errorMsg = MessageFormatter::formatMessage('en', $errorMsg, $context !== null ? $context : []); |
136
|
|
|
|
137
|
|
|
$this->console("$entry failed for `$paramValue` with: $errorMsg" . PHP_EOL); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if ($hasErrors === false) { |
141
|
|
|
$this->console('No errors' . PHP_EOL); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param iterable $captures |
147
|
|
|
* |
148
|
|
|
* @return void |
149
|
|
|
*/ |
150
|
|
|
private function printCaptures(iterable $captures): void |
151
|
|
|
{ |
152
|
|
|
$hasCaptures = false; |
153
|
|
|
|
154
|
|
|
foreach ($captures as $name => $value) { |
155
|
|
|
$hasCaptures = true; |
156
|
|
|
$type = gettype($value); |
157
|
|
|
$value = $value instanceof DateTimeInterface ? $value->format(DateTime::ISO8601) : $value; |
158
|
|
|
$this->console("`$name` = `$value` ($type)" . PHP_EOL); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if ($hasCaptures === false) { |
162
|
|
|
$this->console('No captures' . PHP_EOL); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string $string |
168
|
|
|
*/ |
169
|
|
|
private function console(string $string): void |
170
|
|
|
{ |
171
|
|
|
if ($this->isOutputToConsole === true) { |
172
|
|
|
echo $string; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
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: