Passed
Push — master ( 116340...b15bec )
by Bruno
07:47
created

Field::setValidatorOption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 1
cts 1
cp 1
crap 1
1
<?php declare(strict_types=1);
2
3
namespace Formularium;
4
5
use Formularium\Exception\Exception;
6
7
class Field
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $name;
13
14
    /**
15
     * @var Datatype
16
     */
17
    protected $datatype;
18
19
    /**
20
     * @var array
21
     */
22
    protected $renderable;
23
24
    /**
25
     * @var array
26
     */
27
    protected $validators;
28
29 31
    public static function getFromData(string $name, array $data) : Field
30
    {
31 31
        if (!$name) {
32 1
            throw new Exception("Missing name in fields");
33
        }
34 30
        if (!array_key_exists('datatype', $data)) {
35 1
            throw new Exception("Missing type in field data for $name");
36
        }
37 29
        return new Field($name, $data['datatype'], $data['renderable'] ?? [], $data['validators'] ?? []);
38
    }
39
40
    /**
41
     * @param string $name
42
     * @param string|Datatype $datatype
43
     * @param array $renderable
44
     * @param array $validators
45
     */
46 157
    public function __construct(string $name, $datatype, array $renderable = [], array $validators = [])
47
    {
48 157
        $this->name = $name;
49 157
        if ($datatype instanceof Datatype) {
50 128
            $this->datatype = $datatype;
51
        } else {
52 29
            $this->datatype = DatatypeFactory::factory($datatype);
53
        }
54 155
        $this->renderable = $renderable;
55 155
        $this->validators = $validators;
56 155
        foreach ($this->validators as $name => $data) {
57 27
            if (!is_array($data)) {
58
                throw new Exception("Validator data for $name must be an array");
59
            }
60
        }
61 155
    }
62
63
    /**
64
     * @param string $name
65
     * @param string|Datatype $datatype
66
     * @param array $renderable
67
     * @param array $validators
68
     * @return self
69
     */
70
    public function create(string $name, $datatype, array $renderable = [], array $validators = []): self
71
    {
72
        return new self($name, $datatype, $renderable, $validators);
73
    }
74
75 2
    public function getName(): string
76
    {
77 2
        return $this->name;
78
    }
79
80 11
    public function getDatatype(): Datatype
81
    {
82 11
        return $this->datatype;
83
    }
84
85 10
    public function getValidators(): array
86
    {
87 10
        return $this->validators;
88
    }
89
90
    /**
91
     * @param string $name
92
     * @param mixed $default
93
     * @return mixed
94
     */
95 16
    public function getValidator(string $name, $default = [])
96
    {
97 16
        return $this->validators[$name] ?? $default;
98
    }
99
100
    /**
101
     * Get option value from a validator.
102
     *
103
     * @param string $validator The validator name.
104
     * @param string $option The validation option.
105
     * @param mixed $default The default value.
106
     * @return mixed The option value or the default value if there is none.
107
     */
108
    public function getValidatorOption(string $validator, string $option = 'value', $default = null)
109
    {
110
        return $this->validators[$validator][$option] ?? $default;
111
    }
112
113 2
    /**
114
     * Sets an option value
115 2
     *
116
     * @param string $validator
117
     * @param string $option
118
     * @param mixed $value
119
     * @return self
120
     */
121
    public function setValidatorOption(string $validator, string $option, $value): self
122
    {
123 1
        $this->validators[$validator][$option] = $value;
124
        return $this;
125 1
    }
126
127
    public function getRenderables(): array
128
    {
129
        return $this->renderable;
130
    }
131
132
    /**
133
     * @param string $name
134
     * @param mixed $default
135
     * @return mixed
136
     */
137
    public function getRenderable(string $name, $default)
138
    {
139
        return $this->renderable[$name] ?? $default;
140
    }
141
142
    public function toArray(): array
143
    {
144
        return [
145
            'name' => $this->name,
146
            'datatype' => $this->datatype->getName(),
147
            'validators' => $this->validators,
148
            'renderable' => $this->renderable
149
        ];
150
    }
151
}
152