Skip to content

Commit 6cd913e

Browse files
author
Pe Ell
authored
Add Approved At flag (#21)
1 parent 0e83bd0 commit 6cd913e

13 files changed

+800
-3
lines changed

CHANGELOG.md

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

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

5+
## [3.5.0] - 2017-01-13
6+
7+
### Added
8+
9+
- `approved_at` classic timestamp flag added.
10+
11+
### Changed
12+
13+
- `is_approved` classic boolean flag helpers implemented.
14+
515
## [3.4.0] - 2017-01-13
616

717
### Added
818

919
- `closed_at` inverse timestamp flag added.
20+
21+
### Changed
22+
1023
- `is_closed` inverse boolean flag helpers added.
1124

1225
## [3.3.0] - 2017-01-12
@@ -83,6 +96,7 @@ All notable changes to `laravel-eloquent-flag` will be documented in this file.
8396

8497
- `is_active` boolean flag added.
8598

99+
[3.5.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/3.4.0...3.5.0
86100
[3.4.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/3.3.0...3.4.0
87101
[3.3.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/3.2.0...3.3.0
88102
[3.2.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/3.1.0...3.2.0

README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ Eloquent boolean & timestamp flagged attributes behavior. Enhance eloquent model
2727
| `HasAcceptedAt` | Classic | `accepted_at` | Timestamp | `HasAcceptedFlag` |
2828
| `HasAcceptedFlag` | Classic | `is_accepted` | Boolean | `HasAcceptedAt` |
2929
| `HasActiveFlag` | Classic | `is_active` | Boolean | - |
30-
| `HasApprovedFlag` | Classic | `is_approved` | Boolean | - |
30+
| `HasApprovedAt` | Classic | `approved_at` | Timestamp | `HasApprovedFlag` |
31+
| `HasApprovedFlag` | Classic | `is_approved` | Boolean | `HasApprovedAt` |
3132
| `HasClosedAt` | Inverse | `closed_at` | Timestamp | `HasClosedFlag` |
3233
| `HasClosedFlag` | Inverse | `is_closed` | Boolean | `HasClosedAt` |
3334
| `HasExpiredFlag` | Inverse | `is_expired` | Boolean | - |
@@ -233,6 +234,8 @@ Post::where('id', 4)->reject();
233234

234235
### Setup an approvable model
235236

237+
#### With boolean flag
238+
236239
```php
237240
<?php
238241

@@ -249,6 +252,24 @@ class Post extends Model
249252

250253
*Model must have boolean `is_approved` column in database table.*
251254

255+
#### With timestamp flag
256+
257+
```php
258+
<?php
259+
260+
namespace App\Models;
261+
262+
use Cog\Flag\Traits\Classic\HasApprovedAt;
263+
use Illuminate\Database\Eloquent\Model;
264+
265+
class Post extends Model
266+
{
267+
use HasApprovedAt;
268+
}
269+
```
270+
271+
*Model must have nullable timestamp `approved_at` column in database table.*
272+
252273
### Available functions
253274

254275
#### Get only approved models
@@ -651,7 +672,7 @@ If you discover any security related issues, please email [email protected]
651672
- [Anton Komarev](https://github.com/a-komarev)
652673
- [All Contributors](../../contributors)
653674

654-
## Changelog
675+
## Change log
655676

656677
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
657678

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 <[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\Classic;
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 ApprovedAtScope.
21+
*
22+
* @package Cog\Flag\Scopes\Classic
23+
*/
24+
class ApprovedAtScope implements Scope
25+
{
26+
/**
27+
* All of the extensions to be added to the builder.
28+
*
29+
* @var array
30+
*/
31+
protected $extensions = ['Approve', 'Disapprove', 'WithDisapproved', 'WithoutDisapproved', 'OnlyDisapproved'];
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->whereNotNull('approved_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 `approve` extension to the builder.
60+
*
61+
* @param \Illuminate\Database\Eloquent\Builder $builder
62+
* @return void
63+
*/
64+
protected function addApprove(Builder $builder)
65+
{
66+
$builder->macro('approve', function (Builder $builder) {
67+
$builder->withDisapproved();
68+
69+
return $builder->update(['approved_at' => Carbon::now()]);
70+
});
71+
}
72+
73+
/**
74+
* Add the `disapprove` extension to the builder.
75+
*
76+
* @param \Illuminate\Database\Eloquent\Builder $builder
77+
* @return void
78+
*/
79+
protected function addDisapprove(Builder $builder)
80+
{
81+
$builder->macro('disapprove', function (Builder $builder) {
82+
return $builder->update(['approved_at' => null]);
83+
});
84+
}
85+
86+
/**
87+
* Add the `withDisapproved` extension to the builder.
88+
*
89+
* @param \Illuminate\Database\Eloquent\Builder $builder
90+
* @return void
91+
*/
92+
protected function addWithDisapproved(Builder $builder)
93+
{
94+
$builder->macro('withDisapproved', function (Builder $builder) {
95+
return $builder->withoutGlobalScope($this);
96+
});
97+
}
98+
99+
/**
100+
* Add the `withoutDisapproved` extension to the builder.
101+
*
102+
* @param \Illuminate\Database\Eloquent\Builder $builder
103+
* @return void
104+
*/
105+
protected function addWithoutDisapproved(Builder $builder)
106+
{
107+
$builder->macro('withoutDisapproved', function (Builder $builder) {
108+
return $builder->withoutGlobalScope($this)->whereNotNull('approved_at');
109+
});
110+
}
111+
112+
/**
113+
* Add the `onlyDisapproved` extension to the builder.
114+
*
115+
* @param \Illuminate\Database\Eloquent\Builder $builder
116+
* @return void
117+
*/
118+
protected function addOnlyDisapproved(Builder $builder)
119+
{
120+
$builder->macro('onlyDisapproved', function (Builder $builder) {
121+
return $builder->withoutGlobalScope($this)->whereNull('approved_at');
122+
});
123+
}
124+
}

src/Traits/Classic/HasApprovedAt.php

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 <[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\Traits\Classic;
13+
14+
/**
15+
* Class HasApprovedAt.
16+
*
17+
* @package Cog\Flag\Traits\Classic
18+
*/
19+
trait HasApprovedAt
20+
{
21+
use HasApprovedAtHelpers,
22+
HasApprovedAtScope;
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 <[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\Traits\Classic;
13+
14+
use Carbon\Carbon;
15+
16+
/**
17+
* Class HasApprovedAtHelpers.
18+
*
19+
* @package Cog\Flag\Traits\Classic
20+
*/
21+
trait HasApprovedAtHelpers
22+
{
23+
/**
24+
* Set approved flag.
25+
*
26+
* @return static
27+
*/
28+
public function setApprovedFlag()
29+
{
30+
$this->approved_at = Carbon::now();
31+
32+
return $this;
33+
}
34+
35+
/**
36+
* Unset approved flag.
37+
*
38+
* @return static
39+
*/
40+
public function unsetApprovedFlag()
41+
{
42+
$this->approved_at = null;
43+
44+
return $this;
45+
}
46+
47+
/**
48+
* If entity is approved.
49+
*
50+
* @return bool
51+
*/
52+
public function isApproved()
53+
{
54+
return !is_null($this->approved_at);
55+
}
56+
57+
/**
58+
* If entity is disapproved.
59+
*
60+
* @return bool
61+
*/
62+
public function isDisapproved()
63+
{
64+
return !$this->isApproved();
65+
}
66+
67+
/**
68+
* Mark entity as verified.
69+
*
70+
* @return void
71+
*/
72+
public function approve()
73+
{
74+
$this->setApprovedFlag()->save();
75+
76+
// :TODO: Fire an event here
77+
}
78+
79+
/**
80+
* Mark entity as rejected.
81+
*
82+
* @return void
83+
*/
84+
public function disapprove()
85+
{
86+
$this->unsetApprovedFlag()->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 <[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\Traits\Classic;
13+
14+
use Cog\Flag\Scopes\Classic\ApprovedAtScope;
15+
16+
/**
17+
* Class HasApprovedAtScope.
18+
*
19+
* @package Cog\Flag\Traits\Classic
20+
*/
21+
trait HasApprovedAtScope
22+
{
23+
/**
24+
* Boot the HasApprovedAtScope trait for a model.
25+
*
26+
* @return void
27+
*/
28+
public static function bootHasApprovedAtScope()
29+
{
30+
static::addGlobalScope(new ApprovedAtScope);
31+
}
32+
}

0 commit comments

Comments
 (0)