Skip to content

Commit 98af96e

Browse files
author
Pe Ell
authored
Add expired at inverse flag (#23)
1 parent 6cd913e commit 98af96e

File tree

13 files changed

+796
-2
lines changed

13 files changed

+796
-2
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
All notable changes to `laravel-eloquent-flag` will be documented in this file.
44

5+
## [3.6.0] - 2017-01-14
6+
7+
### Added
8+
9+
- `expired_at` inverse timestamp flag added.
10+
11+
### Changed
12+
13+
- `is_expired` inverse boolean flag helpers implemented.
14+
515
## [3.5.0] - 2017-01-13
616

717
### Added
@@ -96,6 +106,7 @@ All notable changes to `laravel-eloquent-flag` will be documented in this file.
96106

97107
- `is_active` boolean flag added.
98108

109+
[3.6.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/3.5.0...3.6.0
99110
[3.5.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/3.4.0...3.5.0
100111
[3.4.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/3.3.0...3.4.0
101112
[3.3.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/3.2.0...3.3.0

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ Eloquent boolean & timestamp flagged attributes behavior. Enhance eloquent model
3131
| `HasApprovedFlag` | Classic | `is_approved` | Boolean | `HasApprovedAt` |
3232
| `HasClosedAt` | Inverse | `closed_at` | Timestamp | `HasClosedFlag` |
3333
| `HasClosedFlag` | Inverse | `is_closed` | Boolean | `HasClosedAt` |
34-
| `HasExpiredFlag` | Inverse | `is_expired` | Boolean | - |
34+
| `HasExpiredAt` | Inverse | `expired_at` | Timestamp | `HasExpiredFlag` |
35+
| `HasExpiredFlag` | Inverse | `is_expired` | Boolean | `HasExpiredAt` |
3536
| `HasKeptFlag` | Classic | `is_kept` | Boolean | - |
3637
| `HasPublishedAt` | Classic | `published_at` | Timestamp | `HasPublishedFlag` |
3738
| `HasPublishedFlag` | Classic | `is_published` | Boolean | `HasPublishedAt` |
@@ -531,6 +532,8 @@ Output will have all unkept models created earlier than 4 hours ago.
531532

532533
### Setup an expirable model
533534

535+
#### With boolean flag
536+
534537
```php
535538
<?php
536539

@@ -547,6 +550,24 @@ class Post extends Model
547550

548551
*Model must have boolean `is_expired` column in database table.*
549552

553+
#### With timestamp flag
554+
555+
```php
556+
<?php
557+
558+
namespace App\Models;
559+
560+
use Cog\Flag\Traits\Classic\HasExpiredAt;
561+
use Illuminate\Database\Eloquent\Model;
562+
563+
class Post extends Model
564+
{
565+
use HasExpiredAt;
566+
}
567+
```
568+
569+
*Model must have nullable timestamp `expired_at` column in database table.*
570+
550571
### Available functions
551572

552573
#### Get only not expired models
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Laravel Eloquent Flag.
5+
*
6+
* (c) Anton Komarev <a.komarev@cybercog.su>
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\Inverse;
13+
14+
use Carbon\Carbon;
15+
use Illuminate\Database\Eloquent\Builder;
16+
use Illuminate\Database\Eloquent\Model;
17+
use Illuminate\Database\Eloquent\Scope;
18+
19+
/**
20+
* Class ExpiredAtScope.
21+
*
22+
* @package Cog\Flag\Scopes\Inverse
23+
*/
24+
class ExpiredAtScope implements Scope
25+
{
26+
/**
27+
* All of the extensions to be added to the builder.
28+
*
29+
* @var array
30+
*/
31+
protected $extensions = ['Unexpire', 'Expire', 'WithExpired', 'WithoutExpired', 'OnlyExpired'];
32+
33+
/**
34+
* Apply the scope to a given Eloquent query builder.
35+
*
36+
* @param \Illuminate\Database\Eloquent\Builder $builder
37+
* @param \Illuminate\Database\Eloquent\Model $model
38+
* @return \Illuminate\Database\Eloquent\Builder
39+
*/
40+
public function apply(Builder $builder, Model $model)
41+
{
42+
return $builder->whereNull('expired_at');
43+
}
44+
45+
/**
46+
* Extend the query builder with the needed functions.
47+
*
48+
* @param \Illuminate\Database\Eloquent\Builder $builder
49+
* @return void
50+
*/
51+
public function extend(Builder $builder)
52+
{
53+
foreach ($this->extensions as $extension) {
54+
$this->{"add{$extension}"}($builder);
55+
}
56+
}
57+
58+
/**
59+
* Add the `unexpire` extension to the builder.
60+
*
61+
* @param \Illuminate\Database\Eloquent\Builder $builder
62+
* @return void
63+
*/
64+
protected function addUnexpire(Builder $builder)
65+
{
66+
$builder->macro('unexpire', function (Builder $builder) {
67+
$builder->withExpired();
68+
69+
return $builder->update(['expired_at' => null]);
70+
});
71+
}
72+
73+
/**
74+
* Add the `expire` extension to the builder.
75+
*
76+
* @param \Illuminate\Database\Eloquent\Builder $builder
77+
* @return void
78+
*/
79+
protected function addExpire(Builder $builder)
80+
{
81+
$builder->macro('expire', function (Builder $builder) {
82+
return $builder->update(['expired_at' => Carbon::now()]);
83+
});
84+
}
85+
86+
/**
87+
* Add the `withExpired` extension to the builder.
88+
*
89+
* @param \Illuminate\Database\Eloquent\Builder $builder
90+
* @return void
91+
*/
92+
protected function addWithExpired(Builder $builder)
93+
{
94+
$builder->macro('withExpired', function (Builder $builder) {
95+
return $builder->withoutGlobalScope($this);
96+
});
97+
}
98+
99+
/**
100+
* Add the `withoutExpired` extension to the builder.
101+
*
102+
* @param \Illuminate\Database\Eloquent\Builder $builder
103+
* @return void
104+
*/
105+
protected function addWithoutExpired(Builder $builder)
106+
{
107+
$builder->macro('withoutExpired', function (Builder $builder) {
108+
return $builder->withoutGlobalScope($this)->whereNull('expired_at');
109+
});
110+
}
111+
112+
/**
113+
* Add the `onlyExpired` extension to the builder.
114+
*
115+
* @param \Illuminate\Database\Eloquent\Builder $builder
116+
* @return void
117+
*/
118+
protected function addOnlyExpired(Builder $builder)
119+
{
120+
$builder->macro('onlyExpired', function (Builder $builder) {
121+
return $builder->withoutGlobalScope($this)->whereNotNull('expired_at');
122+
});
123+
}
124+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Laravel Eloquent Flag.
5+
*
6+
* (c) Anton Komarev <a.komarev@cybercog.su>
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\Traits\Inverse;
13+
14+
/**
15+
* Class HasExpiredAt.
16+
*
17+
* @package Cog\Flag\Traits\Inverse
18+
*/
19+
trait HasExpiredAt
20+
{
21+
use HasExpiredAtHelpers,
22+
HasExpiredAtScope;
23+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Laravel Eloquent Flag.
5+
*
6+
* (c) Anton Komarev <a.komarev@cybercog.su>
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\Traits\Inverse;
13+
14+
use Carbon\Carbon;
15+
16+
/**
17+
* Class HasExpiredAtHelpers.
18+
*
19+
* @package Cog\Flag\Traits\Inverse
20+
*/
21+
trait HasExpiredAtHelpers
22+
{
23+
/**
24+
* Set expired flag.
25+
*
26+
* @return static
27+
*/
28+
public function setExpiredFlag()
29+
{
30+
$this->expired_at = Carbon::now();
31+
32+
return $this;
33+
}
34+
35+
/**
36+
* Unset expired flag.
37+
*
38+
* @return static
39+
*/
40+
public function unsetExpiredFlag()
41+
{
42+
$this->expired_at = null;
43+
44+
return $this;
45+
}
46+
47+
/**
48+
* If entity is expired.
49+
*
50+
* @return bool
51+
*/
52+
public function isExpired()
53+
{
54+
return !is_null($this->expired_at);
55+
}
56+
57+
/**
58+
* If entity is opened.
59+
*
60+
* @return bool
61+
*/
62+
public function isUnexpired()
63+
{
64+
return !$this->isExpired();
65+
}
66+
67+
/**
68+
* Mark entity as expired.
69+
*
70+
* @return void
71+
*/
72+
public function expire()
73+
{
74+
$this->setExpiredFlag()->save();
75+
76+
// :TODO: Fire an event here
77+
}
78+
79+
/**
80+
* Mark entity as opened.
81+
*
82+
* @return void
83+
*/
84+
public function unexpire()
85+
{
86+
$this->unsetExpiredFlag()->save();
87+
88+
// :TODO: Fire an event here
89+
}
90+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Laravel Eloquent Flag.
5+
*
6+
* (c) Anton Komarev <a.komarev@cybercog.su>
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\Traits\Inverse;
13+
14+
use Cog\Flag\Scopes\Inverse\ExpiredAtScope;
15+
16+
/**
17+
* Class HasExpiredAtScope.
18+
*
19+
* @package Cog\Flag\Traits\Inverse
20+
*/
21+
trait HasExpiredAtScope
22+
{
23+
/**
24+
* Boot the HasExpiredAtScope trait for a model.
25+
*
26+
* @return void
27+
*/
28+
public static function bootHasExpiredAtScope()
29+
{
30+
static::addGlobalScope(new ExpiredAtScope);
31+
}
32+
}

0 commit comments

Comments
 (0)