Passed
Branch master (3daac1)
by Vincent
07:53
created

RenderableTrait::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Bdf\Form\View;
4
5
use ArgumentCountError;
6
use TypeError;
7
8
/**
9
 * Implements @see Renderable
10
 */
11
trait RenderableTrait
12
{
13
    /**
14
     * @var array
15
     */
16
    private $attributes = [];
17
18
    /**
19
     * {@inheritdoc}
20
     */
21 28
    public function __call(string $name, array $arguments)
22
    {
23 28
        if (empty($arguments)) {
24 2
            throw new ArgumentCountError('Missing the attribute value.');
25
        }
26
27 26
        return $this->set($name, $arguments[0]);
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 28
    public function set(string $name, $value)
34
    {
35 28
        if (!is_scalar($value)) {
36 2
            throw new TypeError('The attribute value must be a scalar value.');
37
        }
38
39 26
        $this->attributes[$name] = $value;
40
41 26
        return $this;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 4
    public function unset(string $name)
48
    {
49 4
        unset($this->attributes[$name]);
50
51 4
        return $this;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 9
    public function attributes(): array
58
    {
59 9
        return $this->attributes;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    abstract public function render(): string;
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 36
    public function __toString(): string
71
    {
72 36
        return $this->render();
73
    }
74
}
75