Completed
Push — master ( d9e017...cb8e5c )
by wen
04:31
created

Form::getElements()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Sco\Admin\Form;
4
5
6
use Illuminate\Database\Eloquent\Model;
7
use Sco\Admin\Contracts\Form\Elements\ElementInterface;
8
use Sco\Admin\Contracts\Form\FormInterface;
9
use Sco\Admin\Contracts\Validatable;
10
use Sco\Admin\Exceptions\InvalidArgumentException;
11
use Validator;
12
13
class Form implements
14
    FormInterface,
15
    Validatable
16
{
17
    /**
18
     * @var \Sco\Admin\Form\ElementsCollection
19
     */
20
    protected $elements;
21
22
    /**
23
     * @var \Illuminate\Database\Eloquent\Model
24
     */
25
    protected $model;
26
27
    public function __construct(array $elements = [])
28
    {
29
        $this->setElements($elements);
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function getElements()
36
    {
37
        return $this->elements;
38
    }
39
40
    public function getElement($name)
41
    {
42
        $key = $this->getElements()->search(function (ElementInterface $item) use ($name
43
        ) {
44
            return $item->getName() == $name;
45
        });
46
        if ($key === false) {
47
            throw new InvalidArgumentException('Not found element');
48
        }
49
        return $this->getElements()->get($key);
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function setElements(array $elements)
56
    {
57
        $this->elements = new ElementsCollection($elements);
58
59
        return $this;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function addElement(ElementInterface $element)
66
    {
67
        $this->elements->push($element);
68
69
        return $this;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getModel()
76
    {
77
        return $this->model;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function setModel(Model $model)
84
    {
85
        $this->model = $model;
86
87
        $this->setElementModel($model);
88
89
        return $this;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function setElementModel(Model $model)
96
    {
97
        $this->elements->each(function (ElementInterface $element) use ($model) {
98
            //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...
99
            $element->setModel($model);
100
            //}
101
        });
102
103
        return $this;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function validate(array $data = [])
110
    {
111
        $data = empty($data) ? request()->all() : $data;
112
113
        Validator::validate(
114
            $data,
115
            $this->getValidationRules(),
116
            $this->getValidationMessages(),
117
            $this->getValidationTitles()
118
        );
119
120
        return $this;
121
    }
122
123
    public function getValidationRules()
124
    {
125
        return $this->getElementsValidationRules();
126
    }
127
128
    public function getValidationMessages()
129
    {
130
        return array_merge(
131
            trans('admin::validation'),
132
            $this->getElementsValidationMessages()
133
        );
134
    }
135
136
    public function getValidationTitles()
137
    {
138
        return $this->getElementsValidationTitles();
139
    }
140
141
    protected function getElementsValidationRules()
142
    {
143
        $rules = [];
144
        $this->elements->each(function (ElementInterface $element) use (&$rules) {
145
            if ($element instanceof Validatable) {
146
                $rules += $element->getValidationRules();
147
            }
148
        });
149
        return $rules;
150
    }
151
152
    protected function getElementsValidationMessages()
153
    {
154
        $messages = [];
155
        $this->elements->each(function (ElementInterface $element) use (&$messages) {
156
            if ($element instanceof Validatable) {
157
                $messages += $element->getValidationMessages();
158
            }
159
        });
160
        return $messages;
161
    }
162
163
    protected function getElementsValidationTitles()
164
    {
165
        $titles = [];
166
        $this->elements->each(function (ElementInterface $element) use (&$titles) {
167
            if ($element instanceof Validatable) {
168
                $titles += $element->getValidationTitles();
169
            }
170
        });
171
        return $titles;
172
    }
173
174
    protected function saveElements()
175
    {
176
        $this->getElements()->each(function (ElementInterface $element) {
177
            $element->save(request());
178
        });
179
    }
180
181
    public function save()
182
    {
183
        $this->saveElements();
184
185
        $model = $this->getModel();
186
        $model->save();
187
188
        return true;
189
    }
190
191
    /**
192
     * {@inheritdoc}
193
     */
194
    public function getValues()
195
    {
196
        return $this->elements->mapWithKeys(function (ElementInterface $element) {
197
            return [
198
                $element->getName() => $element->getValue(),
199
            ];
200
        });
201
    }
202
203
    public function toArray()
204
    {
205
        return [
206
            'elements' => $this->getElements(),
207
            'values'   => $this->getValues(),
208
        ];
209
    }
210
211
    public function jsonSerialize()
212
    {
213
        return $this->toArray();
214
    }
215
216
    public function toJson($options = 0)
217
    {
218
        return json_encode($this->jsonSerialize(), $options);
219
    }
220
221
    public function __toString()
222
    {
223
        return $this->toJson();
224
    }
225
}
226