Test Failed
Push — master ( 741545...e25e21 )
by Julien
11:23
created

Group::beforeValidation()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 10
cc 3
nc 4
nop 0
crap 12
1
<?php
2
/**
3
 * This file is part of the Zemit Framework.
4
 *
5
 * (c) Zemit Team <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE.txt
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Zemit\Models;
12
13
use Zemit\Models\Base\AbstractGroup;
14
use Phalcon\Validation\Validator\PresenceOf;
15
use Phalcon\Validation\Validator\StringLength\Max;
16
17
/**
18
 * Class Group
19
 *
20
 * @property UserGroup[] $UserNode
21
 * @property User[] $UserList
22
 * @property GroupRole[] $RoleNode
23
 * @property Role[] $RoleList
24
 * @property GroupType[] $TypeNode
25
 * @property Type[] $TypeList
26
 *
27
 * @method UserGroup[] getUserNode($params = null)
28
 * @method User[] getUserList($params = null)
29
 * @method GroupRole[] getRoleNode($params = null)
30
 * @method Role[] getRoleList($params = null)
31
 * @method GroupType[] getTypeNode($params = null)
32
 * @method Type[] getTypeList($params = null)
33
 *
34
 * @package Zemit\Models
35
 */
36
class Group extends AbstractGroup
37
{
38
    protected $deleted = self::NO;
39
    protected $position = self::NO;
40
41
    public function initialize()
42
    {
43
        parent::initialize();
44
45
        // User relationship
46
        $this->hasMany('id', UserGroup::class, 'groupId', ['alias' => 'UserNode']);
47
        $this->hasManyToMany('id', UserGroup::Class, 'groupId',
0 ignored issues
show
Bug introduced by
The constant Zemit\Models\UserGroup::Class was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
48
            'userId', User::class, 'id', ['alias' => 'UserList']);
49
50
        // Role relationship
51
        $this->hasMany('id', GroupRole::class, 'groupId', ['alias' => 'RoleNode']);
52
        $this->hasManyToMany('id', GroupRole::class, 'groupId',
53
            'roleId', Role::class, 'id', ['alias' => 'RoleList']);
54
55
        // Type relationship
56
        $this->hasMany('id', GroupType::class, 'groupId', ['alias' => 'TypeNode']);
57
        $this->hasManyToMany('id', GroupType::class, 'groupId',
58
            'typeId', Type::class, 'id', ['alias' => 'TypeList']);
59
    }
60
61
    public function beforeValidation()
62
    {
63
        if (!$this->index) {
64
            $this->setIndex($this->getLabelFr());
65
        }
66
        if (!$this->labelEn) {
67
            $this->setLabelEn($this->getLabelFr());
68
        }
69
    }
70
71
    public function validation()
72
    {
73
        $validator = $this->genericValidation();
74
75
        $validator->add('index', new Max(['max' => 50, 'message' => $this->_('index') .': '. $this->_('length-exceeded')]));
76
        $validator->add('index', new PresenceOf(['message' => $this->_('index') .': '. $this->_('required')]));
77
78
        $validator->add('labelFr', new PresenceOf(['message' => $this->_('label-fr') .': '. $this->_('required')]));
79
        $validator->add('labelFr', new Max(['max' => 100, 'message' => $this->_('label-fr') .': '. $this->_('length-exceeded')]));
80
81
        $validator->add('labelEn', new PresenceOf(['message' => $this->_('label-en') .': '. $this->_('required')]));
82
        $validator->add('labelFr', new Max(['max' => 100, 'message' => $this->_('label-en') .': '. $this->_('length-exceeded')]));
83
84
        return $this->validate($validator);
85
    }
86
}
87