1
|
|
|
<?php namespace Sample; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2016 [email protected] (www.neomerx.com) |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use Limoncello\Validation\Errors\Error; |
20
|
|
|
use Sample\Validation\Translator; |
21
|
|
|
use Sample\Validation\Validator as v; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @package Sample |
25
|
|
|
*/ |
26
|
|
|
class Application |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var bool |
30
|
|
|
*/ |
31
|
|
|
private $isOutputToConsole; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param bool $isOutputToConsole |
35
|
|
|
*/ |
36
|
|
|
public function __construct($isOutputToConsole = true) |
37
|
|
|
{ |
38
|
|
|
$this->isOutputToConsole = $isOutputToConsole; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function run() |
45
|
|
|
{ |
46
|
|
|
// Validation rules for input are |
47
|
|
|
// - `email` must be a string and a valid email value (as FILTER_VALIDATE_EMAIL describes) |
48
|
|
|
// - `first_name` required in input, must be a string with length from 1 to 255 characters |
49
|
|
|
// - `last_name` could be either `null` or if given it must be a string with length from 1 to 255 characters |
50
|
|
|
// - `payment_plan` must be a valid index for data in database (we will emulate request to database) |
51
|
|
|
// - `interests` must be an array of non-empty strings (any number of items, no limit for max length) |
52
|
|
|
|
53
|
|
|
$invalidInput = [ |
54
|
|
|
'email' => 'john.dow', |
55
|
|
|
//'first_name' => 'John', |
|
|
|
|
56
|
|
|
'last_name' => '', |
57
|
|
|
'payment_plan' => 123, |
58
|
|
|
'interests' => ['leisure', false, 'php', 321], |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
$validInput = [ |
62
|
|
|
'email' => '[email protected]', |
63
|
|
|
'first_name' => 'John', |
64
|
|
|
'last_name' => null, |
65
|
|
|
'payment_plan' => 2, |
66
|
|
|
'interests' => ['leisure', 'php', 'programming'], |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
// Having app specific rules separated makes the code easier to read and reuse. |
70
|
|
|
// Though you can have the rules in-lined. |
71
|
|
|
|
72
|
|
|
$rules = [ |
73
|
|
|
'email' => v::isEmail(), |
74
|
|
|
'first_name' => v::isRequiredString(255), |
75
|
|
|
'last_name' => v::isNullOrNonEmptyString(255), |
76
|
|
|
'payment_plan' => v::isExistingPaymentPlan(), |
77
|
|
|
'interests' => v::isListOfStrings(), |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
$this->console('Invalid data' . PHP_EOL); |
81
|
|
|
$this->printErrors( |
82
|
|
|
v::validator(v::arrayX($rules))->validate($invalidInput) |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
$this->console(PHP_EOL . 'Valid data' . PHP_EOL); |
86
|
|
|
$this->printErrors( |
87
|
|
|
v::validator(v::arrayX($rules))->validate($validInput) |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
// Note that error message placeholders like `first_name` are replaced with |
91
|
|
|
// more readable such as `First Name` and others. |
92
|
|
|
// |
93
|
|
|
// The output would be |
94
|
|
|
// ------------------------------------------------------------------------------------------------------- |
95
|
|
|
// Invalid data |
96
|
|
|
// Param `email` failed for `john.dow` with: The `Email address` value should be a valid email address. |
97
|
|
|
// Param `last_name` failed for `` with: The `Last Name` value should be between 1 and 255 characters. |
98
|
|
|
// Param `payment_plan` failed for `123` with: The `Payment plan` value should be an existing payment plan. |
99
|
|
|
// Param `interests` failed for `` with: The `Interests` value should be a string. |
100
|
|
|
// Param `interests` failed for `321` with: The `Interests` value should be a string. |
101
|
|
|
// Param `first_name` failed for `` with: The `First Name` value is required. |
102
|
|
|
// |
103
|
|
|
// Valid data |
104
|
|
|
// No errors |
105
|
|
|
// ------------------------------------------------------------------------------------------------------- |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param $errors |
110
|
|
|
*/ |
111
|
|
|
private function printErrors($errors) |
112
|
|
|
{ |
113
|
|
|
$hasErrors = false; |
114
|
|
|
$translator = new Translator(); |
115
|
|
|
|
116
|
|
|
foreach ($errors as $error) { |
117
|
|
|
$hasErrors = true; |
118
|
|
|
/** @var Error $error */ |
119
|
|
|
$paramName = $error->getParameterName(); |
120
|
|
|
$paramValue = $error->getParameterValue(); |
121
|
|
|
$errorMsg = $translator->translate($error); |
122
|
|
|
|
123
|
|
|
$this->console("Param `$paramName` failed for `$paramValue` with: $errorMsg" . PHP_EOL); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if ($hasErrors === false) { |
127
|
|
|
$this->console('No errors' . PHP_EOL); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $string |
133
|
|
|
*/ |
134
|
|
|
private function console($string) |
135
|
|
|
{ |
136
|
|
|
if ($this->isOutputToConsole === true) { |
137
|
|
|
echo $string; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.