UniqueJsonRule::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace TopviewDigital\UniqueJsonRule;
4
5
class UniqueJsonRule
6
{
7
    /**
8
     * @var string
9
     */
10
    protected $rule = 'unique_json';
11
    /**
12
     * @var string
13
     */
14
    protected $table;
15
    /**
16
     * @var string|null
17
     */
18
    protected $column = null;
19
    /**
20
     * @var mixed
21
     */
22
    protected $ignoreValue = null;
23
    /**
24
     * @var string|null
25
     */
26
    protected $ignoreColumn = null;
27
28
    /**
29
     * Create a new rule instance.
30
     *
31
     * @param string      $table
32
     * @param string|null $column
33
     *
34
     * @return static
35
     */
36
    public static function for($table, $column = null)
37
    {
38
        return new static($table, $column);
39
    }
40
41
    /**
42
     * Create a new rule instance.
43
     *
44
     * @param string      $table
45
     * @param string|null $column
46
     */
47
    public function __construct($table, $column = null)
48
    {
49
        $this->table = $table;
50
        $this->column = $column;
51
    }
52
53
    /**
54
     * Ignore any record that has a column with the given value.
55
     *
56
     * @param mixed  $value
57
     * @param string $column
58
     *
59
     * @return $this
60
     */
61
    public function ignore($value, $column = 'id')
62
    {
63
        $this->ignoreValue = $value;
64
        $this->ignoreColumn = $column;
65
66
        return $this;
67
    }
68
69
    /**
70
     * Generate a string representation of the validation rule.
71
     *
72
     * @return string
73
     */
74
    public function __toString()
75
    {
76
        return sprintf(
77
            '%s:%s,%s,%s,%s',
78
            $this->rule,
79
            $this->table,
80
            $this->column ?: 'NULL',
81
            $this->ignoreValue ?: 'NULL',
82
            $this->ignoreColumn ?: 'NULL'
83
        );
84
    }
85
}
86