ArrayRule.php
1 week ago
BooleanRule.php
1 week ago
ConditionalRule.php
1 week ago
DateRule.php
1 week ago
ExistsRule.php
1 week ago
FileRule.php
1 week ago
InRule.php
1 week ago
NotInRule.php
1 week ago
NullableRule.php
1 week ago
NumericRule.php
1 week ago
ObjectRule.php
1 week ago
ProhibitedIfRule.php
1 week ago
ProhibitedRule.php
1 week ago
ProhibitedUnlessRule.php
1 week ago
RequiredIfRule.php
1 week ago
RequiredRule.php
1 week ago
RequiredUnlessRule.php
1 week ago
SameAsRule.php
1 week ago
SometimesRule.php
1 week ago
StringRule.php
1 week ago
UniqueRule.php
1 week ago
UniqueRule.php
101 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Unique rule class. |
| 5 | * |
| 6 | * @package Framework |
| 7 | * @subpackage Validation |
| 8 | * @since 1.0.0 |
| 9 | */ |
| 10 | namespace Kirki\Framework\Validation\Rules; |
| 11 | |
| 12 | use Exception; |
| 13 | use Kirki\Framework\Supports\Arr; |
| 14 | use Kirki\Framework\Supports\Facades\DB; |
| 15 | use Kirki\Framework\Validation\ValidationRule; |
| 16 | use function Kirki\Framework\Polyfill\array_last; |
| 17 | \defined('ABSPATH') || exit; |
| 18 | /** |
| 19 | * Validates that the given value does not already exist in a database table column. |
| 20 | */ |
| 21 | class UniqueRule extends ValidationRule |
| 22 | { |
| 23 | /** |
| 24 | * The rule name. |
| 25 | * |
| 26 | * @var string |
| 27 | * |
| 28 | * @since 1.0.0 |
| 29 | */ |
| 30 | protected string $rule = 'unique'; |
| 31 | /** |
| 32 | * The constraints. |
| 33 | * |
| 34 | * @var array |
| 35 | * |
| 36 | * @since 1.0.0 |
| 37 | */ |
| 38 | protected array $constraints = []; |
| 39 | /** |
| 40 | * Validate the rule. |
| 41 | * |
| 42 | * @return bool |
| 43 | * |
| 44 | * @since 1.0.0 |
| 45 | */ |
| 46 | public function validate() : bool |
| 47 | { |
| 48 | if ($this->record_exists()) { |
| 49 | return $this->fails($this->default_messages['default']); |
| 50 | } |
| 51 | return \true; |
| 52 | } |
| 53 | /** |
| 54 | * Check whether a matching record already exists in the table. |
| 55 | * |
| 56 | * @return bool |
| 57 | * |
| 58 | * @since 1.0.0 |
| 59 | */ |
| 60 | protected function record_exists() |
| 61 | { |
| 62 | [$table, $column, $ignore_id] = $this->table_column_and_ignore_id(); |
| 63 | $query = DB::table($table)->where($column, $this->value); |
| 64 | if ($ignore_id !== null) { |
| 65 | $query->where('id', '!=', $ignore_id); |
| 66 | } |
| 67 | try { |
| 68 | $query->sole(); |
| 69 | return \true; |
| 70 | } catch (Exception $error) { |
| 71 | return \false; |
| 72 | } |
| 73 | } |
| 74 | /** |
| 75 | * Resolve the prefixed table name, column, and ignored id from the rule arguments. |
| 76 | * |
| 77 | * @return array |
| 78 | * |
| 79 | * @since 1.0.0 |
| 80 | */ |
| 81 | protected function table_column_and_ignore_id() |
| 82 | { |
| 83 | $arguments = Arr::wrap($this->args); |
| 84 | $table = $arguments[0]; |
| 85 | $column = !empty($arguments[1]) ? $arguments[1] : array_last(\explode('.', $this->name)); |
| 86 | $ignore_id = $arguments[2] ?? null; |
| 87 | return [$table, $column, $ignore_id]; |
| 88 | } |
| 89 | /** |
| 90 | * Get the error messages. |
| 91 | * |
| 92 | * @return array |
| 93 | * |
| 94 | * @since 1.0.0 |
| 95 | */ |
| 96 | public function messages() |
| 97 | { |
| 98 | return $this->process_messages($this->messages); |
| 99 | } |
| 100 | } |
| 101 |