Skip to content

Commit 60fef9f

Browse files
authored
Merge pull request #10 from arifszn/feat/rule-Hash
feat: rule hash
2 parents a60034d + 9551cd8 commit 60fef9f

File tree

4 files changed

+161
-1
lines changed

4 files changed

+161
-1
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function rules()
5252
- [`Divisible By`](#divisibleby)
5353
- [`Ethereum Address`](#ethereumaddress)
5454
- [`Float Number`](#floatnumber)
55+
- [`Hash`](#hash)
5556
- [`Image URL`](#imageurl)
5657
- [`Phone`](#phone)
5758
- [`Without Spaces`](#withoutspaces)
@@ -206,6 +207,23 @@ public function rules()
206207
}
207208
```
208209

210+
### `Hash`
211+
212+
The field under validation must be a hash of type algorithm.
213+
214+
Algorithm is one of `'md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b'`.
215+
216+
```php
217+
use Arifszn\AdvancedValidation\Rules\Hash;
218+
219+
public function rules()
220+
{
221+
return [
222+
'foo' => [new Hash()],
223+
];
224+
}
225+
```
226+
209227
### `ImageURL`
210228

211229
The field under validation must be a valid image URL.

resources/lang/en/validation.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
'data_uri' => 'The :attribute must have data uri format.',
1212
'divisible_by' => 'The :attribute must be divisible by :number.',
1313
'ethereum_address' => 'The :attribute must be an Ethereum address.',
14-
'float_number' => 'The :attribute must be a float number',
14+
'float_number' => 'The :attribute must be a float number.',
15+
'hash' => 'The :attribute must be a hash of :algorithm algorithm.',
1516
'image_url' => 'The :attribute must be a valid image URL.',
1617
'phone' => 'The :attribute must be a valid phone number.',
1718
'without_spaces' => 'The :attribute must not contain spaces.',

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+
}

tests/Rules/HashTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Arifszn\AdvancedValidation\Tests\Rules;
4+
5+
use Arifszn\AdvancedValidation\Rules\Hash;
6+
use Arifszn\AdvancedValidation\Tests\TestCase;
7+
8+
class HashTest extends TestCase
9+
{
10+
/**
11+
* @dataProvider provider
12+
*/
13+
public function testValidation($result, $value)
14+
{
15+
$algorithms = ['md5', 'md4', 'ripemd128', 'tiger128'];
16+
17+
foreach ($algorithms as $algorithm) {
18+
$this->assertEquals($result, (new Hash($algorithm))->passes('foo', $value));
19+
}
20+
}
21+
22+
public function provider()
23+
{
24+
return [
25+
[true, 'd94f3f016ae679c3008de268209132f2'],
26+
[true, '751adbc511ccbe8edf23d486fa4581cd'],
27+
[true, '88dae00e614d8f24cfd5a8b3f8002e93'],
28+
[true, '0bf1c35032a71a14c2f719e5a14c1e96'],
29+
[true, 'd94f3F016Ae679C3008de268209132F2'],
30+
[true, '88DAE00e614d8f24cfd5a8b3f8002E93'],
31+
32+
[false, 'q94375dj93458w34'],
33+
[false, '39485729348'],
34+
[false, '%&FHKJFvk'],
35+
[false, 'KYT0bf1c35032a71a14c2f719e5a1'],
36+
];
37+
}
38+
}

0 commit comments

Comments
 (0)