Completed
Push — master ( 393880...0cf65c )
by wen
03:11
created

Form::setElementModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Sco\Admin\Form;
4
5
use Illuminate\Contracts\Support\Arrayable;
6
use Illuminate\Contracts\Support\Jsonable;
7
use Illuminate\Database\Eloquent\Model;
8
use JsonSerializable;
9
use Sco\Admin\Contracts\Form\Elements\ElementInterface;
10
use Sco\Admin\Contracts\Form\FormInterface;
11
use Sco\Admin\Contracts\Validable;
12
use Sco\Admin\Contracts\WithModel;
13
use Validator;
14
15
class Form implements
16
    FormInterface,
17
    WithModel,
18
    Jsonable,
19
    Arrayable,
20
    JsonSerializable,
21
    Validable
22
{
23
    /**
24
     * @var \Sco\Admin\Form\ElementsCollection
25
     */
26
    protected $elements;
27
28
    /**
29
     * @var \Illuminate\Database\Eloquent\Model
30
     */
31
    protected $model;
32
33
    public function __construct(array $elements = [])
34
    {
35
        $this->setElements($elements);
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getElements()
42
    {
43
        return $this->elements;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function setElements(array $elements)
50
    {
51
        $this->elements = new ElementsCollection($elements);
52
53
        return $this;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function addElement(ElementInterface $element)
60
    {
61
        $this->elements->push($element);
62
63
        return $this;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getModel()
70
    {
71
        return $this->model;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function setModel(Model $model)
78
    {
79
        $this->model = $model;
80
81
        $this->setElementModel($model);
82
83
        return $this;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function setElementModel(Model $model)
90
    {
91
        $this->elements->each(function (ElementInterface $element) use ($model) {
92
            //if ($element instanceof WithModel) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
93
            $element->setModel($model);
94
            //}
95
        });
96
97
        return $this;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function validate(array $data = [])
104
    {
105
        $data = empty($data) ? request()->all() : $data;
106
        Validator::validate(
107
            $data,
108
            $this->getValidationRules(),
109
            $this->getValidationMessages(),
110
            $this->getValidationTitles()
111
        );
112
113
        return $this;
114
    }
115
116
    public function getValidationRules()
117
    {
118
        return $this->getElementsValidationRules();
119
    }
120
121
    public function getValidationMessages()
122
    {
123
        return $this->getElementsValidationMessages();
124
    }
125
126
    public function getValidationTitles()
127
    {
128
        return $this->getElementsValidationTitles();
129
    }
130
131
    protected function getElementsValidationRules()
132
    {
133
        $rules = [];
134
        $this->elements->each(function (ElementInterface $element) use (&$rules) {
135
            if ($element instanceof Validable) {
136
                $rules += $element->getValidationRules();
137
            }
138
        });
139
        return $rules;
140
    }
141
142
    protected function getElementsValidationMessages()
143
    {
144
        $messages = [];
145
        $this->elements->each(function (ElementInterface $element) use (&$messages) {
146
            if ($element instanceof Validable) {
147
                $messages += $element->getValidationMessages();
148
            }
149
        });
150
        return $messages;
151
    }
152
153
    protected function getElementsValidationTitles()
154
    {
155
        $titles = [];
156
        $this->elements->each(function (ElementInterface $element) use (&$titles) {
157
            if ($element instanceof Validable) {
158
                $titles += $element->getValidationTitles();
159
            }
160
        });
161
        return $titles;
162
    }
163
164
    public function save()
165
    {
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171
    public function getValues()
172
    {
173
        return $this->elements->mapWithKeys(function (ElementInterface $element) {
174
            return [$element->getName() => $element->getValue()];
175
        });
176
    }
177
178
    public function toArray()
179
    {
180
        return [
181
            'elements' => $this->getElements(),
182
            'values'   => $this->getValues(),
183
        ];
184
    }
185
186
    public function jsonSerialize()
187
    {
188
        return $this->toArray();
189
    }
190
191
    public function toJson($options = 0)
192
    {
193
        return json_encode($this->jsonSerialize(), $options);
194
    }
195
196
    public function __toString()
197
    {
198
        return $this->toJson();
199
    }
200
}
201