Passed
Push — master ( 16937d...cbfe05 )
by Bruno
06:42
created

Field::getCodeGeneratorFieldDeclaration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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