Completed
Push — dev ( 17f62c...81bde7 )
by Marc
10:28
created

Field::isInArray()   A

Complexity

Conditions 3
Paths 4

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 3
eloc 2
nc 4
nop 2
1
<?php namespace Mascame\Artificer\Fields;
2
3
use App;
4
use Mascame\Artificer\Widgets\AbstractWidget;
5
use Mascame\Formality\Field\FieldInterface;
6
use Mascame\Formality\Field\TypeInterface;
7
8
class Field
9
{
10
    use Filterable;
11
12
    /**
13
     * @var null
14
     */
15
    public $value;
16
17
    public static $widgets = array();
18
    /**
19
     * @var FieldOptions
20
     */
21
    public $options;
22
23
    /**
24
     * @var FieldRelation
25
     */
26
    public $relation = null;
27
28
    /**
29
     * Sometimes ajax limits output, setting this to true will return all
30
     *
31
     * @var bool
32
     */
33
    public $showFullField = false;
34
35
    /**
36
     * @var FieldInterface|TypeInterface
37
     */
38
    public $field;
39
40
    /**
41
     * Field constructor.
42
     * @param FieldInterface|TypeInterface $field
43
     * @param null $relation
44
     */
45
    public function __construct(FieldInterface $field, $relation = null)
46
    {
47
        $this->field = $field;
48
49
        if ($relation) {
50
            $this->relation = new FieldRelation($this->field->getOption('relationship'));
51
        }
52
53
        $this->boot();
54
    }
55
56
    /**
57
     * @param $widget
58
     * @return bool
59
     */
60
    public function addWidget(AbstractWidget $widget)
61
    {
62
        if ( ! in_array($widget->name, self::$widgets)) {
63
            self::$widgets[$widget->name] = $widget;
64
65
            return true;
66
        }
67
68
        return false;
69
    }
70
71
72
    /**
73
     * Used to load custom assets, widgets, ...
74
     *
75
     */
76
    public function boot()
77
    {
78
        if ( ! $this->field->getOption('widgets')) {
79
            return null;
80
        }
81
82
        $widgets = $this->field->getOption('widgets');
83
84
        foreach ($widgets as $widget) {
85
            try {
86
                $this->addWidget(App::make($widget));
87
            } catch (\Exception $e) {
88
                throw new \Exception("Widget '{$widget}' was not found");
89
            }
90
        }
91
    }
92
93
    /**
94
     * @param null $value
95
     * @return null
96
     */
97
//    public function display($value = null)
98
//    {
99
//        $this->value = $this->getValue($value);
100
//
101
//        return $this->show();
102
//    }
103
104
105
//    public function setValue($value) {
106
//        $this->value = $value;
107
//    }
108
109
    /**
110
     * @param null $value
111
     * @return null
112
     */
113
    public function show($value = null)
114
    {
115
        $value = ($value) ? $value : $this->field->getOption('default');
116
117
        if ($show = $this->field->getOption('show')) {
118
            if (is_callable($show)) {
119
                return $show($value);
120
            }
121
        }
122
123
        $this->field->setValue($value);
124
125
        return $this->field->show();
126
    }
127
128
    /**
129
     * @return bool|mixed|null|string
130
     */
131
    public function output()
132
    {
133
        if ($this->isHidden()) return null;
134
135
        if ($this->isGuarded()) return $this->guarded();
136
137
        return $this->field->output();
138
    }
139
140
141
    /**
142
     * @return string
143
     */
144
    public function hidden()
145
    {
146
        return '<div class="label label-warning">Hidden data</div>';
147
    }
148
149
    /**
150
     * @param $array
151
     * @return bool
152
     */
153
    protected function isAll($array)
154
    {
155
        return (is_array($array) && isset($array[0]) && $array[0] == '*');
156
    }
157
158
    /**
159
     * @param string $list
160
     * @return bool
161
     */
162
    protected function isListedAs($list = 'visible')
163
    {
164
        if ( ! isset($this->options->model['list'][$list])) {
165
            return false;
166
        }
167
168
        $list = $this->options->model['list'][$list];
169
170
        if ($this->isAll($list)) return true;
171
172
        return $this->isInArray($this->field->getName(), $list);
173
    }
174
175
    /**
176
     * @return bool
177
     */
178
    public function isListable()
179
    {
180
        return $this->isListedAs('visible') || ! $this->isListedAs('hidden');
181
    }
182
183
184
    /**
185
     * @param $value
186
     * @param $array
187
     * @return bool
188
     */
189
    public function isInArray($value, $array)
190
    {
191
        return (is_array($array) && in_array($value, $array)) ? true : false;
192
    }
193
194
195
    /**
196
     * @return string
197
     */
198
    public function guarded()
199
    {
200
        return '(guarded) ' . $this->show();
201
    }
202
203
    /**
204
     * @return bool
205
     */
206 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...
207
    {
208
        if (!isset($this->options->model['guarded'])) {
209
            return false;
210
        }
211
212
        return $this->isInArray($this->field->getName(), $this->options->model['guarded']);
213
    }
214
215
    /**
216
     * @return bool
217
     */
218 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...
219
    {
220
        if (!isset($this->options->model['hidden'])) {
221
            return false;
222
        }
223
224
        return $this->isInArray($this->field->getName(), $this->options->model['hidden']);
225
    }
226
227
    /**
228
     * @return bool
229
     */
230
    public function isRelation()
231
    {
232
        return $this->relation;
233
    }
234
235
    public static function get($name)
236
    {
237
        return array_get(\View::getShared(), 'fields')[$name];
238
    }
239
240
    public function __get($name) {
241
        $accessor = 'get' . studly_case($name);
242
243
        if (! method_exists($this->field, $accessor)) {
244
            return null;
245
        }
246
247
        return $this->field->$accessor();
248
    }
249
250
    public function __call($method, $args) {
251
        if (! method_exists($this->field, $method)) {
252
            return null;
253
        }
254
255
        return $this->field->$method($args);
256
    }
257
}