Test Setup Failed
Push — master ( 81f5ad...e23419 )
by Terzi
04:23
created

Generic::hideOnPages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Terranet\Administrator\Field;
4
5
use Coduo\PHPHumanizer\StringHumanizer;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Facades\View;
8
use Terranet\Administrator\Field\Traits\AcceptsCustomFormat;
9
use Terranet\Administrator\Scaffolding;
10
11
abstract class Generic
12
{
13
    use AcceptsCustomFormat;
14
15
    /** @var string */
16
    protected $id;
17
18
    /** @var string */
19
    protected $title;
20
21
    /** @var mixed */
22
    protected $value;
23
24
    /** @var string */
25
    protected $description;
26
27
    /** @var Model */
28
    protected $model;
29
30
    /** @var bool */
31
    protected $showLabel = true;
32
33
    /** @var array */
34
    protected $visibility = [
35
        Scaffolding::PAGE_INDEX => true,
36
        Scaffolding::PAGE_EDIT => true,
37
        Scaffolding::PAGE_VIEW => true,
38
    ];
39
40
    /** @var array */
41
    protected $attributes = [];
42
43
    /**
44
     * Generic constructor.
45
     *
46
     * @param $title
47
     * @param null $id
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $id is correct as it would always require null to be passed?
Loading history...
48
     */
49
    private function __construct($title, $id = null)
50
    {
51
        $this->title = StringHumanizer::humanize($title);
52
        $this->id = snake_case($id ?: $this->title);
53
    }
54
55
    /**
56
     * @param $title
57
     * @param null $id
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $id is correct as it would always require null to be passed?
Loading history...
58
     * @param \Closure $callback
59
     *
60
     * @return static
61
     */
62
    public static function make($title, $id = null, \Closure $callback = null): self
63
    {
64
        $instance = new static($title, $id);
65
66
        if (null !== $callback) {
67
            $callback->call($instance, $instance);
68
        }
69
70
        return $instance;
71
    }
72
73
    /**
74
     * @param Model $model
75
     *
76
     * @return self
77
     */
78
    public function setModel(Model $model): self
79
    {
80
        $this->model = $model;
81
82
        return $this;
83
    }
84
85
    /**
86
     * @return Model
87
     */
88
    public function getModel()
89
    {
90
        return $this->model;
91
    }
92
93
    /**
94
     * Render Element.
95
     *
96
     * @param string $page
97
     *
98
     * @return mixed
99
     */
100
    final public function render(string $page = 'index')
101
    {
102
        if ($this->format) {
103
            // Each Field can define its own data for custom formatter.
104
            $withData = method_exists($this, 'renderWith')
105
                ? $this->renderWith()
106
                : [$this->value(), $this->model];
107
108
            return $this->callFormatter($withData);
109
        }
110
111
        $data = [
112
            'field' => $this,
113
            'model' => $this->model,
114
        ];
115
116
        if (method_exists($this, $dataGetter = 'on'.title_case($page))) {
117
            $data += call_user_func([$this, $dataGetter]);
118
        }
119
120
        if (View::exists($view = $this->template($page))) {
121
            return View::make($view, $data);
122
        }
123
124
        return View::make($this->template($page, 'Key'), $data);
125
    }
126
127
    /**
128
     * Return Element ID.
129
     *
130
     * @return string
131
     */
132
    public function id(): string
133
    {
134
        return $this->id;
135
    }
136
137
    /**
138
     * Form name.
139
     *
140
     * @return string
141
     */
142
    public function name()
143
    {
144
        $parts = explode('.', $this->id());
145
146
        if (count($parts) > 1) {
147
            $first = array_first($parts);
148
            $other = array_slice($parts, 1);
149
150
            $other = array_map(function ($part) {
151
                return "[$part]";
152
            }, $other);
153
154
            return join('', array_merge([$first], $other));
155
        }
156
157
        return $this->id();
158
    }
159
160
    /**
161
     * @param string $id
162
     * @return self
163
     */
164
    public function setId(string $id): self
165
    {
166
        $this->id = $id;
167
168
        return $this;
169
    }
170
171
    /**
172
     * Return Element title.
173
     *
174
     * @return string
175
     */
176
    public function title(): string
177
    {
178
        return $this->title;
179
    }
180
181
    /**
182
     * @return string
183
     */
184
    public function getDescription(): ?string
185
    {
186
        return $this->description;
187
    }
188
189
    /**
190
     * @param string $title
191
     *
192
     * @return self
193
     */
194
    public function setTitle(string $title): self
195
    {
196
        $this->title = $title;
197
198
        return $this;
199
    }
200
201
    /**
202
     * @param string $description
203
     * @return self
204
     */
205
    public function setDescription(string $description): self
206
    {
207
        $this->description = $description;
208
209
        return $this;
210
    }
211
212
    /**
213
     * @param bool $showLabel
214
     *
215
     * @return self
216
     */
217
    public function hideLabel(bool $hideLabel): self
218
    {
219
        $this->showLabel = !$hideLabel;
220
221
        return $this;
222
    }
223
224
    /**
225
     * @return bool
226
     */
227
    public function isHiddenLabel(): bool
228
    {
229
        return !$this->showLabel;
230
    }
231
232
    /**
233
     * string $page.
234
     *
235
     * @return bool
236
     */
237
    public function isVisibleOnPage(string $page): bool
238
    {
239
        return (bool) $this->visibility[$page] ?? false;
240
    }
241
242
    /**
243
     * @param array|string $pages
244
     *
245
     * @return self
246
     */
247
    public function hideOnPages($pages): self
248
    {
249
        return $this->setPagesVisibility((array) $pages, false);
250
    }
251
252
    /**
253
     * @param array|string $pages
254
     *
255
     * @return self
256
     */
257
    public function showOnPages($pages): self
258
    {
259
        return $this->setPagesVisibility((array) $pages, true);
260
    }
261
262
    /**
263
     * Make column sortable.
264
     *
265
     * @param null|\Closure $callback
266
     *
267
     * @return self
268
     */
269
    public function sortable(\Closure $callback = null): self
270
    {
271
        app('scaffold.module')->addSortable(
0 ignored issues
show
Bug introduced by
The method addSortable() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

271
        app('scaffold.module')->/** @scrutinizer ignore-call */ addSortable(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
272
            $this->id(),
273
            $callback
274
        );
275
276
        return $this;
277
    }
278
279
    /**
280
     * Remove column from Sortable collection.
281
     *
282
     * @return self
283
     */
284
    public function disableSorting(): self
285
    {
286
        app('scaffold.module')->removeSortable($this->id());
0 ignored issues
show
Bug introduced by
The method removeSortable() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

286
        app('scaffold.module')->/** @scrutinizer ignore-call */ removeSortable($this->id());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
287
288
        return $this;
289
    }
290
291
    /**
292
     * Set value.
293
     *
294
     * @param $value
295
     * @return Generic
296
     */
297
    public function setValue($value): self
298
    {
299
        $this->value = $value;
300
301
        return $this;
302
    }
303
304
    /**
305
     * Extract Element value.
306
     *
307
     * @return mixed
308
     */
309
    public function value()
310
    {
311
        if (!$this->model) {
312
            return null;
313
        }
314
315
        if (null === $this->value) {
316
            $this->value = $this->model->getAttribute($this->id);
317
        }
318
319
        return $this->value;
320
    }
321
322
    /**
323
     * @param $key
324
     * @param null $value
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $value is correct as it would always require null to be passed?
Loading history...
325
     * @return self
326
     */
327
    public function setAttribute($attribute, $value = null): self
328
    {
329
        if (is_array($attribute)) {
330
            foreach ($attribute as $key => $value) {
331
                $this->setAttribute($key, $value);
332
            }
333
        } else {
334
            if (!array_key_exists($key, $this->attributes)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $key seems to be never defined.
Loading history...
335
                throw new Exception("Unknown attribute {$key}");
0 ignored issues
show
Bug introduced by
The type Terranet\Administrator\Field\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
336
            }
337
            $this->attributes[$key] = $value;
338
        }
339
340
        return $this;
341
    }
342
343
    /**
344
     * @param mixed $pages
345
     * @param bool $visibility
346
     *
347
     * @return $this
348
     */
349
    protected function setPagesVisibility($pages, bool $visibility): self
350
    {
351
        $pages = array_intersect($pages, array_keys($this->visibility));
352
353
        foreach ($pages as $page) {
354
            $this->visibility[$page] = $visibility;
355
        }
356
357
        return $this;
358
    }
359
360
    /**
361
     * @param string $page
362
     * @param string $field
363
     *
364
     * @return string
365
     */
366
    protected function template(string $page, string $field = null): string
367
    {
368
        return sprintf(
369
            'administrator::fields.%s.%s',
370
            snake_case($field ?? class_basename($this)),
371
            $page
372
        );
373
    }
374
}
375