Passed
Push — main ( 073c01...b49c3d )
by Dimitri
03:21
created

Exists::check()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 11
rs 10
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
16
class Exists extends AbstractRule
17
{
18
    protected $message        = ':attribute :value do not exist';
19
    protected $fillableParams = ['table', 'column'];
20
21
22
    public function __construct(protected ConnectionInterface $db)
23
    {
24
    }
25
26
    public function check($value): bool
27
    {
28
        $this->requireParameters(['table']);
29
30
        $table  = $this->parameter('table');
31
        $column = $this->parameter('column');
32
        $column = $column ?: $this->getAttribute()->getKey();
33
34
		$builder = $this->db->table($table)->where($column, $value);
35
36
        return $builder->count() > 0;
37
    }
38
}
39