Field::isRelation()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
c 4
b 2
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php namespace Mascame\Artificer\Fields;
2
3
use App;
4
use Mascame\Artificer\Widgets\AbstractWidget;
5
6
class Field implements FieldInterface
7
{
8
    use Filterable;
9
10
    /**
11
     * @var
12
     */
13
    public $name;
14
15
    /**
16
     * @var string
17
     */
18
    public $type;
19
20
    /**
21
     * @var mixed
22
     */
23
    public $title;
24
25
    /**
26
     * @var null
27
     */
28
    public $value;
29
30
    public static $widgets = array();
31
    /**
32
     * @var FieldOptions
33
     */
34
    public $options;
35
36
    /**
37
     * @var FieldRelation
38
     */
39
    public $relation;
40
41
    /**
42
     * @var mixed
43
     */
44
    public $wiki;
45
46
    /**
47
     * @var FieldAttributes
48
     */
49
    public $attributes;
50
51
    /**
52
     * Sometimes ajax limits output, setting this to true will return all
53
     *
54
     * @var bool
55
     */
56
    public $showFullField = false;
57
58
59
    /**
60
     * @param $name
61
     * @param null $value
62
     * @param $modelName
63
     * @param $relation
64
     */
65
    public function __construct($name, $value = null, $relation)
66
    {
67
        $this->name = $name;
68
        $this->value = $value;
69
        $this->type = $this->getType(get_called_class());
70
71
        $this->options = new FieldOptions($this->name, $this->type);
72
73
        $this->relation = new FieldRelation($relation, $this->options->get('relationship'));
74
        $this->attributes = new FieldAttributes($this->options->get('attributes'), $this->options);
75
76
        if (!$this->attributes->has('class')) {
77
            $this->attributes->add(array('class' => 'form-control'));
78
        }
79
80
        $this->title = $this->options->get('title', $this->name);
81
        $this->wiki = $this->options->get('wiki');
82
83
        $this->boot();
84
    }
85
86
    /**
87
     * @param string $type_class
88
     * @return string
89
     */
90
    public function getType($type_class)
91
    {
92
        $pieces = explode('\\', $type_class);
93
94
        return strtolower(end($pieces));
95
    }
96
97
98
    /**
99
     * @param $widget
100
     * @return bool
101
     */
102
    public function addWidget(AbstractWidget $widget)
103
    {
104
        if (!in_array($widget->name, self::$widgets)) {
105
            self::$widgets[$widget->name] = $widget;
106
107
            return true;
108
        }
109
110
        return false;
111
    }
112
113
114
    /**
115
     * Used to load custom assets, widgets, ...
116
     *
117
     */
118
    public function boot()
119
    {
120
        if (! $this->options->has('widgets')) {
121
            return null;
122
        }
123
124
        $widgets = $this->options->get('widgets');
125
126
        foreach ($widgets as $widget) {
127
            try {
128
                $this->addWidget(App::make($widget));
129
            } catch (\Exception $e) {
130
                throw new \Exception("Widget '{$widget}' was not found");
131
            }
132
        }
133
    }
134
135
136
    /**
137
     * @return null
138
     */
139
    public function show()
140
    {
141
        return $this->value;
142
    }
143
144
    /**
145
     * @param null $value
146
     * @return null
147
     */
148
//    public function display($value = null)
149
//    {
150
//        $this->value = $this->getValue($value);
151
//
152
//        return $this->show();
153
//    }
154
155
156
    /**
157
     * @param null $value
158
     * @return null
159
     */
160
    public function getValue($value = null)
161
    {
162
        $value = ($value) ? $value : $this->options->get('default');
163
164
        if ($this->options->has('show')) {
165
            $show = $this->options->get('show');
166
167
            if (is_callable($show)) {
168
                return $show($value);
169
            }
170
        }
171
172
        return $value;
173
    }
174
175
176
    /**
177
     * @return bool
178
     */
179
    public function input()
180
    {
181
        return false;
182
    }
183
184
185
    /**
186
     * @param $input
187
     * @return mixed
188
     */
189
    public function userInput($input)
190
    {
191
        $input = str_replace('(:value)', $this->value, $input);
192
        $input = str_replace('(:name)', $this->name, $input);
193
        $input = str_replace('(:label)', $this->title, $input);
194
195
        return $input;
196
    }
197
198
199
    /**
200
     * @return bool|mixed|null|string
201
     */
202
    public function output()
203
    {
204
        if ($this->isHidden()) return null;
205
206
        if ($this->isGuarded()) return $this->guarded();
207
208
        $this->value = $this->getValue($this->value);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->value is correct as $this->getValue($this->value) (which targets Mascame\Artificer\Fields\Field::getValue()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
209
210
        if ($this->options->has('input')) {
211
            return $this->userInput($this->options->get('input'));
212
        }
213
214
        return $this->input();
215
    }
216
217
218
    /**
219
     * @return string
220
     */
221
    public function hidden()
222
    {
223
        return '<div class="label label-warning">Hidden data</div>';
224
    }
225
226
    /**
227
     * @param $array
228
     * @return bool
229
     */
230
    protected function isAll($array)
231
    {
232
        return (is_array($array) && isset($array[0]) && $array[0] == '*');
233
    }
234
235
    /**
236
     * @param string $list
237
     * @return bool
238
     */
239
    public function isListed($list = 'show')
240
    {
241
        if (!isset($this->options->model['list'][$list])) {
242
            return false;
243
        }
244
245
        $list = $this->options->model['list'][$list];
246
247
        if ($this->isAll($list)) {
248
            return true;
249
        }
250
251
        return $this->isInArray($this->name, $list);
252
    }
253
254
255
    /**
256
     * @return bool
257
     */
258
    public function isHiddenList()
259
    {
260
        return $this->isListed('hide');
261
    }
262
263
264
    /**
265
     * @param $value
266
     * @param $array
267
     * @return bool
268
     */
269
    public function isInArray($value, $array)
270
    {
271
        return (is_array($array) && in_array($value, $array)) ? true : false;
272
    }
273
274
275
    /**
276
     * @return string
277
     */
278
    public function guarded()
279
    {
280
        return '(guarded) ' . $this->show();
281
    }
282
283
    /**
284
     * @return bool
285
     */
286 View Code Duplication
    public function isGuarded()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
287
    {
288
        if (!isset($this->options->model['guarded'])) {
289
            return false;
290
        }
291
292
        return $this->isInArray($this->name, $this->options->model['guarded']);
293
    }
294
295
296
    /**
297
     * @return bool
298
     */
299 View Code Duplication
    public function isHidden()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
300
    {
301
        if (!isset($this->options->model['hidden'])) {
302
            return false;
303
        }
304
305
        return $this->isInArray($this->name, $this->options->model['hidden']);
306
    }
307
308
    /**
309
     * @return bool
310
     */
311
    public function isRelation()
312
    {
313
        return $this->relation->isRelation();
314
    }
315
316
    public static function get($name)
317
    {
318
        return array_get(\View::getShared(), 'fields')[$name];
319
    }
320
}