Resource::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Bouncer package.
5
 *
6
 * (c) François Hodierne <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Bouncer;
13
14
class Resource
15
{
16
17
    protected $attributes = array();
18
19
    public function __construct($attributes = null)
20
    {
21
        if (!empty($attributes)) {
22
            $this->setAttributes($attributes);
23
        }
24
    }
25
26
    public function setAttributes($attributes = array())
27
    {
28
        foreach ($attributes as $key => $value) {
29
            $this->setAttribute($key, $value);
30
        }
31
    }
32
33
    public function setAttribute($key, $value)
34
    {
35
        $this->attributes[$key] = $value;
36
        $setMethod = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
37
        if (method_exists($this, $setMethod)) {
38
            $this->$setMethod($value);
39
        }
40
    }
41
42
    public function hasAttribute($key)
43
    {
44
        return array_key_exists($key, $this->attributes);
45
    }
46
47
    public function getAttribute($name)
48
    {
49
        if (isset($this->attributes[$name])) {
50
            return $this->attributes[$name];
51
        }
52
    }
53
54
}
55