Skip to content

Commit 0e20cd6

Browse files
author
Pe Ell
authored
Add verified timestamp classic flag (#19)
1 parent 062da6a commit 0e20cd6

16 files changed

+794
-5
lines changed

CHANGELOG.md

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

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

5+
## 3.3.0 - 2017-01-12
6+
7+
### Added
8+
9+
- `verified_at` classic timestamp flag added.
10+
511
## 3.2.0 - 2017-01-12
612

713
### Added

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ Eloquent boolean & timestamp flagged attributes behavior. Enhance eloquent model
3333
| `HasKeptFlag` | Classic | `is_kept` | Boolean | - |
3434
| `HasPublishedAt` | Classic | `published_at` | Timestamp | `HasPublishedFlag` |
3535
| `HasPublishedFlag` | Classic | `is_published` | Boolean | `HasPublishedAt` |
36-
| `HasVerifiedFlag` | Classic | `is_verified` | Boolean | - |
36+
| `HasVerifiedAt` | Classic | `verified_at` | Timestamp | `HasVerifiedFlag` |
37+
| `HasVerifiedFlag` | Classic | `is_verified` | Boolean | `HasVerifiedAt` |
3738

3839
Any entity can has more than one flag at the same time. If flags can't work for the same entity simultaneously they are listed in `Conflict` column.
3940

@@ -353,6 +354,8 @@ Post::where('id', 4)->unpublish();
353354

354355
### Setup a verifiable model
355356

357+
#### With boolean flag
358+
356359
```php
357360
<?php
358361

@@ -369,6 +372,24 @@ class Post extends Model
369372

370373
*Model must have boolean `is_verified` column in database table.*
371374

375+
#### With timestamp flag
376+
377+
```php
378+
<?php
379+
380+
namespace App\Models;
381+
382+
use Cog\Flag\Traits\Classic\HasVerifiedAt;
383+
use Illuminate\Database\Eloquent\Model;
384+
385+
class Post extends Model
386+
{
387+
use HasVerifiedAt;
388+
}
389+
```
390+
391+
*Model must have nullable timestamp `verified_at` column in database table.*
392+
372393
### Available functions
373394

374395
#### Get only verified 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 <[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 VerifiedAtScope.
21+
*
22+
* @package Cog\Flag\Scopes\Classic
23+
*/
24+
class VerifiedAtScope implements Scope
25+
{
26+
/**
27+
* All of the extensions to be added to the builder.
28+
*
29+
* @var array
30+
*/
31+
protected $extensions = ['Verify', 'Unverify', 'WithUnverified', 'WithoutUnverified', 'OnlyUnverified'];
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('verified_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 `verify` extension to the builder.
60+
*
61+
* @param \Illuminate\Database\Eloquent\Builder $builder
62+
* @return void
63+
*/
64+
protected function addVerify(Builder $builder)
65+
{
66+
$builder->macro('verify', function (Builder $builder) {
67+
$builder->withUnverified();
68+
69+
return $builder->update(['verified_at' => Carbon::now()]);
70+
});
71+
}
72+
73+
/**
74+
* Add the `unverify` extension to the builder.
75+
*
76+
* @param \Illuminate\Database\Eloquent\Builder $builder
77+
* @return void
78+
*/
79+
protected function addUnverify(Builder $builder)
80+
{
81+
$builder->macro('unverify', function (Builder $builder) {
82+
return $builder->update(['verified_at' => null]);
83+
});
84+
}
85+
86+
/**
87+
* Add the `withUnverified` extension to the builder.
88+
*
89+
* @param \Illuminate\Database\Eloquent\Builder $builder
90+
* @return void
91+
*/
92+
protected function addWithUnverified(Builder $builder)
93+
{
94+
$builder->macro('withUnverified', function (Builder $builder) {
95+
return $builder->withoutGlobalScope($this);
96+
});
97+
}
98+
99+
/**
100+
* Add the `withoutUnverified` extension to the builder.
101+
*
102+
* @param \Illuminate\Database\Eloquent\Builder $builder
103+
* @return void
104+
*/
105+
protected function addWithoutUnverified(Builder $builder)
106+
{
107+
$builder->macro('withoutUnverified', function (Builder $builder) {
108+
return $builder->withoutGlobalScope($this)->whereNotNull('verified_at');
109+
});
110+
}
111+
112+
/**
113+
* Add the `onlyUnverified` extension to the builder.
114+
*
115+
* @param \Illuminate\Database\Eloquent\Builder $builder
116+
* @return void
117+
*/
118+
protected function addOnlyUnverified(Builder $builder)
119+
{
120+
$builder->macro('onlyUnverified', function (Builder $builder) {
121+
return $builder->withoutGlobalScope($this)->whereNull('verified_at');
122+
});
123+
}
124+
}

src/Traits/Classic/HasActiveFlagScope.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
trait HasActiveFlagScope
2222
{
2323
/**
24-
* Boot the HasKeptFlagScope trait for a model.
24+
* Boot the HasActiveFlagScope trait for a model.
2525
*
2626
* @return void
2727
*/

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

0 commit comments

Comments
 (0)