Passed
Push — master ( f1c3fb...9dda0c )
by Victor
02:00
created

PresentationModel::variableExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Shoot\Shoot;
5
6
/**
7
 * Holds the variables available to a view.
8
 */
9
class PresentationModel
10
{
11
    /**
12
     * @param mixed[] $variables
13
     */
14 16
    final public function __construct(array $variables = [])
15
    {
16 16
        $this->setVariables($variables);
17 15
    }
18
19
    /**
20
     * @return string The name of the presentation model.
21
     */
22 2
    final public function getName(): string
23
    {
24 2
        return static::class;
25
    }
26
27
    /**
28
     * @return mixed[]
29
     */
30 10
    final public function getVariables(): array
31
    {
32 10
        return get_object_vars($this);
33
    }
34
35
    /**
36
     * @param mixed[] $variables
37
     *
38
     * @return PresentationModel
39
     */
40 3
    final public function withVariables(array $variables): self
41
    {
42 3
        $new = clone $this;
43 3
        $new->setVariables($variables);
44
45 3
        return $new;
46
    }
47
48
    /**
49
     * @param mixed[] $variables
50
     *
51
     * @return void
52
     */
53 16
    private function setVariables(array $variables)
54
    {
55 16
        foreach ($variables as $variable => $value) {
56 7
            if ($this->variableExists($variable)) {
57 6
                $this->$variable = $value;
58
            }
59
        }
60 15
    }
61
62
    /**
63
     * @param string $variable
64
     *
65
     * @return bool
66
     */
67 6
    private function variableExists(string $variable): bool
68
    {
69 6
        if (static::class === self::class) {
70 1
            return true;
71
        }
72
73 5
        return property_exists($this, $variable);
74
    }
75
}
76