Completed
Pull Request — master (#265)
by
unknown
05:08
created

Row::isBasic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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