Completed
Branch master (495df4)
by Anton
01:49
created

Row::beforeUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 22
ccs 0
cts 18
cp 0
crap 2
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link      https://github.com/bluzphp/skeleton
5
 */
6
7
/**
8
 * @namespace
9
 */
10
11
namespace Application\Roles;
12
13
use Bluz\Validator\Traits\Validator;
14
use Bluz\Validator\Validator as v;
15
16
/**
17
 * User Role
18
 *
19
 * @package Application\Roles
20
 *
21
 * @property integer $id
22
 * @property string  $name
23
 */
24
class Row extends \Bluz\Db\Row
25
{
26
    use Validator;
27
28
    /**
29
     * Before Insert/Update
30
     *
31
     * @return void
32
     */
33
    protected function beforeInsert()
34
    {
35
        $this->addValidator(
36
            'name',
37
            v::required()->latin(),
38
            v::callback(
39
                function ($name) {
40
                    return !Table::getInstance()->findRowWhere(['name' => $name]);
41
                }
42
            )->setError('Role name "{{input}}" already exists')
43
        );
44
    }
45
46
    /**
47
     * Before Update
48
     *
49
     * @return void
50
     */
51
    protected function beforeUpdate()
52
    {
53
        $this->addValidator(
54
            'name',
55
            v::required()->latin(),
56
            v::callback(
57
                function ($name) {
58
                    return !in_array(strtolower($name), Table::getInstance()->getBasicRoles());
59
                }
60
            )->setError('Role "{{input}}" is basic and can\'t be editable'),
61
            v::callback(
62
                function ($name) {
63
                    return $this->clean['name'] != $name;
64
                }
65
            )->setError('Role name "{{input}}" the same as original'),
66
            v::callback(
67
                function ($name) {
68
                    return !Table::getInstance()->findRowWhere(['name' => $name]);
69
                }
70
            )->setError('Role name "{{input}}" already exists')
71
        );
72
    }
73
74
    /**
75
     * isBasic
76
     *
77
     * @return boolean
78
     */
79 1
    public function isBasic()
80
    {
81 1
        return in_array(strtolower($this->name), Table::getInstance()->getBasicRoles());
82
    }
83
}
84