Skip to content

Commit 25ce5a0

Browse files
authored
Merge pull request #14 from arifszn/dev
Merge dev into main(1.0.1)
2 parents a60034d + 74dedef commit 25ce5a0

File tree

10 files changed

+620
-4
lines changed

10 files changed

+620
-4
lines changed

README.md

Lines changed: 145 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ php artisan vendor:publish --provider="Arifszn\AdvancedValidation\ServiceProvide
3030
You can specify the error message on the fly when declaring the rules. Simple pass the error message parameter.
3131

3232
```php
33-
use Arifszn\AdvancedValidation\Rules\Base64Image;
33+
use Arifszn\AdvancedValidation\Rules\Username;
3434

3535
public function rules()
3636
{
3737
return [
38-
'avatar' => [new Base64Image('Your custom error message')],
38+
'foo' => [new Username('Your custom error message')],
3939
];
4040
}
4141
```
@@ -52,15 +52,23 @@ public function rules()
5252
- [`Divisible By`](#divisibleby)
5353
- [`Ethereum Address`](#ethereumaddress)
5454
- [`Float Number`](#floatnumber)
55+
- [`Hash`](#hash)
5556
- [`Image URL`](#imageurl)
57+
- [`JWT`](#jwt)
58+
- [`Name`](#name)
5659
- [`Phone`](#phone)
60+
- [`Username`](#username)
5761
- [`Without Spaces`](#withoutspaces)
5862

5963

6064
### `Ascii`
6165

6266
The field under validation must contain ASCII chars only.
6367

68+
```
69+
public Arifszn\AdvancedValidation\Rules\Ascii::__construct(string $errorMessage = null)
70+
```
71+
6472
```php
6573
use Arifszn\AdvancedValidation\Rules\Ascii;
6674

@@ -76,6 +84,10 @@ public function rules()
7684

7785
The field under validation must be a Base64 encoded image.
7886

87+
```
88+
public Arifszn\AdvancedValidation\Rules\Base64Image::__construct(string $errorMessage = null)
89+
```
90+
7991
```php
8092
use Arifszn\AdvancedValidation\Rules\Base64Image;
8193

@@ -91,6 +103,10 @@ public function rules()
91103

92104
The field under validation must be a Base64 encoded string.
93105

106+
```
107+
public Arifszn\AdvancedValidation\Rules\Base64String::__construct(string $errorMessage = null)
108+
```
109+
94110
```php
95111
use Arifszn\AdvancedValidation\Rules\Base64String;
96112

@@ -106,6 +122,10 @@ public function rules()
106122

107123
The field under validation must be a BIC([Business Identifier Code](https://en.wikipedia.org/wiki/ISO_9362)) or SWIFT code.
108124

125+
```
126+
public Arifszn\AdvancedValidation\Rules\BIC::__construct(string $errorMessage = null)
127+
```
128+
109129
```php
110130
use Arifszn\AdvancedValidation\Rules\BIC;
111131

@@ -121,6 +141,10 @@ public function rules()
121141

122142
The field under validation must be a valid BTC address.
123143

144+
```
145+
public Arifszn\AdvancedValidation\Rules\BtcAddress::__construct(string $errorMessage = null)
146+
```
147+
124148
```php
125149
use Arifszn\AdvancedValidation\Rules\BtcAddress;
126150

@@ -136,6 +160,10 @@ public function rules()
136160

137161
The field under validation must be a valid credit card number.
138162

163+
```
164+
public Arifszn\AdvancedValidation\Rules\CreditCard::__construct(string $errorMessage = null)
165+
```
166+
139167
```php
140168
use Arifszn\AdvancedValidation\Rules\CreditCard;
141169

@@ -151,6 +179,10 @@ public function rules()
151179

152180
The field under validation must have [data uri format](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs).
153181

182+
```
183+
public Arifszn\AdvancedValidation\Rules\DataURI::__construct(string $errorMessage = null)
184+
```
185+
154186
```php
155187
use Arifszn\AdvancedValidation\Rules\DataURI;
156188

@@ -166,6 +198,10 @@ public function rules()
166198

167199
The field under validation must be divisible by the given number.
168200

201+
```
202+
public Arifszn\AdvancedValidation\Rules\DivisibleBy::__construct(int $number, string $errorMessage = null)
203+
```
204+
169205
```php
170206
use Arifszn\AdvancedValidation\Rules\DivisibleBy;
171207

@@ -180,6 +216,10 @@ public function rules()
180216
### `EthereumAddress`
181217
The field under validation must be an [Ethereum](https://ethereum.org/en/) address. Does not validate address checksums.
182218

219+
```
220+
public Arifszn\AdvancedValidation\Rules\EthereumAddress::__construct(string $errorMessage = null)
221+
```
222+
183223
```php
184224
use Arifszn\AdvancedValidation\Rules\EthereumAddress;
185225

@@ -195,6 +235,10 @@ public function rules()
195235

196236
The field under validation must be a float number.
197237

238+
```
239+
public Arifszn\AdvancedValidation\Rules\FloatNumber::__construct(string $errorMessage = null)
240+
```
241+
198242
```php
199243
use Arifszn\AdvancedValidation\Rules\FloatNumber;
200244

@@ -206,13 +250,38 @@ public function rules()
206250
}
207251
```
208252

253+
### `Hash`
254+
255+
The field under validation must be a hash of type algorithm.
256+
257+
Algorithm is one of `'md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b'`.
258+
259+
```
260+
public Arifszn\AdvancedValidation\Rules\Hash::__construct(string $algorithm, string $errorMessage = null)
261+
```
262+
263+
```php
264+
use Arifszn\AdvancedValidation\Rules\Hash;
265+
266+
public function rules()
267+
{
268+
return [
269+
'foo' => [new Hash('md4')],
270+
];
271+
}
272+
```
273+
209274
### `ImageURL`
210275

211276
The field under validation must be a valid image URL.
212277

213278
https://www.php.net/images/logos/php-logo.png \
214279
https://imaginarysite123.com/invalid.png
215280

281+
```
282+
public Arifszn\AdvancedValidation\Rules\ImageURL::__construct(string $errorMessage = null)
283+
```
284+
216285
```php
217286
use Arifszn\AdvancedValidation\Rules\ImageURL;
218287

@@ -224,6 +293,48 @@ public function rules()
224293
}
225294
```
226295

296+
### `JWT`
297+
298+
The field under validation must have a valid format of JWT ([JSON Web Token](https://en.wikipedia.org/wiki/JSON_Web_Token)).
299+
300+
```
301+
public Arifszn\AdvancedValidation\Rules\Jwt::__construct(string $errorMessage = null)
302+
```
303+
304+
```php
305+
use Arifszn\AdvancedValidation\Rules\Jwt;
306+
307+
public function rules()
308+
{
309+
return [
310+
'foo' => [new Jwt()],
311+
];
312+
}
313+
```
314+
315+
### `Name`
316+
317+
The field under validation must be a valid name.
318+
319+
- no emoji
320+
- no number (if $allowNumber flag is true, it will accept numbers, default is false)
321+
- special characters are allowed (restricting special characters will cause false-negative for names like `Martin Luther King, Jr.` or `李小龍`)
322+
323+
```
324+
public Arifszn\AdvancedValidation\Rules\Name::__construct(bool $allowNumber = false, string $errorMessage = null)
325+
```
326+
327+
```php
328+
use Arifszn\AdvancedValidation\Rules\Name;
329+
330+
public function rules()
331+
{
332+
return [
333+
'name' => [new Name()],
334+
];
335+
}
336+
```
337+
227338
### `Phone`
228339

229340
The field under validation must be a valid phone number.
@@ -233,6 +344,10 @@ The field under validation must be a valid phone number.
233344
✓ (xxx) xxx-xxxx \
234345
✓ xxxxxxxxxx
235346

347+
```
348+
public Arifszn\AdvancedValidation\Rules\Phone::__construct(string $errorMessage = null)
349+
```
350+
236351
```php
237352
use Arifszn\AdvancedValidation\Rules\Phone;
238353

@@ -244,10 +359,38 @@ public function rules()
244359
}
245360
```
246361

362+
### `Username`
363+
364+
The field under validation must be a valid username.
365+
366+
- starts with a letter (alpha)
367+
- only alpha-numeric (a-z, A-Z, 0-9), underscore, minus and dot
368+
- multiple underscores, minus and are not allowed (-- or __ or ..)
369+
- underscores, minus and dot are not allowed at the beginning or end
370+
371+
```
372+
public Arifszn\AdvancedValidation\Rules\Username::__construct(string $errorMessage = null)
373+
```
374+
375+
```php
376+
use Arifszn\AdvancedValidation\Rules\Username;
377+
378+
public function rules()
379+
{
380+
return [
381+
'username' => [new Username()],
382+
];
383+
}
384+
```
385+
247386
### `WithoutSpaces`
248387

249388
The field under validation must not contain spaces.
250389

390+
```
391+
public Arifszn\AdvancedValidation\Rules\WithoutSpaces::__construct(string $errorMessage = null)
392+
```
393+
251394
```php
252395
use Arifszn\AdvancedValidation\Rules\WithoutSpaces;
253396

resources/lang/en/validation.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
<?php
32

43
return [
@@ -11,8 +10,12 @@
1110
'data_uri' => 'The :attribute must have data uri format.',
1211
'divisible_by' => 'The :attribute must be divisible by :number.',
1312
'ethereum_address' => 'The :attribute must be an Ethereum address.',
14-
'float_number' => 'The :attribute must be a float number',
13+
'float_number' => 'The :attribute must be a float number.',
14+
'hash' => 'The :attribute must be a hash of :algorithm algorithm.',
1515
'image_url' => 'The :attribute must be a valid image URL.',
16+
'jwt' => 'The :attribute must have a valid format of JWT.',
17+
'name' => 'The :attribute must be a valid name.',
1618
'phone' => 'The :attribute must be a valid phone number.',
19+
'username' => 'The :attribute must be a valid username.',
1720
'without_spaces' => 'The :attribute must not contain spaces.',
1821
];

src/Rules/Hash.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace Arifszn\AdvancedValidation\Rules;
4+
5+
use Illuminate\Contracts\Validation\Rule;
6+
7+
/**
8+
* The field under validation must be a hash of type algorithm. Algorithm is one of
9+
* ['md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160',
10+
* 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']
11+
*
12+
* @package Arifszn\AdvancedValidation\Rules
13+
*/
14+
class Hash implements Rule
15+
{
16+
/**
17+
* @var string
18+
*/
19+
private $algorithm;
20+
21+
/**
22+
* @var string
23+
*/
24+
private $errorMessage;
25+
26+
/**
27+
* @var string
28+
*/
29+
private $attribute;
30+
31+
/**
32+
* Create a new rule instance.
33+
*
34+
* @param string $algorithm 'md4' | 'md5' | 'sha1' | 'sha256' | 'sha384' | 'sha512' | 'ripemd128' | 'ripemd160' | 'tiger128' | 'tiger160' | 'tiger192' | 'crc32' | 'crc32b'
35+
* @param string|null $errorMessage Custom error message.
36+
* @return void
37+
*/
38+
public function __construct(string $algorithm, string $errorMessage = null)
39+
{
40+
$this->algorithm = $algorithm;
41+
$this->errorMessage = $errorMessage;
42+
}
43+
44+
/**
45+
* Determine if the validation rule passes.
46+
*
47+
* @param string $attribute
48+
* @param mixed $value
49+
* @return bool
50+
*/
51+
public function passes($attribute, $value)
52+
{
53+
$this->attribute = $attribute;
54+
55+
try {
56+
$hash = '/^[a-fA-F0-9]{' . $this->getLength($this->algorithm) . '}$/';
57+
58+
return preg_match($hash, $value);
59+
} catch (\Throwable $th) {
60+
return false;
61+
}
62+
}
63+
64+
/**
65+
* Get the validation error message.
66+
*
67+
* @return string
68+
*/
69+
public function message()
70+
{
71+
return $this->errorMessage ? $this->errorMessage : trans('advancedValidation::validation.hash', [
72+
'attribute' => $this->attribute,
73+
'algorithm' => $this->algorithm
74+
]);
75+
}
76+
77+
/**
78+
* Get length.
79+
*
80+
* @param string $algorithm
81+
* @return int
82+
*/
83+
private function getLength(string $algorithm)
84+
{
85+
$lengths = [
86+
'md5' => 32,
87+
'md4' => 32,
88+
'sha1' => 40,
89+
'sha256' => 64,
90+
'sha384' => 96,
91+
'sha512' => 128,
92+
'ripemd128' => 32,
93+
'ripemd160' => 40,
94+
'tiger128' => 32,
95+
'tiger160' => 40,
96+
'tiger192' => 48,
97+
'crc32' => 8,
98+
'crc32b' => 8,
99+
];
100+
101+
return $lengths[$algorithm];
102+
}
103+
}

0 commit comments

Comments
 (0)