| @@ -1,72 +1,72 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace Booktics\Validation\Rules; | |
| 4 | - | |
| 5 | -/** | |
| 6 | - * Class Unique_Rule | |
| 7 | - */ | |
| 8 | -class Unique_Rule implements Rule { | |
| 9 | - | |
| 10 | - /** | |
| 11 | - * Store table name | |
| 12 | - * | |
| 13 | - * @var string | |
| 14 | - */ | |
| 15 | - private $table; | |
| 16 | - | |
| 17 | - /** | |
| 18 | - * Store column name | |
| 19 | - * | |
| 20 | - * @var string | |
| 21 | - */ | |
| 22 | - private $column; | |
| 23 | - | |
| 24 | - /** | |
| 25 | - * Store except id | |
| 26 | - * | |
| 27 | - * @var mixed|null | |
| 28 | - */ | |
| 29 | - private $exceptId; | |
| 30 | - | |
| 31 | - public function __construct( string $table, string $column, $exceptId = null ) { | |
| 32 | - $this->table = $table; | |
| 33 | - $this->column = $column; | |
| 34 | - $this->exceptId = $exceptId; | |
| 35 | - } | |
| 36 | - | |
| 37 | - /** | |
| 38 | - * Check the rule passed or not | |
| 39 | - * | |
| 40 | - * @param any $field | |
| 41 | - * @param any $value | |
| 42 | - * | |
| 43 | - * @return bool | |
| 44 | - */ | |
| 45 | - public function passes( $field, $value ): bool { | |
| 46 | - if ( $this->table === 'wp_users' ) { | |
| 47 | - $existing_user = get_user_by( $field, $value ); | |
| 48 | - | |
| 49 | - if ( ! $existing_user ) { | |
| 50 | - return true; | |
| 51 | - } | |
| 52 | - | |
| 53 | - if ( intval( $existing_user->ID ) === intval( $this->exceptId ) ) { | |
| 54 | - return true; | |
| 55 | - } | |
| 56 | - | |
| 57 | - return false; | |
| 58 | - } | |
| 59 | - return true; | |
| 60 | - } | |
| 61 | - | |
| 62 | - /** | |
| 63 | - * Set validation message | |
| 64 | - * | |
| 65 | - * @param any $field | |
| 66 | - * | |
| 67 | - * @return string | |
| 68 | - */ | |
| 69 | - public function message( $field ) { | |
| 70 | - return $field . ' is not unique'; | |
| 71 | - } | |
| 72 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace Booktics\Validation\Rules; | |
| 4 | + | |
| 5 | +/** | |
| 6 | + * Class Unique_Rule | |
| 7 | + */ | |
| 8 | +class Unique_Rule implements Rule { | |
| 9 | + | |
| 10 | + /** | |
| 11 | + * Store table name | |
| 12 | + * | |
| 13 | + * @var string | |
| 14 | + */ | |
| 15 | + private $table; | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * Store column name | |
| 19 | + * | |
| 20 | + * @var string | |
| 21 | + */ | |
| 22 | + private $column; | |
| 23 | + | |
| 24 | + /** | |
| 25 | + * Store except id | |
| 26 | + * | |
| 27 | + * @var mixed|null | |
| 28 | + */ | |
| 29 | + private $except_id; | |
| 30 | + | |
| 31 | + public function __construct( string $table, string $column, $except_id = null ) { | |
| 32 | + $this->table = $table; | |
| 33 | + $this->column = $column; | |
| 34 | + $this->except_id = $except_id; | |
| 35 | + } | |
| 36 | + | |
| 37 | + /** | |
| 38 | + * Check the rule passed or not | |
| 39 | + * | |
| 40 | + * @param any $field | |
| 41 | + * @param any $value | |
| 42 | + * | |
| 43 | + * @return bool | |
| 44 | + */ | |
| 45 | + public function passes( $field, $value ): bool { | |
| 46 | + if ( $this->table === 'wp_users' ) { | |
| 47 | + $existing_user = get_user_by( $field, $value ); | |
| 48 | + | |
| 49 | + if ( ! $existing_user ) { | |
| 50 | + return true; | |
| 51 | + } | |
| 52 | + | |
| 53 | + if ( intval( $existing_user->ID ) === intval( $this->except_id ) ) { | |
| 54 | + return true; | |
| 55 | + } | |
| 56 | + | |
| 57 | + return false; | |
| 58 | + } | |
| 59 | + return true; | |
| 60 | + } | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * Set validation message | |
| 64 | + * | |
| 65 | + * @param any $field | |
| 66 | + * | |
| 67 | + * @return string | |
| 68 | + */ | |
| 69 | + public function message( $field ) { | |
| 70 | + return $field . ' is not unique'; | |
| 71 | + } | |
| 72 | +} | |