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

AttributesTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 43
ccs 10
cts 10
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setAttribute() 0 5 1
A getAttribute() 0 4 1
A getAttributes() 0 4 1
A clearAttributes() 0 5 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