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