getArrayAttribute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace Kuleuven\AuthenticationBundle\Traits;
4
5
trait ShibbolethAttributesResolverTrait
6
{
7
    /**
8
     * @var array
9
     */
10
    protected $attributes = [];
11
12
    /**
13
     * Returns the attributes.
14
     *
15
     * @return array The attributes
16
     */
17
    public function getAttributes()
18
    {
19
        return $this->attributes;
20
    }
21
22
    /**
23
     * Sets the attributes.
24
     *
25
     * @param array $attributes The attributes
26
     */
27
    public function setAttributes(array $attributes)
28
    {
29
        $this->attributes = $attributes;
30
    }
31
32
    /**
33
     * Returns true if the attribute exists.
34
     *
35
     * @param string $name The attribute name
36
     * @return bool true if the attribute exists, false otherwise
37
     */
38
    public function hasAttribute($name)
39
    {
40
        return array_key_exists($name, $this->attributes);
41
    }
42
43
    /**
44
     * Returns an attribute value.
45
     *
46
     * @param string $name The attribute name
47
     * @return mixed The attribute value
48
     * @throws \InvalidArgumentException When attribute doesn't exist
49
     */
50
    public function getAttribute($name)
51
    {
52
        if (!array_key_exists($name, $this->attributes)) {
53
            return null;
54
        }
55
56
        return $this->attributes[$name];
57
    }
58
59
    /**
60
     * Sets an attribute.
61
     *
62
     * @param string $name  The attribute name
63
     * @param mixed  $value The attribute value
64
     */
65
    public function setAttribute($name, $value)
66
    {
67
        $this->attributes[$name] = $value;
68
    }
69
70
    /**
71
     * Returns attribute value. If it's a multivalue, the first value is returned, or the value at the specified index.
72
     *
73
     * @param string $name
74
     * @param null   $index
75
     * @return mixed
76
     */
77
    public function getSingleAttribute($name, $index = null)
78
    {
79
        $value = $this->getAttribute($name);
80
        if (!is_array($value)) {
81
            return $value;
82
        }
83
        return (null === $index ? reset($value) : $value[$index]);
84
    }
85
86
    /**
87
     * Returns an attribute as an array of values.
88
     *
89
     * @param string $name
90
     * @return array
91
     */
92
    public function getArrayAttribute($name)
93
    {
94
        $value = $this->getAttribute($name);
95
        return (is_array($value) ? $value : [$value]);
96
    }
97
98
    /**
99
     * Returns true if attribute exists (if value is given, it will also check the value).
100
     *
101
     * @param string      $name
102
     * @param null|string $value
103
     * @return bool
104
     */
105
    public function hasAttributeValue($name, $value = null)
106
    {
107
        if (!$this->hasAttribute($name)) return false;
108
        return (empty($value) ? true : (array_search($value, $this->getArrayAttribute($name)) !== false));
109
    }
110
111
    /**
112
     * @return string uid
113
     */
114
    public function getUID()
115
    {
116
        return $this->getAttribute('Shib-Person-uid');
117
    }
118
119
    /**
120
     * Alias for cn
121
     *
122
     * @return string cn
123
     */
124
    public function getCommonName()
125
    {
126
        return $this->getAttribute('Shib-Person-commonName');
127
    }
128
129
    /**
130
     * Alias for cn
131
     *
132
     * @return string cn
133
     */
134
    public function getFullName()
135
    {
136
        return $this->getCommonName();
137
    }
138
139
    /**
140
     * @return string givenName
141
     */
142
    public function getGivenName()
143
    {
144
        return $this->getAttribute('Shib-Person-givenName');
145
    }
146
147
    /**
148
     * Alias for givenName
149
     *
150
     * @return string givenName
151
     */
152
    public function getFirstName()
153
    {
154
        return $this->getGivenName();
155
    }
156
157
    /**
158
     * @return string sn
159
     */
160
    public function getSurname()
161
    {
162
        return $this->getAttribute('Shib-Person-surname');
163
    }
164
165
    /**
166
     * Alias for sn
167
     *
168
     * @return string sn
169
     */
170
    public function getLastName()
171
    {
172
        return $this->getSurname();
173
    }
174
175
    /**
176
     * Alias for cn, fallback to uid
177
     *
178
     * @return string cn|uid
179
     */
180
    public function getDisplayName()
181
    {
182
        return ($this->hasAttribute('Shib-Person-commonName')) ? $this->getCommonName() : $this->getUID();
183
    }
184
185
    /**
186
     * @return string mail
187
     */
188
    public function getMail()
189
    {
190
        return $this->getAttribute('Shib-Person-mail');
191
    }
192
193
    /**
194
     * Alias for mail
195
     *
196
     * @return string mail
197
     */
198
    public function getEmail()
199
    {
200
        return $this->getMail();
201
    }
202
203
    /**
204
     * @return string mail
205
     */
206
    public function getMails()
207
    {
208
        return $this->getArrayAttribute('Shib-Person-mail');
209
    }
210
211
    /**
212
     * @return string affiliation
213
     */
214
    public function getAffiliation()
215
    {
216
        return $this->getAttribute('Shib-EP-UnscopedAffiliation');
217
    }
218
219
    /**
220
     * @return string scopedAffiliation
221
     */
222
    public function getScopedAffiliation()
223
    {
224
        return $this->getAttribute('Shib-EP-ScopedAffiliation');
225
    }
226
227
    /**
228
     * @param null|string $value
229
     * @return bool
230
     */
231
    public function hasAffiliation($value = null)
232
    {
233
        return $this->hasAttributeValue('Shib-EP-UnscopedAffiliation', $value);
234
    }
235
236
    /**
237
     * @param null|string $value
238
     * @return bool
239
     */
240
    public function hasScopedAffiliation($value = null)
241
    {
242
        return $this->hasAttributeValue('Shib-EP-ScopedAffiliation', $value);
243
    }
244
245
    /**
246
     * @param null|string $scope
247
     * @return bool
248
     */
249
    public function isMember($scope = null)
250
    {
251
        return (empty($scope) ? $this->hasAffiliation('member') : $this->hasScopedAffiliation('member@' . $scope));
252
    }
253
254
    /**
255
     * @param null|string $scope
256
     * @return bool
257
     */
258
    public function isEmployee($scope = null)
259
    {
260
        return (empty($scope) ? $this->hasAffiliation('employee') : $this->hasScopedAffiliation('employee@' . $scope));
261
    }
262
263
    /**
264
     * @param null|string $scope
265
     * @return bool
266
     */
267
    public function isStudent($scope = null)
268
    {
269
        return (empty($scope) ? $this->hasAffiliation('student') : $this->hasScopedAffiliation('student@' . $scope));
270
    }
271
272
    /**
273
     * @param null|string $scope
274
     * @return bool
275
     */
276
    public function isStaff($scope = null)
277
    {
278
        return (empty($scope) ? $this->hasAffiliation('staff') : $this->hasScopedAffiliation('staff@' . $scope));
279
    }
280
281
    /**
282
     * @param null|string $scope
283
     * @return bool
284
     */
285
    public function isFaculty($scope = null)
286
    {
287
        return (empty($scope) ? $this->hasAffiliation('faculty') : $this->hasScopedAffiliation('faculty@' . $scope));
288
    }
289
290
    /**
291
     * @return string logoutURL
292
     */
293
    public function getLogoutURL()
294
    {
295
        return $this->getAttribute('Shib-logoutURL');
296
    }
297
}
298