Passed
Push — master ( cd2ef0...b8142e )
by Bruno
07:52
created

Field   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 24
c 2
b 0
f 0
dl 0
loc 100
ccs 26
cts 26
cp 1
rs 10
wmc 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A getFromData() 0 9 3
A __construct() 0 10 2
A getValidators() 0 3 1
A getDatatype() 0 3 1
A getValidator() 0 3 1
A getValidatorOption() 0 3 1
A getExtensions() 0 3 1
A getExtension() 0 3 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 $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
     * Undocumented function
42
     *
43
     * @param string $name
44
     * @param string|Datatype $datatype
45
     * @param array $extensions
46
     * @param array $validators
47
     */
48 135
    public function __construct(string $name, $datatype, array $extensions = [], array $validators = [])
49
    {
50 135
        $this->name = $name;
51 135
        if ($datatype instanceof Datatype) {
52 120
            $this->datatype = $datatype;
53
        } else {
54 15
            $this->datatype = Datatype::factory($datatype);
55
        }
56 133
        $this->extensions = $extensions;
57 133
        $this->validators = $validators;
58 133
    }
59
60 5
    public function getName(): string
61
    {
62 5
        return $this->name;
63
    }
64
65 11
    public function getDatatype(): Datatype
66
    {
67 11
        return $this->datatype;
68
    }
69
70 56
    public function getValidators(): array
71
    {
72 56
        return $this->validators;
73
    }
74
75
    /**
76
     * @param string $name
77
     * @param mixed $default
78
     * @return mixed
79
     */
80 7
    public function getValidator(string $name, $default = [])
81
    {
82 7
        return $this->validators[$name] ?? $default;
83
    }
84
85 2
    /**
86
     * @param string $name
87 2
     * @return array
88
     */
89
    public function getValidatorOption(string $name): array
90
    {
91
        return $this->validators[$name] ?? [];
92
    }
93
94
    public function getExtensions(): array
95 1
    {
96
        return $this->extensions;
97 1
    }
98
99
    /**
100
     * @param string $name
101
     * @param mixed $default
102
     * @return mixed
103
     */
104
    public function getExtension(string $name, $default)
105
    {
106
        return $this->extensions[$name] ?? $default;
107
    }
108
}
109