Completed
Push — master ( 0cf65c...1773ee )
by wen
02:56
created

Form::saveElements()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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
107
        Validator::validate(
108
            $data,
109
            $this->getValidationRules(),
110
            $this->getValidationMessages(),
111
            $this->getValidationTitles()
112
        );
113
114
        return $this;
115
    }
116
117
    public function getValidationRules()
118
    {
119
        return $this->getElementsValidationRules();
120
    }
121
122
    public function getValidationMessages()
123
    {
124
        return array_merge(
125
            trans('admin::validation'),
126
            $this->getElementsValidationMessages()
127
        );
128
    }
129
130
    public function getValidationTitles()
131
    {
132
        return $this->getElementsValidationTitles();
133
    }
134
135
    protected function getElementsValidationRules()
136
    {
137
        $rules = [];
138
        $this->elements->each(function (ElementInterface $element) use (&$rules) {
139
            if ($element instanceof Validable) {
140
                $rules += $element->getValidationRules();
141
            }
142
        });
143
        return $rules;
144
    }
145
146
    protected function getElementsValidationMessages()
147
    {
148
        $messages = [];
149
        $this->elements->each(function (ElementInterface $element) use (&$messages) {
150
            if ($element instanceof Validable) {
151
                $messages += $element->getValidationMessages();
152
            }
153
        });
154
        return $messages;
155
    }
156
157
    protected function getElementsValidationTitles()
158
    {
159
        $titles = [];
160
        $this->elements->each(function (ElementInterface $element) use (&$titles) {
161
            if ($element instanceof Validable) {
162
                $titles += $element->getValidationTitles();
163
            }
164
        });
165
        return $titles;
166
    }
167
168
    protected function saveElements()
169
    {
170
        $this->getElements()->each(function (ElementInterface $element) {
171
            $element->save(request());
172
        });
173
    }
174
175
    public function save()
176
    {
177
        $this->saveElements();
178
179
        $model = $this->getModel();
180
        $model->save();
181
182
        return true;
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188
    public function getValues()
189
    {
190
        return $this->elements->mapWithKeys(function (ElementInterface $element) {
191
            return [$element->getName() => $element->getValue()];
192
        });
193
    }
194
195
    public function toArray()
196
    {
197
        return [
198
            'elements' => $this->getElements(),
199
            'values'   => $this->getValues(),
200
        ];
201
    }
202
203
    public function jsonSerialize()
204
    {
205
        return $this->toArray();
206
    }
207
208
    public function toJson($options = 0)
209
    {
210
        return json_encode($this->jsonSerialize(), $options);
211
    }
212
213
    public function __toString()
214
    {
215
        return $this->toJson();
216
    }
217
}
218