Completed
Push — dev ( fd204c...070865 )
by Marc
02:19
created

FieldWrapper::isInArray()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
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\Artificer;
5
use Mascame\Artificer\Widgets\AbstractWidget;
6
use Mascame\Formality\Field\FieldInterface;
7
use Mascame\Formality\Field\TypeInterface;
8
9
class FieldWrapper
10
{
11
    use Filterable;
12
13
    public static $widgets = array();
14
15
    /**
16
     * Sometimes ajax limits output, setting this to true will return all
17
     *
18
     * @var bool
19
     */
20
    public $showFullField = false;
21
22
    /**
23
     * @var FieldInterface|TypeInterface
24
     */
25
    protected $field;
26
27
    /**
28
     * Field constructor.
29
     * @param FieldInterface|TypeInterface $field
30
     * @param null $relation
0 ignored issues
show
Bug introduced by
There is no parameter named $relation. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
31
     */
32
    public function __construct(FieldInterface $field)
33
    {
34
        $this->field = $field;
35
36
        $this->boot();
37
    }
38
39
    /**
40
     * @param $widget
41
     * @return bool
42
     */
43
    public function addWidget(AbstractWidget $widget)
44
    {
45
        if ( ! in_array($widget->name, self::$widgets)) {
46
            self::$widgets[$widget->name] = $widget;
47
48
            return true;
49
        }
50
51
        return false;
52
    }
53
54
55
    /**
56
     * Used to load custom assets, widgets, ...
57
     *
58
     */
59
    public function boot()
60
    {
61
        if ( ! $this->field->getOption('widgets')) {
62
            return null;
63
        }
64
65
        $widgets = $this->field->getOption('widgets');
66
67
        foreach ($widgets as $widget) {
68
            try {
69
                $this->addWidget(App::make($widget));
70
            } catch (\Exception $e) {
71
                throw new \Exception("Widget '{$widget}' was not found");
72
            }
73
        }
74
    }
75
76
    /**
77
     * @param null $value
78
     * @return null
79
     */
80
    public function show($value = null)
81
    {
82
        $value = ($value) ? $this->field->setValue($value) : $this->field->getOption('default');
83
84
        if ($show = $this->field->getOption('show')) {
85
            if (is_callable($show)) {
86
                return $show($value);
87
            }
88
        }
89
90
        return $this->field->show();
91
    }
92
93
    /**
94
     * @return bool|mixed|null|string
95
     */
96
    public function output()
97
    {
98
        if ($this->isHidden()) return null;
99
100
        if ($this->isGuarded()) return $this->guarded();
101
102
        return $this->field->output();
103
    }
104
105
    /**
106
     * @param $array
107
     * @return bool
108
     */
109
    protected function isAll($array)
110
    {
111
        return (is_array($array) && isset($array[0]) && $array[0] == '*');
112
    }
113
114
    /**
115
     * @param string $visibility [visible|hidden]
116
     * @return bool
117
     */
118
    protected function isListedAs($visibility, $action)
119
    {
120
        $listOptions = Artificer::getModel()->getOption($action);
121
122
        if (! $listOptions || ! isset($listOptions[$visibility])) return false;
123
124
        $list = $listOptions[$visibility];
125
126
        if ($this->isAll($list)) return true;
127
128
        return $this->isInArray($this->field->getName(), $list);
129
    }
130
131
    /**
132
     * list, edit, create
133
     *
134
     * Hidden fields have preference.
135
     *
136
     * @return bool
137
     */
138
    public function isListable($action = null)
139
    {
140
        if ( ! $action) $action = Artificer::getCurrentAction();
141
142
        if ($this->isListedAs('hidden', $action)) return false;
143
144
        return $this->isListedAs('visible', $action);
145
    }
146
147
    /**
148
     * @param $value
149
     * @param $array
150
     * @return bool
151
     */
152
    public function isInArray($value, $array)
153
    {
154
        return (is_array($array) && in_array($value, $array)) ? true : false;
155
    }
156
157
158
    /**
159
     * @return string
160
     */
161
    public function guarded()
162
    {
163
        return '[PROTECTED] ' . $this->show();
164
    }
165
166
    /**
167
     * @return bool
168
     */
169
    public function isGuarded()
170
    {
171
        $guarded = Artificer::getModel()->getOption('guarded', []);
172
173
        return $this->isInArray($this->field->getName(), $guarded);
174
    }
175
176
    /**
177
     * @return bool
178
     */
179
    public function isHidden()
180
    {
181
        $hidden = Artificer::getModel()->getOption('hidden', []);
182
183
        return $this->isInArray($this->field->getName(), $hidden);
184
    }
185
186
    public static function get($name)
187
    {
188
        return array_get(\View::getShared(), 'fields')[$name];
189
    }
190
191
    public function __get($name) {
192
        $accessor = 'get' . studly_case($name);
193
194
        if (! method_exists($this->field, $accessor)) {
195
            return null;
196
        }
197
198
        return $this->field->$accessor();
199
    }
200
201
    public function __call($method, $args) {
202
        if (! method_exists($this->field, $method)) {
203
            return null;
204
        }
205
206
        return $this->field->$method($args);
207
    }
208
}