Passed
Push — master ( fd0391...6bafc6 )
by Bruno
04:22
created

ExtradataTrait::removeExtraData()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
ccs 0
cts 6
cp 0
rs 10
cc 3
nc 3
nop 1
crap 12
1
<?php declare(strict_types=1);
2
3
namespace Formularium;
4
5
trait ExtradataTrait
6
{
7
    /**
8
     * @var Extradata[]
9
     */
10
    protected $extradata = [];
11
12
    public function setExtradata(array $data): self
13
    {
14
        $this->extradata = [];
15
        foreach ($data as $d) {
16
            $this->extradata[] = ($d instanceof Extradata) ? $d : Extradata::getFromData(null, $d);
17
        }
18
        return $this;
19
    }
20
21
    /**
22
     * All extradata objects. Yeah, that's a horrible plural, I know.
23
     *
24
     * @return Extradata[]
25
     */
26
    public function getExtradatas(): array
27
    {
28
        return $this->extradata;
29
    }
30
31 1
    public function getExtradataSerialize(): array
32
    {
33 1
        return array_map(
34
            function (Extradata $e) {
35
                return [
36 1
                    'name' => $e->getName(),
37 1
                    'args' => array_map(
38
                        function (ExtradataParameter $a) {
39
                            return [
40 1
                                'name' => $a->name,
41 1
                                'value' => $a->value
42
                            ];
43 1
                        },
44 1
                        $e->args
45
                    )
46
                ];
47 1
            },
48 1
            $this->extradata
49
        );
50
    }
51
52
    /**
53
     * @param string $name
54
     * @return Extradata|null
55
     */
56 1
    public function getExtradata(string $name): ?Extradata
57
    {
58 1
        foreach ($this->extradata as $m) {
59 1
            if ($m->getName() === $name) {
60 1
                return $m;
61
            }
62
        }
63 1
        return null;
64
    }
65
66
    /**
67
     * @param string $name
68
     * @param string $parameter
69
     * @param Mixed $default
70
     * @return Mixed
71
     */
72 1
    public function getExtradataValue(string $name, string $parameter, $default = null)
73
    {
74 1
        $e = $this->getExtradata($name);
75 1
        if (!$e) {
76 1
            return $default;
77
        }
78 1
        return $e->value($parameter, $default);
79
    }
80
81
    public function appendExtradata(Extradata $m): self
82
    {
83
        $this->extradata[] = $m;
84
        return $this;
85
    }
86
87
    public function removeExtraData(string $name): self
88
    {
89
        foreach ($this->extradata as $k => $m) {
90
            if ($m->getName() === $name) {
91
                unset($this->extradata[$k]);
92
                break;
93
            }
94
        }
95
        return $this;
96
    }
97
}
98