Completed
Push — master ( 442119...4a1793 )
by wen
02:50
created

Form::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
106
            $this->getMessage(),
0 ignored issues
show
Bug introduced by
The method getMessage() does not seem to exist on object<Sco\Admin\Form\Form>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
            $this->getCustomAttributes()
0 ignored issues
show
Bug introduced by
The method getCustomAttributes() does not seem to exist on object<Sco\Admin\Form\Form>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
        );
109
110
        return $this;
111
    }
112
113
    public function save()
114
    {
115
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function getValues()
122
    {
123
        return $this->elements->mapWithKeys(function (ElementInterface $element) {
124
            return [$element->getName() => $element->getValue()];
125
        });
126
    }
127
128
    public function toArray()
129
    {
130
        return [
131
            'elements' => $this->getElements(),
132
            'values'   => $this->getValues(),
133
        ];
134
    }
135
136
    public function jsonSerialize()
137
    {
138
        return $this->toArray();
139
    }
140
141
    public function toJson($options = 0)
142
    {
143
        return json_encode($this->jsonSerialize(), $options);
144
    }
145
146
    public function __toString()
147
    {
148
        return $this->toJson();
149
    }
150
}
151