Completed
Push — master ( e5a491...150fe1 )
by Julito
11:53
created

Group::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Entity;
6
7
use Doctrine\ORM\Mapping as ORM;
8
9
/**
10
 * @ORM\Entity()
11
 * @ORM\Table(name="fos_group")
12
 */
13
class Group
14
{
15
    /**
16
     * @var int
17
     *
18
     * @ORM\Column(name="id", type="integer")
19
     * @ORM\Id
20
     * @ORM\GeneratedValue(strategy="AUTO")
21
     */
22
    protected $id;
23
24
    /**
25
     * @var string
26
     * @ORM\Column(name="name", type="string", length=255, nullable=false, unique=true)
27
     */
28
    protected $name;
29
30
    /**
31
     * @var array
32
     * @ORM\Column(name="roles", type="array")
33
     */
34
    protected $roles;
35
36
    /**
37
     * @ORM\ManyToMany(targetEntity="Chamilo\CoreBundle\Entity\User", mappedBy="groups")
38
     */
39
    protected $users;
40
41
    /**
42
     * @var string
43
     *
44
     * @ORM\Column(name="code", type="string", length=40, nullable=false, unique=true)
45
     */
46
    protected $code;
47
48
    public function __construct($name, $roles = [])
49
    {
50
        $this->name = $name;
51
        $this->roles = $roles;
52
    }
53
54
    public function __toString()
55
    {
56
        return $this->getName() ?: '';
57
    }
58
59
    public function addRole($role)
60
    {
61
        if (!$this->hasRole($role)) {
62
            $this->roles[] = strtoupper($role);
63
        }
64
65
        return $this;
66
    }
67
68
    public function hasRole($role)
69
    {
70
        return in_array(strtoupper($role), $this->roles, true);
71
    }
72
73
    public function getRoles()
74
    {
75
        return $this->roles;
76
    }
77
78
    public function removeRole($role)
79
    {
80
        if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
81
            unset($this->roles[$key]);
82
            $this->roles = array_values($this->roles);
83
        }
84
85
        return $this;
86
    }
87
88
    public function getName(): string
89
    {
90
        return $this->name;
91
    }
92
93
    public function setName(string $name): self
94
    {
95
        $this->name = $name;
96
97
        return $this;
98
    }
99
100
    /**
101
     * @return Group
102
     */
103
    public function setRoles(array $roles)
104
    {
105
        $this->roles = $roles;
106
107
        return $this;
108
    }
109
110
    /**
111
     * Get id.
112
     *
113
     * @return int
114
     */
115
    public function getId()
116
    {
117
        return $this->id;
118
    }
119
120
    public function getUsers()
121
    {
122
        return $this->users;
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    public function getCode()
129
    {
130
        return $this->code;
131
    }
132
133
    /**
134
     * @param string $code
135
     *
136
     * @return Group
137
     */
138
    public function setCode($code)
139
    {
140
        $this->code = $code;
141
142
        return $this;
143
    }
144
}
145