-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BannedAtScope
- Loading branch information
Pe Ell
authored
Mar 20, 2017
1 parent
3f8c0a3
commit e83ebaa
Showing
9 changed files
with
379 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Laravel Ban. | ||
* | ||
* (c) Anton Komarev <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Cog\Ban\Scopes; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Scope; | ||
|
||
/** | ||
* Class BannedAtScope. | ||
* | ||
* @package Cog\Ban\Scopes | ||
*/ | ||
class BannedAtScope implements Scope | ||
{ | ||
/** | ||
* All of the extensions to be added to the builder. | ||
* | ||
* @var array | ||
*/ | ||
protected $extensions = ['WithBanned', 'WithoutBanned', 'OnlyBanned']; | ||
|
||
/** | ||
* Apply the scope to a given Eloquent query builder. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $builder | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @return \Illuminate\Database\Eloquent\Builder | ||
*/ | ||
public function apply(Builder $builder, Model $model) | ||
{ | ||
if (method_exists($model, 'shouldApplyBannedAtScope') && $model->shouldApplyBannedAtScope()) { | ||
return $builder->whereNull('banned_at'); | ||
} | ||
|
||
return $builder; | ||
} | ||
|
||
/** | ||
* Extend the query builder with the needed functions. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $builder | ||
* @return void | ||
*/ | ||
public function extend(Builder $builder) | ||
{ | ||
foreach ($this->extensions as $extension) { | ||
$this->{"add{$extension}"}($builder); | ||
} | ||
} | ||
|
||
/** | ||
* Add the `withBanned` extension to the builder. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $builder | ||
* @return void | ||
*/ | ||
protected function addWithBanned(Builder $builder) | ||
{ | ||
$builder->macro('withBanned', function (Builder $builder) { | ||
return $builder->withoutGlobalScope($this); | ||
}); | ||
} | ||
|
||
/** | ||
* Add the `withoutBanned` extension to the builder. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $builder | ||
* @return void | ||
*/ | ||
protected function addWithoutBanned(Builder $builder) | ||
{ | ||
$builder->macro('withoutBanned', function (Builder $builder) { | ||
return $builder->withoutGlobalScope($this)->whereNull('banned_at'); | ||
}); | ||
} | ||
|
||
/** | ||
* Add the `onlyBanned` extension to the builder. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $builder | ||
* @return void | ||
*/ | ||
protected function addOnlyBanned(Builder $builder) | ||
{ | ||
$builder->macro('onlyBanned', function (Builder $builder) { | ||
return $builder->withoutGlobalScope($this)->whereNotNull('banned_at'); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Laravel Ban. | ||
* | ||
* (c) Anton Komarev <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Cog\Ban\Traits; | ||
|
||
use Cog\Ban\Scopes\BannedAtScope; | ||
|
||
/** | ||
* Class HasBannedAtScope. | ||
* | ||
* @package Cog\Ban\Traits | ||
*/ | ||
trait HasBannedAtScope | ||
{ | ||
/** | ||
* Boot the HasBannedAtScope trait for a model. | ||
* | ||
* @return void | ||
*/ | ||
public static function bootHasBannedAtScope() | ||
{ | ||
static::addGlobalScope(new BannedAtScope); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Laravel Ban. | ||
* | ||
* (c) Anton Komarev <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Cog\Ban\Traits; | ||
|
||
use Cog\Ban\Contracts\Ban as BanContract; | ||
|
||
/** | ||
* Class HasBansRelation. | ||
* | ||
* @package Cog\Ban\Traits | ||
*/ | ||
trait HasBansRelation | ||
{ | ||
/** | ||
* Entity Bans. | ||
* | ||
* @return \Illuminate\Database\Eloquent\Relations\MorphMany | ||
*/ | ||
public function bans() | ||
{ | ||
return $this->morphMany(app(BanContract::class), 'owned_by'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Laravel Ban. | ||
* | ||
* (c) Anton Komarev <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Cog\Ban\Tests\Stubs\Models; | ||
|
||
/** | ||
* Class UserWithBannedAtScopeApplied. | ||
* | ||
* @package Cog\Ban\Tests\Stubs\Models | ||
*/ | ||
class UserWithBannedAtScopeApplied extends User | ||
{ | ||
/** | ||
* Determine which BannedAtScope should be applied by default. | ||
* | ||
* @return bool | ||
*/ | ||
public function shouldApplyBannedAtScope() | ||
{ | ||
return true; | ||
} | ||
} |
Oops, something went wrong.