Completed
Push — master ( fa1799...47d055 )
by Hong
03:05
created

AttributeTrait::getAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Shared
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Shared\Attribute;
16
17
/**
18
 * AttributeTrait
19
 *
20
 * Implementation of AttributeInterface
21
 *
22
 * @package Phossa2\Shared
23
 * @author  Hong Zhang <[email protected]>
24
 * @see     AttributeInterface
25
 * @version 2.0.28
26
 * @since   2.0.28 added
27
 */
28
trait AttributeTrait
29
{
30
    use StaticVarTrait; // methods dealing with class static attributes
31
32
    /**
33
     * Instance attributes
34
     *
35
     * @var    array
36
     * @access protected
37
     */
38
    protected $attributes = [];
39
40
    /**
41
     * {@inheritDoc}
42
     */
43
    public function setAttribute(/*# string */ $attrName, $attrValue)
44
    {
45
        $this->attributes[$attrName] = $attrValue;
46
        return $this;
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    public function addAttribute(/*# string */ $attrName, $attrValue)
53
    {
54
        if (!is_array($this->attributes[$attrName])) {
55
            $this->attributes[$attrName] = (array) $this->attributes[$attrName];
56
        }
57
        $this->attributes[$attrName][] = $attrValue;
58
        return $this;
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64
    public function getAttribute(/*# string */ $attrName)
65
    {
66
        if (isset($this->attributes[(string) $attrName])) {
67
            return $this->attributes[(string) $attrName];
68
        }
69
        return null;
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75
    public function setAttributes(array $attributes)
76
    {
77
        $this->attributes = $attributes;
78
        return $this;
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84
    public function getAttributes()/*# : array */
85
    {
86
        return $this->attributes;
87
    }
88
}
89