Completed
Push — master ( c4ab64...474227 )
by wen
02:28
created

Form::toJson()   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
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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
14
class Form implements
15
    FormInterface,
16
    WithModel,
17
    Jsonable,
18
    Arrayable,
19
    JsonSerializable,
20
    Validable
21
{
22
    /**
23
     * @var \Sco\Admin\Form\ElementsCollection
24
     */
25
    protected $elements;
26
27
    /**
28
     * @var \Illuminate\Database\Eloquent\Model
29
     */
30
    protected $model;
31
32
    public function __construct(array $elements = [])
33
    {
34
        $this->setElements($elements);
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getElements()
41
    {
42
        return $this->elements;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function setElements(array $elements)
49
    {
50
        $this->elements = new ElementsCollection($elements);
51
52
        return $this;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function addElement(ElementInterface $element)
59
    {
60
        $this->elements->push($element);
61
62
        return $this;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getModel()
69
    {
70
        return $this->model;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function setModel(Model $model)
77
    {
78
        $this->model = $model;
79
80
        $this->setElementModel($model);
81
82
        return $this;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function setElementModel(Model $model)
89
    {
90
        $this->elements->each(function (ElementInterface $element) use ($model) {
91
            //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...
92
            $element->setModel($model);
93
            //}
94
        });
95
96
        return $this;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function validate(array $data = [])
103
    {
104
        $data = empty($data) ? request()->all() : $data;
105
        \Validator::validate(
106
            $data,
107
            $this->getValidationRules(),
108
            $this->getValidationMessages(),
109
            $this->getValidationTitles()
110
        );
111
112
        return $this;
113
    }
114
115
    public function getValidationRules()
116
    {
117
        return $this->getElementsValidationRules();
118
    }
119
120
    public function getValidationMessages()
121
    {
122
        return $this->getElementsValidationMessages();
0 ignored issues
show
Bug introduced by
The method getElementsValidationMessages() does not exist on Sco\Admin\Form\Form. Did you maybe mean getElements()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
123
    }
124
125
    public function getValidationTitles()
126
    {
127
        return $this->getElementsValidationTitles();
0 ignored issues
show
Bug introduced by
The method getElementsValidationTitles() does not exist on Sco\Admin\Form\Form. Did you maybe mean getElements()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
128
    }
129
130
    protected function getElementsValidationRules()
131
    {
132
        return $this->elements->mapWithKeys(function (ElementInterface $element) {
133
            if ($element instanceof Validable) {
134
                return $element->getValidationRules();
135
            }
136
        });
137
    }
138
139
    public function save()
140
    {
141
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function getValues()
148
    {
149
        return $this->elements->mapWithKeys(function (ElementInterface $element) {
150
            return [$element->getName() => $element->getValue()];
151
        });
152
    }
153
154
    public function toArray()
155
    {
156
        return [
157
            'elements' => $this->getElements(),
158
            'values'   => $this->getValues(),
159
        ];
160
    }
161
162
    public function jsonSerialize()
163
    {
164
        return $this->toArray();
165
    }
166
167
    public function toJson($options = 0)
168
    {
169
        return json_encode($this->jsonSerialize(), $options);
170
    }
171
172
    public function __toString()
173
    {
174
        return $this->toJson();
175
    }
176
}
177