Passed
Push — master ( 8132a2...4e233c )
by Bruno
07:18
created

Renderable::getVueCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
namespace Formularium\Frontend\Vuelidate;
4
5
use Formularium\Datatype;
6
use Formularium\Extradata;
7
use Formularium\Field;
8
use Formularium\Frontend\Vue\Framework as FrameworkVue;
9
use Formularium\Frontend\Vue\VueCode;
10
use Formularium\HTMLNode;
11
use Formularium\Validator\Equals;
12
use Formularium\Validator\Filled;
13
use Formularium\Validator\In;
14
use Formularium\Validator\Max;
15
use Formularium\Validator\MaxLength;
16
use Formularium\Validator\Min;
17
use Formularium\Validator\MinLength;
18
use Formularium\Validator\NotIn;
19
use Formularium\Validator\Password;
20
use Formularium\Validator\Regex;
21
use Formularium\Validator\SameAs;
22
23
class Renderable extends \Formularium\Renderable
24
{
25
    public function viewable($value, Field $field, HTMLNode $previous): HTMLNode
26
    {
27
        return $previous;
28
    }
29
30
    public function editable($value, Field $field, HTMLNode $previous): HTMLNode
31
    {
32
        $validators = $field->getValidators();
33
34
35
        foreach ($validators as $validator => $data) {
36
            switch ($validator) {
37
            case Datatype::REQUIRED:
38
            case Filled::class:
39
                $this->setValidations(
40
                    $field,
41
                    'required',
42
                    'required'
43
                );
44
                break;
45
            case Equals::class:
46
                // TODO
47
                break;
48
49
            case In::class:
50
                // TODO
51
                break;
52
53
            case Max::class:
54
                $this->setValidations(
55
                    $field,
56
                    'maxValue',
57
                    'maxValue(' . $field->getValidatorOption($validator, 'value', '') . ')'
58
                );
59
                break;
60
            case Min::class:
61
                $this->setValidations(
62
                    $field,
63
                    'minValue',
64
                    'minValue(' . $field->getValidatorOption($validator, 'value', '') . ')'
65
                );
66
                break;
67
68
            case MaxLength::class:
69
                $this->setValidations(
70
                    $field,
71
                    'maxLength',
72
                    'maxLength(' . $field->getValidatorOption($validator, 'value', '') . ')'
73
                );
74
                break;
75
            case MinLength::class:
76
                $this->setValidations(
77
                    $field,
78
                    'minLength',
79
                    'minLength(' . $field->getValidatorOption($validator, 'value', '') . ')'
80
                );
81
                break;
82
83
            case NotIn::class:
84
                // TODO
85
                break;
86
87
            case Password::class:
88
                // TODO
89
                break;
90
91
            case Regex::class:
92
                $name = 'regex' . mt_rand();
93
                $this->setValidations(
94
                    $field,
95
                    $name,
96
                    'helpers.regex(\'' . $name . '\', /' . $field->getValidatorOption($validator, 'value', '') . '/)',
97
                    'helpers'
98
                );
99
                break;
100
                        
101
            case SameAs::class:
102
                $target = $field->getValidatorOption($validator, 'value', '');
103
                $locator = $target;// TODO
104
                $this->setValidations(
105
                    $field,
106
                    'sameAs',
107
                    'sameAs(' . $locator . ')'
108
                );
109
                break;
110
            default:
111
                break;
112
            }
113
        }
114
115
        return $previous;
116
    }
117
118
    protected function getVueCode(): VueCode
119
    {
120
        /**
121
         * @var FrameworkVue $vue
122
         */
123
        $vue = $this->composer->getByName('Vue');
124
        return $vue->getVueCode();
125
    }
126
127
    /**
128
     * Sets validation
129
     *
130
     * @param Field $field
131
     * @param string $name
132
     * @param string $value
133
     * @param string $import
134
     * @return void
135
     */
136
    protected function setValidations(Field $field, $name, $value, $import = ''): void
137
    {
138
        $vueCode = $this->getVueCode();
139
        $vueCode->appendImport($import ? $import : $name, 'vuelidate/lib/validators');
140
        $other = &$vueCode->getOther();
141
        $other['validations']['form'][$field->getName()][$name] = $value;
142
    }
143
}
144