|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Reallyli\Options; |
| 4 | + |
| 5 | +use Illuminate\Database\Eloquent\Model; |
| 6 | +use Illuminate\Database\Eloquent\SoftDeletes; |
| 7 | + |
| 8 | +class Option extends Model |
| 9 | +{ |
| 10 | + use SoftDeletes; |
| 11 | + |
| 12 | + /** |
| 13 | + * The attributes that are mass assignable. |
| 14 | + * |
| 15 | + * @var [type] |
| 16 | + */ |
| 17 | + protected $fillable = [ |
| 18 | + 'key', |
| 19 | + 'value', |
| 20 | + 'comment', |
| 21 | + 'module' |
| 22 | + ]; |
| 23 | + |
| 24 | + /** |
| 25 | + * Determine if the given option value exists. |
| 26 | + * |
| 27 | + * @param mixed $module |
| 28 | + * @param string $key |
| 29 | + * @return bool |
| 30 | + */ |
| 31 | + public function exists($module, $key) |
| 32 | + { |
| 33 | + return self::where([ |
| 34 | + 'module' => $module, |
| 35 | + 'key' => $key |
| 36 | + ])->exists(); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Get the specified option value. |
| 41 | + * |
| 42 | + * @param mixed $module |
| 43 | + * @param mixed $key |
| 44 | + * @param mixed $default |
| 45 | + * @return mixed |
| 46 | + */ |
| 47 | + public function get($module, $key, $default = null) |
| 48 | + { |
| 49 | + $option = self::where([ |
| 50 | + 'module' => $module, |
| 51 | + 'key' => $key |
| 52 | + ])->first(); |
| 53 | + |
| 54 | + return $option ? $option->value : $default; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Set a given option value. |
| 59 | + * |
| 60 | + * @param mixed $module |
| 61 | + * @param array|string $key |
| 62 | + * @param mixed $value |
| 63 | + * @param mixed $comment |
| 64 | + * @return void |
| 65 | + */ |
| 66 | + public function set($module, $key, $value = null, $comment = null) |
| 67 | + { |
| 68 | + $keys = is_array($key) ? $key : [$key => $value]; |
| 69 | + |
| 70 | + $data = [ |
| 71 | + 'module' => $module, |
| 72 | + 'comment' => $comment |
| 73 | + ]; |
| 74 | + |
| 75 | + foreach ($keys as $key => $value) { |
| 76 | + $data['value'] = $value; |
| 77 | + |
| 78 | + self::updateOrCreate(['key' => $key, 'module' => $module], $data); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Remove/delete the specified option value. |
| 84 | + * |
| 85 | + * @param mixed $module |
| 86 | + * @param string $key |
| 87 | + * @return bool |
| 88 | + */ |
| 89 | + public function remove($module, $key) |
| 90 | + { |
| 91 | + return (bool) self::where([ |
| 92 | + 'module' => $module, |
| 93 | + 'key' => $key |
| 94 | + ])->delete(); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Increase the specified option value. |
| 99 | + * |
| 100 | + * @param mixed $module |
| 101 | + * @param string $key |
| 102 | + * @param int $step |
| 103 | + * @return mixed |
| 104 | + */ |
| 105 | + public function increase($module, $key, $step = 1) |
| 106 | + { |
| 107 | + $option = self::where([ |
| 108 | + 'module' => $module, |
| 109 | + 'key' => $key |
| 110 | + ])->first(); |
| 111 | + |
| 112 | + return $option ? $option->increment('value', $step) : null; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Decrease the specified option value. |
| 117 | + * |
| 118 | + * @param mixed $module |
| 119 | + * @param string $key |
| 120 | + * @param int $step |
| 121 | + * @return bool |
| 122 | + */ |
| 123 | + public function decrease($module, $key, $step = 1) |
| 124 | + { |
| 125 | + $option = self::where([ |
| 126 | + 'module' => $module, |
| 127 | + 'key' => $key |
| 128 | + ])->first(); |
| 129 | + |
| 130 | + return $option ? $option->decrement('value', $step) : null; |
| 131 | + } |
| 132 | +} |
0 commit comments