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

Element::addValidationRule()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
4
namespace Sco\Admin\Form\Elements;
5
6
use Illuminate\Database\Eloquent\Model;
7
use JsonSerializable;
8
use Illuminate\Contracts\Support\Arrayable;
9
use Illuminate\Contracts\Support\Jsonable;
10
use Sco\Admin\Contracts\Form\Elements\ElementInterface;
11
use Sco\Admin\Contracts\Validable;
12
use Sco\Admin\Contracts\WithModel;
13
14
abstract class Element implements
15
    ElementInterface,
16
    WithModel,
17
    Arrayable,
18
    Jsonable,
19
    JsonSerializable,
20
    Validable
21
{
22
    protected $type;
23
24
    protected $name;
25
    protected $title;
26
27
    /**
28
     * @var mixed
29
     */
30
    protected $defaultValue;
31
32
    /**
33
     * @var \Illuminate\Database\Eloquent\Model
34
     */
35
    protected $model;
36
37
    protected $validationRules = [];
38
39
    protected $validationMessages = [];
40
41
    public function __construct($name, $title)
42
    {
43
        $this->name  = $name;
44
        $this->title = $title;
45
    }
46
47
    public function getName()
48
    {
49
        return $this->name;
50
    }
51
52
    public function setName($value)
53
    {
54
        $this->name = $value;
55
56
        return $this;
57
    }
58
59
    public function getTitle()
60
    {
61
        return $this->title;
62
    }
63
64
    public function setTitle($value)
65
    {
66
        $this->title = $value;
67
68
        return $this;
69
    }
70
71
    public function getModel()
72
    {
73
        return $this->model;
74
    }
75
76
    public function setModel(Model $model)
77
    {
78
        $this->model = $model;
79
80
        return $this;
81
    }
82
83
    public function getValue()
84
    {
85
        return $this->getModelValue();
86
    }
87
88
    protected function getModelValue()
89
    {
90
        $model = $this->getModel();
91
        $value = $this->getDefaultValue();
92
        if (is_null($model) || !$model->exists) {
93
            return $value;
94
        }
95
96
        $relations = explode('.', $this->getName(), 2);
97
        $count = count($relations);
98
99
        if ($count == 1) {
100
            return $model->getAttribute($this->getName());
101
        }
102
103
        foreach ($relations as $relation) {
104
            if ($model->{$relation} instanceof Model) {
105
                $model = $model->{$relation};
106
                continue;
107
            }
108
109
            return $model->getAttribute($relation);
110
        }
111
    }
112
113
    protected function getDefaultValue()
114
    {
115
        return $this->defaultValue;
116
    }
117
118
    public function toArray()
119
    {
120
        return [
121
            'key'   => $this->name,
122
            'title' => $this->title,
123
            'type'  => $this->type,
124
        ];
125
    }
126
127
    public function addValidationRule($rule, $message = null)
128
    {
129
        $this->validationRules[] = $rule;
130
131
        if (is_null($message)) {
132
            return $this;
133
        }
134
135
        return $this->addValidationMessage($rule, $message);
136
    }
137
138
    public function addValidationMessage($rule, $message)
139
    {
140
        if (($pos = strpos($rule, ':')) !== false) {
141
            $rule = substr($rule, 0, $pos);
142
        }
143
144
        $this->validationMessages[$rule] = $message;
145
146
        return $this;
147
    }
148
149
    public function getValidationMessages()
150
    {
151
        return $this->validationMessages;
152
    }
153
154
    public function getValidationRules()
155
    {
156
        return $this->validationRules;
157
    }
158
159
    public function getValidationTitles()
160
    {
161
        return [$this->getName() => $this->getTitle()];
162
    }
163
164
    public function jsonSerialize()
165
    {
166
        return $this->toArray();
167
    }
168
169
    public function toJson($options = 0)
170
    {
171
        return json_encode($this->jsonSerialize(), $options);
172
    }
173
174
    public function __toString()
175
    {
176
        return $this->toJson();
177
    }
178
}
179