HasDataTrait::setData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 1
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
}