Completed
Push — dev ( 17f62c )
by Marc
09:19
created

Field::isHidden()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

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