Completed
Push — master ( c18000...efec97 )
by Anton
12s
created

Row::isBasic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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