Completed
Push — master ( ee0bf4...f89977 )
by Hannes
03:46 queued 02:03
created

AttributesTrait::getAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace byrokrat\billing;
6
7
/**
8
 * Standard implementation of setAttribute and getAttribute
9
 */
10
trait AttributesTrait
11
{
12
    /**
13
     * @var array Loaded attributes
14
     */
15
    private $attributes = [];
16
17
    /**
18
     * Set attribute defined by key
19
     */
20 2
    public function setAttribute(string $key, $value): self
21
    {
22 2
        $this->attributes[$key] = $value;
23 2
        return $this;
24
    }
25
26
    /**
27
     * Get attribute definied by key, returning default if attribute is not set
28
     *
29
     * @return mixed
30
     */
31 2
    public function getAttribute(string $key, $default = '')
32
    {
33 2
        return $this->attributes[$key] ?? $default;
34
    }
35
36
    /**
37
     * Get all loaded attributes
38
     */
39 6
    public function getAttributes(): array
40
    {
41 6
        return $this->attributes;
42
    }
43
44
    /**
45
     * Clear all attributes
46
     */
47 9
    public function clearAttributes(): self
48
    {
49 9
        $this->attributes = [];
50 9
        return $this;
51
    }
52
}
53