Passed
Push — master ( 0c664b...4bf842 )
by Gombos
04:37
created

src/Traits/HasSettingsField.php (1 issue)

1
<?php
2
3
namespace Glorand\Model\Settings\Traits;
4
5
use Glorand\Model\Settings\Contracts\SettingsManagerContract;
6
use Glorand\Model\Settings\Exceptions\ModelSettingsException;
7
use Glorand\Model\Settings\Managers\FieldSettingsManager;
8
9
/**
10
 * Trait HasSettingsField
11
 * @package Glorand\Model\Settings\Traits
12
 * @property array $settings
13
 * @property string $settingsFieldName
14
 * @property boolean $persistSettings
15
 */
16
trait HasSettingsField
17
{
18
    use HasSettings;
19
20
    private $persistSettings = null;
21
22
    /**
23
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
24
     * @throws ModelSettingsException
25
     */
26 14
    public function settings(): SettingsManagerContract
27
    {
28 14
        return new FieldSettingsManager($this);
29
    }
30
31
    /**
32
     * @return array
33
     * @throws ModelSettingsException
34
     */
35 14
    public function getSettingsValue(): array
36
    {
37 14
        $settingsFieldName = $this->getSettingsFieldName();
38 14
        $attributes = $this->getAttributes();
39 14
        if (!array_has($attributes, $settingsFieldName)) {
0 ignored issues
show
Deprecated Code introduced by
The function array_has() has been deprecated: Arr::has() should be used directly instead. Will be removed in Laravel 6.0. ( Ignorable by Annotation )

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

39
        if (!/** @scrutinizer ignore-deprecated */ array_has($attributes, $settingsFieldName)) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
40 2
            throw new ModelSettingsException("Unknown field ($settingsFieldName) on table {$this->getTable()}");
41
        }
42
43 12
        return json_decode($this->getAttributeValue($settingsFieldName) ?? '[]', true);
44
    }
45
46
    /**
47
     * @return string
48
     */
49 14
    public function getSettingsFieldName(): string
50
    {
51 14
        return $this->settingsFieldName ?? config('model_settings.settings_field_name');
52
    }
53
54
    /**
55
     * @return bool
56
     */
57 11
    public function isPersistSettings(): bool
58
    {
59 11
        return boolval($this->persistSettings ?? config('model_settings.settings_persistent'));
60
    }
61
62
    /**
63
     * @param bool $val
64
     */
65 1
    public function setPersistSettings(bool $val = true)
66
    {
67 1
        $this->persistSettings = $val;
68 1
    }
69
70
    abstract public function getTable();
71
72
    abstract public function getAttributes();
73
74
    abstract public function getAttributeValue($key);
75
}
76