diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c88b3c..f4d4438 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,16 @@ All notable changes to `laravel-ban` will be documented in this file. +## [3.3.0] - 2018-09-16 + +### Added + +- ([#27](https://github.com/cybercog/laravel-ban/pull/27)) Add `isPermanent` & `isTemporary` ban checks + +### Fixed + +- ([#27](https://github.com/cybercog/laravel-ban/pull/27)) Stop trying to parse `null` value for `expired_at` as Carbon value + ## [3.2.0] - 2018-09-09 ### Added diff --git a/README.md b/README.md index 4d4a97b..bd6b139 100644 --- a/README.md +++ b/README.md @@ -195,6 +195,34 @@ $user->isNotBanned(); app(\Cog\Contracts\Ban\BanService::class)->deleteExpiredBans(); ``` +#### Determine if ban is permanent + +```php +$ban = $user->ban(); + +$ban->isPermanent(); // true +``` + +Or pass `null` value. + +```php +$ban = $user->ban([ + 'expired_at' => null, +]); + +$ban->isPermanent(); // true +``` + +#### Determine if ban is temporary + +```php +$ban = $user->ban([ + 'expired_at' => '2086-03-28 00:00:00', +]); + +$ban->isTemporary(); // true +``` + ### Scopes #### Get all models which are not banned diff --git a/contracts/Ban.php b/contracts/Ban.php index e0ffab4..182409c 100644 --- a/contracts/Ban.php +++ b/contracts/Ban.php @@ -11,9 +11,6 @@ namespace Cog\Contracts\Ban; -use Cog\Contracts\Ban\Bannable as BannableContract; -use Illuminate\Database\Eloquent\Builder; - /** * Interface Ban. * @@ -36,11 +33,16 @@ public function createdBy(); public function bannable(); /** - * Scope a query to only include bans by bannable model. + * Determine if Ban is permanent. + * + * @return bool + */ + public function isPermanent(); + + /** + * Determine if Ban is temporary. * - * @param \Illuminate\Database\Eloquent\Builder $query - * @param \Cog\Contracts\Ban\Bannable $bannable - * @return \Illuminate\Database\Eloquent\Builder + * @return bool */ - public function scopeWhereBannable(Builder $query, BannableContract $bannable); + public function isTemporary(); } diff --git a/src/Models/Ban.php b/src/Models/Ban.php index 7533faa..55e2ee2 100644 --- a/src/Models/Ban.php +++ b/src/Models/Ban.php @@ -57,16 +57,37 @@ class Ban extends Model implements BanContract * Expired timestamp mutator. * * @param \Carbon\Carbon|string $value + * @return void */ public function setExpiredAtAttribute($value) { - if (!$value instanceof Carbon) { + if (!is_null($value) && !$value instanceof Carbon) { $value = Carbon::parse($value); } $this->attributes['expired_at'] = $value; } + /** + * Determine if Ban is permanent. + * + * @return bool + */ + public function isPermanent() + { + return !isset($this->attributes['expired_at']) || is_null($this->attributes['expired_at']); + } + + /** + * Determine if Ban is temporary. + * + * @return bool + */ + public function isTemporary() + { + return !$this->isPermanent(); + } + /** * Entity responsible for ban. * @@ -97,8 +118,8 @@ public function bannable() public function scopeWhereBannable(Builder $query, BannableContract $bannable) { return $query->where([ - 'bannable_id' => $bannable->getKey(), 'bannable_type' => $bannable->getMorphClass(), + 'bannable_id' => $bannable->getKey(), ]); } } diff --git a/tests/Unit/Models/BanTest.php b/tests/Unit/Models/BanTest.php index ec18192..2815aeb 100644 --- a/tests/Unit/Models/BanTest.php +++ b/tests/Unit/Models/BanTest.php @@ -57,6 +57,16 @@ public function it_can_cast_expired_at() $this->assertInstanceOf(Carbon::class, $ban->expired_at); } + /** @test */ + public function it_not_modify_null_expired_at() + { + $ban = new Ban([ + 'expired_at' => null, + ]); + + $this->assertNull($ban->expired_at); + } + /** @test */ public function it_can_has_ban_creator() { @@ -134,4 +144,39 @@ public function it_can_scope_bannable_models() $this->assertCount(4, $bannableModels); } + + /** @test */ + public function it_can_check_if_ban_is_permanent() + { + $permanentBan = new Ban(); + $temporaryBan = new Ban([ + 'expired_at' => '2086-03-28 00:00:00', + ]); + + $this->assertTrue($permanentBan->isPermanent()); + $this->assertFalse($temporaryBan->isPermanent()); + } + + /** @test */ + public function it_can_check_if_ban_is_temporary() + { + $permanentBan = new Ban(); + $temporaryBan = new Ban([ + 'expired_at' => '2086-03-28 00:00:00', + ]); + + $this->assertFalse($permanentBan->isTemporary()); + $this->assertTrue($temporaryBan->isTemporary()); + } + + /** @test */ + public function it_can_check_if_ban_with_null_expired_at_is_permanent() + { + $permanentBan = new Ban([ + 'expired_at' => null, + ]); + + $this->assertTrue($permanentBan->isPermanent()); + $this->assertFalse($permanentBan->isTemporary()); + } }