Completed
Branch master (8e0976)
by Adam
04:13
created

Rules::makeRules()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 13
nop 2
dl 0
loc 30
rs 8.8177
c 0
b 0
f 0
1
<?php
2
3
namespace Coyote\Services\FormBuilder;
4
5
use Coyote\Services\FormBuilder\Fields\ChildForm;
6
use Coyote\Services\FormBuilder\Fields\Collection;
7
use Coyote\Services\FormBuilder\Fields\Field;
8
use Coyote\Services\FormBuilder\Fields\ParentType;
9
10
class Rules
11
{
12
    /**
13
     * @var FormInterface
14
     */
15
    protected $form;
16
17
    /**
18
     * @param FormInterface $form
19
     */
20
    public function __construct(FormInterface $form)
21
    {
22
        $this->form = $form;
23
    }
24
25
    /**
26
     * @return array
27
     */
28
    public function getRules()
29
    {
30
        return $this->makeRules($this->form, $this->form->getFields());
31
    }
32
33
    /**
34
     * @param FormInterface|Field $context
35
     * @param Field[] $fields
36
     * @return array
37
     */
38
    protected function makeRules($context, array $fields)
39
    {
40
        $result = [];
41
42
        foreach ($fields as $field) {
43
            $rules = $field->getRules();
44
45
            if ($field->isRequired()) {
46
                $rules = explode('|', $rules);
47
48
                if (!in_array('required', $rules)) {
49
                    $rules = array_prepend($rules, 'required');
50
                }
51
52
                $rules = implode('|', $rules);
53
            }
54
55
            if ($rules) {
56
                $result[$this->transformNameToRule($context, $field)] = $rules;
57
            }
58
59
            // @todo: moze da sie to zrobic jakos lepiej. byc moze przeniesc ten kod do metody getRules()
60
            // klas dziedziczacych po Field?
61
            if ($field instanceof ParentType) {
62
                $result = array_merge($result, $this->makeRules($field, $field->getChildren()));
63
            }
64
        }
65
66
        return $result;
67
    }
68
69
    /**
70
     * Prepare laravel's rule name. Transforms string like tags[0] to tags.*
71
     *
72
     * @param FormInterface|Field $context
73
     * @param Field $field
74
     * @return string
75
     */
76
    protected function transformNameToRule($context, Field $field)
77
    {
78
        if ($context instanceof ChildForm) {
79
            return $this->transformNestedName($field->getName());
80
        } elseif ($context instanceof Collection) {
81
            return $this->transformArrayName($field->getName());
82
        } else {
83
            return $field->getName();
84
        }
85
    }
86
87
    /**
88
     * @param string $name
89
     * @return string
90
     */
91
    protected function transformArrayName($name)
92
    {
93
        return trim(str_replace(['[', ']'], '', preg_replace('/\[[0-9]+\]/', '.*.', $name)), '.');
94
    }
95
96
    /**
97
     * @param string $name
98
     * @return string
99
     */
100
    protected function transformNestedName($name)
101
    {
102
        return preg_replace('/\.[0-9]+\./', '.*.', str_replace(['[', ']'], ['.', ''], $name));
103
    }
104
}
105