|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of Laravel Eloquent Flag. |
| 5 | + * |
| 6 | + * (c) CyberCog <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Cog\Flag\Scopes; |
| 13 | + |
| 14 | +use Illuminate\Database\Eloquent\Model; |
| 15 | +use Illuminate\Database\Eloquent\Scope; |
| 16 | +use Illuminate\Database\Eloquent\Builder; |
| 17 | + |
| 18 | +/** |
| 19 | + * Class KeptFlagScope. |
| 20 | + * |
| 21 | + * @package Cog\Flag\Scopes |
| 22 | + */ |
| 23 | +class KeptFlagScope implements Scope |
| 24 | +{ |
| 25 | + /** |
| 26 | + * All of the extensions to be added to the builder. |
| 27 | + * |
| 28 | + * @var array |
| 29 | + */ |
| 30 | + protected $extensions = ['Keep', 'Unkeep', 'WithUnkept', 'WithoutUnkept', 'OnlyUnkept']; |
| 31 | + |
| 32 | + /** |
| 33 | + * Apply the scope to a given Eloquent query builder. |
| 34 | + * |
| 35 | + * @param \Illuminate\Database\Eloquent\Builder $builder |
| 36 | + * @param \Illuminate\Database\Eloquent\Model $model |
| 37 | + * @return \Illuminate\Database\Eloquent\Builder |
| 38 | + */ |
| 39 | + public function apply(Builder $builder, Model $model) |
| 40 | + { |
| 41 | + return $builder->where('is_kept', 1); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Extend the query builder with the needed functions. |
| 46 | + * |
| 47 | + * @param \Illuminate\Database\Eloquent\Builder $builder |
| 48 | + * @return void |
| 49 | + */ |
| 50 | + public function extend(Builder $builder) |
| 51 | + { |
| 52 | + foreach ($this->extensions as $extension) { |
| 53 | + $this->{"add{$extension}"}($builder); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Add the `keep` extension to the builder. |
| 59 | + * |
| 60 | + * @param \Illuminate\Database\Eloquent\Builder $builder |
| 61 | + * @return void |
| 62 | + */ |
| 63 | + protected function addKeep(Builder $builder) |
| 64 | + { |
| 65 | + $builder->macro('keep', function (Builder $builder) { |
| 66 | + $builder->withUnkept(); |
| 67 | + |
| 68 | + return $builder->update(['is_kept' => 1]); |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Add the `unkeep` extension to the builder. |
| 74 | + * |
| 75 | + * @param \Illuminate\Database\Eloquent\Builder $builder |
| 76 | + * @return void |
| 77 | + */ |
| 78 | + protected function addUnkeep(Builder $builder) |
| 79 | + { |
| 80 | + $builder->macro('unkeep', function (Builder $builder) { |
| 81 | + return $builder->update(['is_kept' => 0]); |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Add the `withUnkept` extension to the builder. |
| 87 | + * |
| 88 | + * @param \Illuminate\Database\Eloquent\Builder $builder |
| 89 | + * @return void |
| 90 | + */ |
| 91 | + protected function addWithUnkept(Builder $builder) |
| 92 | + { |
| 93 | + $builder->macro('withUnkept', function (Builder $builder) { |
| 94 | + return $builder->withoutGlobalScope($this); |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Add the `withoutUnkept` extension to the builder. |
| 100 | + * |
| 101 | + * @param \Illuminate\Database\Eloquent\Builder $builder |
| 102 | + * @return void |
| 103 | + */ |
| 104 | + protected function addWithoutUnkept(Builder $builder) |
| 105 | + { |
| 106 | + $builder->macro('withoutUnkept', function (Builder $builder) { |
| 107 | + return $builder->withoutGlobalScope($this)->where('is_kept', 1); |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Add the `onlyUnkept` extension to the builder. |
| 113 | + * |
| 114 | + * @param \Illuminate\Database\Eloquent\Builder $builder |
| 115 | + * @return void |
| 116 | + */ |
| 117 | + protected function addOnlyUnkept(Builder $builder) |
| 118 | + { |
| 119 | + $builder->macro('onlyUnkept', function (Builder $builder) { |
| 120 | + return $builder->withoutGlobalScope($this)->where('is_kept', 0); |
| 121 | + }); |
| 122 | + } |
| 123 | +} |
0 commit comments