Skip to content

Commit 11bf0c4

Browse files
Added validation helpers: created shortcut functions for validators (alphabetic, alphanumeric, boolean, choice, etc.). Each helper allows easy instantiation of validators with an optional custom message.
1 parent 4ffca13 commit 11bf0c4

File tree

6 files changed

+393
-3
lines changed

6 files changed

+393
-3
lines changed

Diff for: composer.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
"psr-4": {
1414
"PhpDevCommunity\\Validator\\": "src",
1515
"Test\\PhpDevCommunity\\Validator\\": "tests"
16-
}
16+
},
17+
"files": [
18+
"functions/helpers.php"
19+
]
1720
},
1821
"minimum-stability": "alpha",
1922
"require": {

Diff for: functions/helpers.php

+205
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
<?php
2+
3+
use PhpDevCommunity\Validator\Assert\Alphabetic;
4+
use PhpDevCommunity\Validator\Assert\Alphanumeric;
5+
use PhpDevCommunity\Validator\Assert\Boolean;
6+
use PhpDevCommunity\Validator\Assert\Choice;
7+
use PhpDevCommunity\Validator\Assert\Collection;
8+
use PhpDevCommunity\Validator\Assert\Custom;
9+
use PhpDevCommunity\Validator\Assert\Email;
10+
use PhpDevCommunity\Validator\Assert\Integer;
11+
use PhpDevCommunity\Validator\Assert\Item;
12+
use PhpDevCommunity\Validator\Assert\NotEmpty;
13+
use PhpDevCommunity\Validator\Assert\NotNull;
14+
use PhpDevCommunity\Validator\Assert\Numeric;
15+
use PhpDevCommunity\Validator\Assert\Psr7UploadFile;
16+
use PhpDevCommunity\Validator\Assert\StringLength;
17+
use PhpDevCommunity\Validator\Assert\Url;
18+
19+
if (!function_exists('v_alphabetic')) {
20+
function v_alphabetic(string $message = null): Alphabetic
21+
{
22+
$validator = new Alphabetic();
23+
if ($message !== null) {
24+
$validator->message($message);
25+
}
26+
return $validator;
27+
}
28+
}
29+
30+
if (!function_exists('v_alphanumeric')) {
31+
function v_alphanumeric(string $message = null): Alphanumeric
32+
{
33+
$validator = new Alphanumeric();
34+
if ($message !== null) {
35+
$validator->message($message);
36+
}
37+
return $validator;
38+
}
39+
}
40+
41+
if (!function_exists('v_boolean')) {
42+
function v_boolean(string $message = null): PhpDevCommunity\Validator\Assert\Boolean
43+
{
44+
$validator = new Boolean();
45+
if ($message !== null) {
46+
$validator->message($message);
47+
}
48+
return $validator;
49+
}
50+
}
51+
52+
53+
if (!function_exists('v_choice')) {
54+
function v_choice(array $choices, string $message = null): Choice
55+
{
56+
$validator = new Choice($choices);
57+
if ($message !== null) {
58+
$validator->message($message);
59+
}
60+
return $validator;
61+
}
62+
}
63+
64+
if (!function_exists('v_collection')) {
65+
function v_collection(array $validators, string $message = null): Collection
66+
{
67+
$validator = new Collection($validators);
68+
if ($message !== null) {
69+
$validator->message($message);
70+
}
71+
return $validator;
72+
}
73+
}
74+
75+
if (!function_exists('v_custom')) {
76+
function v_custom(callable $validate, string $message = null): Custom
77+
{
78+
$validator = new Custom($validate);
79+
if ($message !== null) {
80+
$validator->message($message);
81+
}
82+
return $validator;
83+
}
84+
}
85+
86+
87+
if (!function_exists('v_email')) {
88+
function v_email(string $message = null): Email
89+
{
90+
$validator = new Email();
91+
if ($message !== null) {
92+
$validator->message($message);
93+
}
94+
return $validator;
95+
}
96+
}
97+
98+
if (!function_exists('v_integer')) {
99+
function v_integer(?int $min, ?int $max = null,string $invalidMessage = null): PhpDevCommunity\Validator\Assert\Integer
100+
{
101+
$validator = new Integer();
102+
if ($invalidMessage !== null) {
103+
$validator->invalidMessage($invalidMessage);
104+
}
105+
106+
if ($min !== null) {
107+
$validator->min($min);
108+
}
109+
if ($max !== null) {
110+
$validator->max($max);
111+
}
112+
113+
return $validator;
114+
}
115+
}
116+
117+
if (!function_exists('v_item')) {
118+
function v_item(array $validators, string $message = null): Item
119+
{
120+
$validator = new Item($validators);
121+
if ($message !== null) {
122+
$validator->message($message);
123+
}
124+
return $validator;
125+
}
126+
}
127+
128+
if (!function_exists('v_not_empty')) {
129+
function v_not_empty(string $message = null): NotEmpty
130+
{
131+
$validator = new NotEmpty();
132+
if ($message !== null) {
133+
$validator->message($message);
134+
}
135+
return $validator;
136+
}
137+
}
138+
139+
if (!function_exists('v_not_null')) {
140+
function v_not_null(string $message = null): NotNull
141+
{
142+
$validator = new NotNull();
143+
if ($message !== null) {
144+
$validator->message($message);
145+
}
146+
return $validator;
147+
}
148+
}
149+
150+
151+
if (!function_exists('v_numeric')) {
152+
function v_numeric(string $message = null): Numeric
153+
{
154+
$validator = new Numeric();
155+
if ($message !== null) {
156+
$validator->message($message);
157+
}
158+
return $validator;
159+
}
160+
}
161+
162+
if (!function_exists('v_psr7_upload_file')) {
163+
function v_psr7_upload_file(?int $maxSize, array $allowedMimeTypes, string $message = null): Psr7UploadFile
164+
{
165+
$validator = new Psr7UploadFile();
166+
if ($message !== null) {
167+
$validator->message($message);
168+
}
169+
if ($maxSize !== null) {
170+
$validator->maxSize($maxSize);
171+
}
172+
$validator->mimeTypes($allowedMimeTypes);
173+
return $validator;
174+
}
175+
}
176+
177+
if (!function_exists('v_string_length')) {
178+
function v_string_length(?int $min, ?int $max = null, string $invalidMessage = null): StringLength
179+
{
180+
$validator = new StringLength();
181+
if ($invalidMessage !== null) {
182+
$validator->invalidMessage($invalidMessage);
183+
}
184+
185+
if ($min !== null) {
186+
$validator->min($min);
187+
}
188+
if ($max !== null) {
189+
$validator->max($max);
190+
}
191+
192+
return $validator;
193+
}
194+
}
195+
196+
if (!function_exists('v_url')) {
197+
function v_url(string $message = null): Url
198+
{
199+
$validator = new Url();
200+
if ($message !== null) {
201+
$validator->message($message);
202+
}
203+
return $validator;
204+
}
205+
}

Diff for: src/Assert/AbstractValidator.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ public function getError(): ?string
2020
{
2121
return $this->error;
2222
}
23-
24-
23+
2524
/**
2625
* Set the error message by replacing placeholders with values from the context array
2726
*

Diff for: src/Assert/Boolean.php

100755100644
File mode changed.

Diff for: src/Validation.php

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public function __construct(array $fieldValidators)
4141
if (!is_array($validators)) {
4242
$validators = [$validators];
4343
}
44+
if (!is_string($field)) {
45+
throw new InvalidArgumentException('The field name must be a string');
46+
}
4447
$this->addValidator($field, $validators);
4548
}
4649
}

0 commit comments

Comments
 (0)