Passed
Push — main ( c6deb1...79ccdf )
by Dimitri
12:23
created

Unique::ignoreModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Validation\Rules;
13
14
use BlitzPHP\Contracts\Database\ConnectionInterface;
15
use BlitzPHP\Wolke\Model;
0 ignored issues
show
Bug introduced by
The type BlitzPHP\Wolke\Model was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Dimtrovich\Validation\Rules\AbstractRule;
17
18
class Unique extends AbstractRule
19
{
20
    protected $message        = ':attribute :value has been used';
21
    protected $fillableParams = ['table', 'column', 'ignore'];
22
23
	/**
24
     * The name of the ID column.
25
     */
26
    protected string $idColumn = 'id';
27
28
	protected string $deletedAtColumn = '';
29
30
31
    public function __construct(protected ConnectionInterface $db)
32
    {
33
    }
34
35
	/**
36
     * Ignore the given ID during the unique check.
37
     */
38
    public function ignore(mixed $id, ?string $idColumn = null): self
39
    {
40
		if (class_exists(Model::class) && $id instanceof Model) {
41
            return $this->ignoreModel($id, $idColumn);
42
        }
43
		
44
        $this->params['ignore'] = $id;
45
        $this->idColumn         = $idColumn ?? 'id';
46
		
47
48
        return $this;
49
    }
50
51
	/**
52
     * Ignore the given model during the unique check.
53
     */
54
    public function ignoreModel(Model $entity, ?string $idColumn = null): self
55
    {
56
		$this->idColumn         = $idColumn ?? $entity->getKeyName();
57
        $this->params['ignore'] = $entity->{$this->idColumn};
58
59
        return $this;
60
    }
61
62
	/**
63
     * Ignore soft deleted models during the unique check.
64
     */
65
    public function withoutTrashed(string $deletedAtColumn = 'deleted_at'): self
66
    {
67
		$this->deletedAtColumn = $deletedAtColumn;
68
69
        return $this;
70
    }
71
72
    public function check($value): bool
73
    {
74
        $this->requireParameters(['table']);
75
76
        $table  = $this->parameter('table');
77
        $ignore = $this->parameter('ignore');
78
        $column = $this->parameter('column');
79
        $column = $column ?: $this->getAttribute()->getKey();
80
81
		$builder = $this->db->table($table)->where($column, $value);
82
83
		if ($ignore) {
84
			$builder->where($this->idColumn . ' !=', $ignore);
85
		}
86
		if ('' !== $this->deletedAtColumn) {
87
			$builder->where($this->deletedAtColumn . ' IS NULL');
88
		}
89
90
        return $builder->count() === 0;
91
    }
92
}
93