HasDataTrait::getData()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
namespace Sirius\Input\Traits;
3
4
use Sirius\Input\Specs;
5
use Sirius\Input\DataContainer;
6
7
trait HasDataTrait
8
{
9
10 2
    protected function ensureData()
11
    {
12 2
        if (!isset($this[Specs::DATA])) {
13 2
            $this[Specs::DATA] = new DataContainer();
14 2
        }
15 2
    }
16
17
    /**
18
     * Retrieve an attribute from the label
19
     *
20
     * @param string $key
21
     *
22
     * @return mixed
23
     */
24 2
    public function getData($key = null)
25
    {
26 2
        $this->ensureData();
27 2
        if ($key === null) {
28 1
            return $this[Specs::DATA]->getAll();
29
        }
30
31 2
        return $this[Specs::DATA]->get($key);
32
    }
33
34
    /**
35
     * Set/Unset a label attribute
36
     *
37
     * @param string $keyOrArray
38
     * @param mixed|null $value
39
     *
40
     * @return self
41
     */
42 2
    public function setData($keyOrArray, $value = null)
43
    {
44 2
        $this->ensureData();
45 2
        $this[Specs::DATA]->set($keyOrArray, $value);
46
47 2
        return $this;
48
    }
49
50
}