Passed
Push — master ( 81fc17...00e7b9 )
by Bruno
08:46
created

Field::toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 $extensions;
23
24
    /**
25
     * @var array
26
     */
27
    protected $validators;
28
29 17
    public static function getFromData(string $name, array $data) : Field
30
    {
31 17
        if (!$name) {
32 1
            throw new Exception("Missing name in fields");
33
        }
34 16
        if (!array_key_exists('datatype', $data)) {
35 1
            throw new Exception("Missing type in field data for $name");
36
        }
37 15
        return new Field($name, $data['datatype'], $data['extensions'] ?? [], $data['validators'] ?? []);
38
    }
39
40
    /**
41
     * @param string $name
42
     * @param string|Datatype $datatype
43
     * @param array $extensions
44
     * @param array $validators
45
     */
46
    public function __construct(string $name, $datatype, array $extensions = [], array $validators = [])
47
    {
48 135
        $this->name = $name;
49
        if ($datatype instanceof Datatype) {
50 135
            $this->datatype = $datatype;
51 135
        } else {
52 120
            $this->datatype = Datatype::factory($datatype);
53
        }
54 15
        $this->extensions = $extensions;
55
        $this->validators = $validators;
56 133
        foreach ($this->validators as $name => $data) {
57 133
            if (!is_array($data)) {
58 133
                throw new Exception("Validator data for $name must be an array");
59
            }
60 5
        }
61
    }
62 5
63
    /**
64
     * @param string $name
65 11
     * @param string|Datatype $datatype
66
     * @param array $extensions
67 11
     * @param array $validators
68
     * @return self
69
     */
70 56
    public function create(string $name, $datatype, array $extensions = [], array $validators = []): self
71
    {
72 56
        return new self($name, $datatype, $extensions, $validators);
73
    }
74
75
    public function getName(): string
76
    {
77
        return $this->name;
78
    }
79
80 7
    public function getDatatype(): Datatype
81
    {
82 7
        return $this->datatype;
83
    }
84
85 2
    public function getValidators(): array
86
    {
87 2
        return $this->validators;
88
    }
89
90
    /**
91
     * @param string $name
92
     * @param mixed $default
93
     * @return mixed
94
     */
95 1
    public function getValidator(string $name, $default = [])
96
    {
97 1
        return $this->validators[$name] ?? $default;
98
    }
99
100
    /**
101
     * @param string $name
102
     * @return array
103
     */
104
    public function getValidatorOption(string $name): array
105
    {
106
        return $this->validators[$name] ?? [];
107
    }
108
109
    public function getExtensions(): array
110
    {
111
        return $this->extensions;
112
    }
113
114
    /**
115
     * @param string $name
116
     * @param mixed $default
117
     * @return mixed
118
     */
119
    public function getExtension(string $name, $default)
120
    {
121
        return $this->extensions[$name] ?? $default;
122
    }
123
124
    public function __toString()
125
    {
126
        return [
127
            'name' => $this->name,
128
            'datatype' => $this->datatype->getName(),
129
            'validators' => $this->validators,
130
            'extensions' => $this->extensions
131
        ];
132
    }
133
}
134