Completed
Push — master ( 4a1793...973992 )
by wen
05:18
created

Form::getValidationMessages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

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 1
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
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
    protected $validationRules = [];
33
34
    protected $validationMessages = [];
35
36
    public function __construct(array $elements = [])
37
    {
38
        $this->setElements($elements);
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getElements()
45
    {
46
        return $this->elements;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function setElements(array $elements)
53
    {
54
        $this->elements = new ElementsCollection($elements);
55
56
        return $this;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function addElement(ElementInterface $element)
63
    {
64
        $this->elements->push($element);
65
66
        return $this;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getModel()
73
    {
74
        return $this->model;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function setModel(Model $model)
81
    {
82
        $this->model = $model;
83
84
        $this->setElementModel($model);
85
86
        return $this;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function setElementModel(Model $model)
93
    {
94
        $this->elements->each(function (ElementInterface $element) use ($model) {
95
            //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...
96
            $element->setModel($model);
97
            //}
98
        });
99
100
        return $this;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function validate(array $data = [])
107
    {
108
        $data = empty($data) ? request()->all() : $data;
109
        \Validator::validate(
110
            $data,
111
            $this->getValidationRules(),
112
            $this->getValidationMessages(),
113
            $this->getValidationTitles()
114
        );
115
116
        return $this;
117
    }
118
119
    public function getValidationRules()
120
    {
121
122
    }
123
124
    public function getValidationMessages()
125
    {
126
127
    }
128
129
    public function getValidationTitles()
130
    {
131
132
    }
133
134
    /*public function addValidationRule($rule, $message = null)
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% 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...
135
    {
136
        $this->validationRules[] = $rule;
137
138
        if (is_null($message)) {
139
            return $this;
140
        }
141
142
        return $this->addValidationMessage($message);
143
    }*/
144
145
    public function save()
146
    {
147
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function getValues()
154
    {
155
        return $this->elements->mapWithKeys(function (ElementInterface $element) {
156
            return [$element->getName() => $element->getValue()];
157
        });
158
    }
159
160
    public function toArray()
161
    {
162
        return [
163
            'elements' => $this->getElements(),
164
            'values'   => $this->getValues(),
165
        ];
166
    }
167
168
    public function jsonSerialize()
169
    {
170
        return $this->toArray();
171
    }
172
173
    public function toJson($options = 0)
174
    {
175
        return json_encode($this->jsonSerialize(), $options);
176
    }
177
178
    public function __toString()
179
    {
180
        return $this->toJson();
181
    }
182
}
183