AttributesAwareMethods::getAttribute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: fsilva
5
 * Date: 05-02-2016
6
 * Time: 14:30
7
 */
8
9
namespace Slick\Form\Element;
10
11
use Slick\Form\ElementInterface;
12
use Slick\Form\Utils\AttributesMap;
13
use Slick\Form\Utils\AttributesMapInterface;
14
15
/**
16
 * Attributes Aware Methods
17
 *
18
 * @package Slick\Form\Element
19
 * @author  Filipe Silva <[email protected]>
20
 */
21
trait AttributesAwareMethods
22
{
23
24
    /**
25
     * @var AttributesMapInterface
26
     */
27
    protected $attributes;
28
29
    /**
30
     * Set an HTML tag attribute
31
     *
32
     * @param string $name
33
     * @param string $value
34
     *
35
     * @return self|$this|ElementInterface
36
     */
37 88
    public function setAttribute($name, $value = null)
38
    {
39 88
        $this->getAttributes()->set($name, $value);
40 88
        return $this;
41
    }
42
43
    /**
44
     * Gets the value of the attribute with the provided name
45
     *
46
     * If there is no attribute with provided name, the default value
47
     * SHOULD be returned.
48
     *
49
     * @param string      $name
50
     * @param null|string $default
51
     *
52
     * @return null|string
53
     */
54 84
    public function getAttribute($name, $default = null)
55
    {
56 84
        $value = $default;
57 84
        if ($this->hasAttribute($name)) {
58 82
            $value = $this->attributes->get($name);
59 82
        }
60 84
        return $value;
61
    }
62
63
    /**
64
     * Check if the provided attribute is set/exists
65
     *
66
     * @param string $name
67
     *
68
     * @return boolean True if attribute with name exists, false otherwise
69
     */
70 88
    public function hasAttribute($name)
71
    {
72 88
        return $this->getAttributes()
73 88
            ->containsKey($name);
74
    }
75
76
    /**
77
     * Get the attributes map collection
78
     *
79
     * @return AttributesMapInterface
80
     */
81 116
    public function getAttributes()
82
    {
83 116
        if (null === $this->attributes) {
84 110
            $this->setAttributes(new AttributesMap());
85 110
        }
86 116
        return $this->attributes;
87
    }
88
89
    /**
90
     * Sets the attribute map collection
91
     *
92
     * @param AttributesMapInterface $attributes
93
     *
94
     * @return self|$this|ElementInterface
95
     */
96 116
    public function setAttributes(AttributesMapInterface $attributes)
97
    {
98 116
        $this->attributes = $attributes;
99 116
        return $this;
100
    }
101
}