Completed
Push — master ( a8ee71...8c1f56 )
by wen
02:53
created

Element::getValidationRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Sco\Admin\Form\Elements;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Http\Request;
7
use Sco\Admin\Contracts\Form\Elements\ElementInterface;
8
9
abstract class Element implements ElementInterface
10
{
11
    protected $type;
12
13
    protected $name;
14
    protected $title;
15
16
    /**
17
     * @var mixed
18
     */
19
    protected $defaultValue;
20
21
    /**
22
     * @var \Illuminate\Database\Eloquent\Model
23
     */
24
    protected $model;
25
26
    public function __construct($name, $title)
27
    {
28
        $this->name  = $name;
29
        $this->title = $title;
30
    }
31
32
    public function getName()
33
    {
34
        return $this->name;
35
    }
36
37
    public function setName($value)
38
    {
39
        $this->name = $value;
40
41
        return $this;
42
    }
43
44
    public function getTitle()
45
    {
46
        return $this->title;
47
    }
48
49
    public function setTitle($value)
50
    {
51
        $this->title = $value;
52
53
        return $this;
54
    }
55
56
    public function save(Request $request)
57
    {
58
        $this->setModelAttribute(
59
            $this->getValueFromRequest($request)
60
        );
61
    }
62
63
    protected function setModelAttribute($value)
64
    {
65
        $model = $this->getModel();
66
        $model->setAttribute($this->getName(), $value);
67
    }
68
69
    protected function getValueFromRequest(Request $request)
70
    {
71
        return $request->input($this->getName());
72
    }
73
74
    public function getModel()
75
    {
76
        return $this->model;
77
    }
78
79
    public function setModel(Model $model)
80
    {
81
        $this->model = $model;
82
83
        return $this;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function getValue()
90
    {
91
        return $this->getValueFromModel();
92
    }
93
94
    protected function getValueFromModel()
95
    {
96
        $model = $this->getModel();
97
        $value = $this->getDefaultValue();
98
        if (is_null($model) || !$model->exists) {
99
            return $value;
100
        }
101
        return $model->getAttribute($this->getName());
102
103
        /*$relations = explode('.', $this->getName(), 2);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
104
        $count = count($relations);
105
106
        if ($count == 1) {
107
            return $model->getAttribute($this->getName());
108
        }
109
110
        foreach ($relations as $relation) {
111
            if ($model->{$relation} instanceof Model) {
112
                $model = $model->{$relation};
113
                continue;
114
            }
115
116
            return $model->getAttribute($relation);
117
        }*/
118
    }
119
120
    protected function getDefaultValue()
121
    {
122
        return $this->defaultValue;
123
    }
124
125
    public function toArray()
126
    {
127
        return [
128
            'key'   => $this->name,
129
            'title' => $this->title,
130
            'type'  => $this->type,
131
        ];
132
    }
133
134
    public function jsonSerialize()
135
    {
136
        return $this->toArray();
137
    }
138
139
    public function toJson($options = 0)
140
    {
141
        return json_encode($this->jsonSerialize(), $options);
142
    }
143
144
    public function __toString()
145
    {
146
        return $this->toJson();
147
    }
148
}
149