Completed
Push — master ( 77dbd7...6a8ad9 )
by wen
26:38
created

Form::jsonSerialize()   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 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\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
    public function getValues()
98
    {
99
    }
100
101
    public function toArray()
102
    {
103
        return [
104
            'elements' => $this->getElements(),
105
            'values'   => $this->getValues(),
106
        ];
107
    }
108
109
    public function jsonSerialize()
110
    {
111
        return $this->toArray();
112
    }
113
114
    public function toJson($options = 0)
115
    {
116
        return json_encode($this->jsonSerialize(), $options);
117
    }
118
119
    public function __toString()
120
    {
121
        return $this->toJson();
122
    }
123
}
124