webshelf /
framework
We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Created by PhpStorm. |
||
| 4 | * User: Mark |
||
| 5 | * Date: 19/09/2016 |
||
| 6 | * Time: 21:34. |
||
| 7 | */ |
||
| 8 | |||
| 9 | namespace App\Classes; |
||
| 10 | |||
| 11 | use App\Model\Setting; |
||
| 12 | use Illuminate\Support\Collection; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Class Settings. |
||
| 16 | */ |
||
| 17 | class SettingsManager |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * A storage of all the settings being held for usage. |
||
| 21 | * |
||
| 22 | * @var Collection |
||
| 23 | */ |
||
| 24 | private $settings; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Settings constructor. |
||
| 28 | */ |
||
| 29 | public function __construct() |
||
| 30 | { |
||
| 31 | $this->settings = new Collection; |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Add a new setting to the container. |
||
| 36 | * |
||
| 37 | * @param $key |
||
| 38 | * @param $value |
||
| 39 | * @param null $shadow |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 40 | * @internal param array $array |
||
| 41 | * @return Collection |
||
| 42 | */ |
||
| 43 | public function add($key, $value, $shadow = null) |
||
| 44 | { |
||
| 45 | return $this->settings->put($key, ['key' => $key, 'value' => $value, 'shadow' => $shadow]); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Remove a setting from the container. |
||
| 50 | * |
||
| 51 | * @param $key |
||
| 52 | * @return Collection |
||
| 53 | */ |
||
| 54 | public function remove($key) |
||
| 55 | { |
||
| 56 | return $this->settings->forget($key); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Edit a setting from the container. |
||
| 61 | * |
||
| 62 | * @param $key |
||
| 63 | * @param $value |
||
| 64 | * @return Collection |
||
| 65 | */ |
||
| 66 | public function set($key, $value) |
||
| 67 | { |
||
| 68 | return $this->add($key, $value); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Check if settings exists. |
||
| 73 | * |
||
| 74 | * @param $key |
||
| 75 | * @return bool |
||
| 76 | */ |
||
| 77 | public function has($key) |
||
| 78 | { |
||
| 79 | return $this->settings->has($key); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Get a value from the container. |
||
| 84 | * This represents the default value of the key. |
||
| 85 | * |
||
| 86 | * @param $key |
||
| 87 | */ |
||
| 88 | public function getValue($key) |
||
| 89 | { |
||
| 90 | return $this->settings->get($key)['value']; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Get the shadow value from the container. |
||
| 95 | * This represents the original default of the key. |
||
| 96 | * |
||
| 97 | * @param $key |
||
| 98 | */ |
||
| 99 | public function getShadow($key) |
||
| 100 | { |
||
| 101 | return $this->settings->get($key)['shadow']; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Get the settings default key. |
||
| 106 | * |
||
| 107 | * If the user did not enter a value, we can use the shadow value. |
||
| 108 | * |
||
| 109 | * @param $key |
||
| 110 | * @return mixed |
||
| 111 | */ |
||
| 112 | public function getDefault($key) |
||
| 113 | { |
||
| 114 | $key = $this->settings->get($key); |
||
| 115 | |||
| 116 | return $key['value'] ?: $key['shadow']; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Count the available settings. |
||
| 121 | * |
||
| 122 | * @return int |
||
| 123 | */ |
||
| 124 | public function count() |
||
| 125 | { |
||
| 126 | return $this->settings->count(); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * For tenant usage, we must add a collection of settings models from the database. |
||
| 131 | * |
||
| 132 | * Can be used for anything else though. |
||
| 133 | * |
||
| 134 | * @param Collection $collection |
||
| 135 | * @return $this |
||
| 136 | */ |
||
| 137 | public function collect(Collection $collection) |
||
| 138 | { |
||
| 139 | /** @var Setting $model */ |
||
| 140 | foreach ($collection as $model) { |
||
| 141 | $this->add($model->key(), $model->value(), $model->shadow()); |
||
| 142 | } |
||
| 143 | |||
| 144 | return $this; |
||
| 145 | } |
||
| 146 | } |
||
| 147 |