Passed
Push — master ( 0f2edd...d98c91 )
by Bruno
09:06
created

Field::toArray()   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
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 2
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 39
    public static function getFromData(string $name, array $data) : Field
30
    {
31 39
        if (!$name) {
32 1
            throw new Exception("Missing name in fields");
33
        }
34 38
        if (!array_key_exists('datatype', $data)) {
35 1
            throw new Exception("Missing type in field data for $name");
36
        }
37 37
        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 165
    public function __construct(string $name, $datatype, array $renderable = [], array $validators = [])
47
    {
48 165
        $this->name = $name;
49 165
        if ($datatype instanceof Datatype) {
50 128
            $this->datatype = $datatype;
51
        } else {
52 37
            $this->datatype = DatatypeFactory::factory($datatype);
53
        }
54 163
        $this->renderable = $renderable;
55 163
        $this->validators = $validators;
56 163
        foreach ($this->validators as $name => $data) {
57 34
            if (!is_array($data)) {
58
                throw new Exception("Validator data for $name must be an array");
59
            }
60
        }
61 163
    }
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 4
    public function getName(): string
76
    {
77 4
        return $this->name;
78
    }
79
80 12
    public function getDatatype(): Datatype
81
    {
82 12
        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 23
    public function getValidator(string $name, $default = [])
96
    {
97 23
        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
    /**
114
     * Sets an option value
115
     *
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
        $this->validators[$validator][$option] = $value;
124
        return $this;
125
    }
126
127 3
    public function getRenderables(): array
128
    {
129 3
        return $this->renderable;
130
    }
131
132
    /**
133
     * @param string $name
134
     * @param mixed $default
135
     * @return mixed
136
     */
137 2
    public function getRenderable(string $name, $default)
138
    {
139 2
        return $this->renderable[$name] ?? $default;
140
    }
141
142
    public function toGraphqlQuery(): string
143
    {
144
        return $this->datatype->getGraphqlField($this->getName());
145
    }
146
147
    public function toGraphqlTypeDefinition(): string
148
    {
149
        $renderable = array_map(
150
            function ($name, $value) {
151
                $v = $value;
152
                if (is_string($value)) {
153
                    $v = '"' . str_replace('"', '\\"', $value) . '"';
154
                }
155
                return ' ' . $name . ': ' . $v;
156
            },
157
            array_keys($this->renderable),
158
            $this->renderable
159
        );
160
161
        return $this->getName() . ': ' . $this->datatype->getGraphqlType() .
162
            ($this->getValidator(Datatype::REQUIRED, false) ? '' : '!') .
163
            // TODO: validators
164
            ($this->renderable ? " @renderable(\n" . join("\n", $renderable) . "\n)" : '') .
165
            "\n";
166
    }
167
168
    public function toArray(): array
169
    {
170
        return [
171
            'name' => $this->name,
172
            'datatype' => $this->datatype->getName(),
173
            'validators' => $this->validators,
174
            'renderable' => $this->renderable
175
        ];
176
    }
177
}
178