HasAttributes   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 3 1
A verifyAttributes() 0 10 3
1
<?php
2
3
namespace TPG\Pcflib\Traits;
4
5
use TPG\Pcflib\Exceptions\MissingRequiredAttribute;
6
7
/**
8
 * Trait HasAttributes
9
 * @package TPG\Pcflib\Traits
10
 */
11
trait HasAttributes
12
{
13
    /**
14
     * @var array
15
     */
16
    protected $attributes = [];
17
18
    /**
19
     * @var array
20
     */
21
    protected $requiredAttributes = [];
22
23
    /**
24
     * @var null|string
25
     */
26
    protected $rootKey = null;
27
28
    /**
29
     * Verify the current attributes
30
     *
31
     * @return bool
32
     * @throws MissingRequiredAttribute
33
     */
34
    public function verifyAttributes()
35
    {
36
        foreach ($this->requiredAttributes as $required) {
37
38
            if (!in_array($required, array_keys($this->attributes))) {
39
40
                throw new MissingRequiredAttribute($required);
41
            }
42
        }
43
        return true;
44
    }
45
46
    /**
47
     * Output an array
48
     *
49
     * @return array
50
     */
51
    public function toArray(): array
52
    {
53
        return $this->attributes;
54
    }
55
}