Skip to content

Commit 731cd11

Browse files
author
Pe Ell
authored
Merge pull request #5 from cybercog/feature/is-verified
Add verified flag
2 parents 9030aab + 78b4807 commit 731cd11

File tree

8 files changed

+454
-9
lines changed

8 files changed

+454
-9
lines changed

CHANGELOG.md

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

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

5+
## 1.4.0 - 2016-12-26
6+
7+
- `is_verified` flag added.
8+
59
## 1.3.0 - 2016-12-14
610

711
- `is_accepted` flag added.

README.md

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,28 @@ Eloquent flagged attributes behavior. Add commonly used flags to models very qui
99

1010
![cog-laravel-eloquent-flag](https://cloud.githubusercontent.com/assets/1849174/21166452/b1bbf3e8-c1b6-11e6-8f06-468828402ebe.png)
1111

12-
## How it works
13-
14-
Eloquent Flag is an easy way to add flagged attributes to eloquent models. All flags has their own trait which adds global scopes to desired entity.
12+
## Features
1513

16-
The main logic of the flags: If flag is `false` - entity should be hidden from the query results. Omitted entities could be retrieved by using special global scope methods.
14+
- Designed to work with Laravel Eloquent models
15+
- Each model can has as many flags as required
16+
- Each flag adds global query scopes to models
17+
- Covered with unit tests
1718

1819
## Available flags list
1920

20-
- `is_accepted`
21-
- `is_active`
22-
- `is_published`
23-
- `is_kept`
21+
| Trait name | Database columns | Flag type |
22+
| ---------- | ---------------- | --------- |
23+
| `HasAcceptedFlag` | `is_accepted` | Boolean |
24+
| `HasActiveFlag` | `is_active` | Boolean |
25+
| `HasKeptFlag` | `is_kept` | Boolean |
26+
| `HasPublishedFlag` | `is_published` | Boolean |
27+
| `HasVerifiedFlag` | `is_verified` | Boolean |
28+
29+
## How it works
30+
31+
Eloquent Flag is an easy way to add flagged attributes to eloquent models. All flags has their own trait which adds global scopes to desired entity.
32+
33+
The main logic of the flags: If flag is `false` - entity should be hidden from the query results. Omitted entities could be retrieved by using special global scope methods.
2434

2535
## Installation
2636

@@ -32,6 +42,8 @@ composer require cybercog/laravel-eloquent-flag
3242

3343
And then include the service provider within `app/config/app.php`.
3444

45+
*// Service provider not using yet. Will be used to boot console commands in future.*
46+
3547
```php
3648
'providers' => [
3749
Cog\Flag\Providers\FlagServiceProvider::class,
@@ -193,6 +205,57 @@ Post::where('id', 4)->publish();
193205
Post::where('id', 4)->unpublish();
194206
```
195207
208+
### Setup a verifiable model
209+
210+
```php
211+
<?php
212+
213+
namespace App\Models;
214+
215+
use Cog\Flag\Traits\HasVerifiedFlag;
216+
use Illuminate\Database\Eloquent\Model;
217+
218+
class Post extends Model
219+
{
220+
use HasVerifiedFlag;
221+
}
222+
```
223+
224+
*Model must have boolean `is_verified` column in database table.*
225+
226+
### Available functions
227+
228+
#### Get only verified models
229+
230+
```shell
231+
Post::all();
232+
Post::withoutUnverified();
233+
```
234+
235+
#### Get only unverified models
236+
237+
```shell
238+
Post::onlyUnverified();
239+
```
240+
241+
#### Get verified + unverified models
242+
243+
```shell
244+
Post::withUnverified();
245+
```
246+
247+
#### Verify model
248+
249+
```shell
250+
Post::where('id', 4)->verify();
251+
```
252+
253+
#### Unverify model
254+
255+
```shell
256+
Post::where('id', 4)->unverify();
257+
```
258+
196259
### Setup a keepable model
197260
198261
Keep functionality required when you are trying to attach related models before parent one isn't persisted in application.
@@ -304,7 +367,9 @@ If you discover any security related issues, please email [email protected] in
304367
305368
## License
306369
307-
Please see [License](LICENSE) file for more information.
370+
- `Laravel Eloquent Flag` package is open-sourced software licensed under the [MIT license](LICENSE).
371+
- `Check Mark` image licensed under [Creative Commons 3.0](https://creativecommons.org/licenses/by/3.0/us/) by Kimmi Studio.
372+
- `Clock Check` image licensed under [Creative Commons 3.0](https://creativecommons.org/licenses/by/3.0/us/) by Harsha Rai.
308373
309374
## About CyberCog
310375

src/Scopes/VerifiedFlagScope.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Laravel Eloquent Flag.
5+
*
6+
* (c) CyberCog <[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;
13+
14+
use Illuminate\Database\Eloquent\Model;
15+
use Illuminate\Database\Eloquent\Scope;
16+
use Illuminate\Database\Eloquent\Builder;
17+
18+
/**
19+
* Class VerifiedFlagScope.
20+
*
21+
* @package Cog\Flag\Scopes
22+
*/
23+
class VerifiedFlagScope implements Scope
24+
{
25+
/**
26+
* All of the extensions to be added to the builder.
27+
*
28+
* @var array
29+
*/
30+
protected $extensions = ['Verify', 'Unverify', 'WithUnverified', 'WithoutUnverified', 'OnlyUnverified'];
31+
32+
/**
33+
* Apply the scope to a given Eloquent query builder.
34+
*
35+
* @param \Illuminate\Database\Eloquent\Builder $builder
36+
* @param \Illuminate\Database\Eloquent\Model $model
37+
* @return \Illuminate\Database\Eloquent\Builder
38+
*/
39+
public function apply(Builder $builder, Model $model)
40+
{
41+
return $builder->where('is_verified', 1);
42+
}
43+
44+
/**
45+
* Extend the query builder with the needed functions.
46+
*
47+
* @param \Illuminate\Database\Eloquent\Builder $builder
48+
* @return void
49+
*/
50+
public function extend(Builder $builder)
51+
{
52+
foreach ($this->extensions as $extension) {
53+
$this->{"add{$extension}"}($builder);
54+
}
55+
}
56+
57+
/**
58+
* Add the `verify` extension to the builder.
59+
*
60+
* @param \Illuminate\Database\Eloquent\Builder $builder
61+
* @return void
62+
*/
63+
protected function addVerify(Builder $builder)
64+
{
65+
$builder->macro('verify', function (Builder $builder) {
66+
$builder->withUnverified();
67+
68+
return $builder->update(['is_verified' => 1]);
69+
});
70+
}
71+
72+
/**
73+
* Add the `unverify` extension to the builder.
74+
*
75+
* @param \Illuminate\Database\Eloquent\Builder $builder
76+
* @return void
77+
*/
78+
protected function addUnverify(Builder $builder)
79+
{
80+
$builder->macro('unverify', function (Builder $builder) {
81+
return $builder->update(['is_verified' => 0]);
82+
});
83+
}
84+
85+
/**
86+
* Add the `withUnverified` extension to the builder.
87+
*
88+
* @param \Illuminate\Database\Eloquent\Builder $builder
89+
* @return void
90+
*/
91+
protected function addWithUnverified(Builder $builder)
92+
{
93+
$builder->macro('withUnverified', function (Builder $builder) {
94+
return $builder->withoutGlobalScope($this);
95+
});
96+
}
97+
98+
/**
99+
* Add the `withoutUnverified` extension to the builder.
100+
*
101+
* @param \Illuminate\Database\Eloquent\Builder $builder
102+
* @return void
103+
*/
104+
protected function addWithoutUnverified(Builder $builder)
105+
{
106+
$builder->macro('withoutUnverified', function (Builder $builder) {
107+
return $builder->withoutGlobalScope($this)->where('is_verified', 1);
108+
});
109+
}
110+
111+
/**
112+
* Add the `onlyUnverified` extension to the builder.
113+
*
114+
* @param \Illuminate\Database\Eloquent\Builder $builder
115+
* @return void
116+
*/
117+
protected function addOnlyUnverified(Builder $builder)
118+
{
119+
$builder->macro('onlyUnverified', function (Builder $builder) {
120+
return $builder->withoutGlobalScope($this)->where('is_verified', 0);
121+
});
122+
}
123+
}

src/Traits/HasVerifiedFlag.php

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) CyberCog <[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;
13+
14+
use Cog\Flag\Scopes\VerifiedFlagScope;
15+
16+
/**
17+
* Class HasVerifiedFlag.
18+
*
19+
* @package Cog\Flag\Traits
20+
*/
21+
trait HasVerifiedFlag
22+
{
23+
/**
24+
* Boot the HasVerifiedTrait for a model.
25+
*
26+
* @return void
27+
*/
28+
public static function bootHasVerifiedFlag()
29+
{
30+
static::addGlobalScope(new VerifiedFlagScope);
31+
}
32+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Laravel Eloquent Flag.
5+
*
6+
* (c) CyberCog <[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+
$factory->define(\Cog\Flag\Tests\Stubs\Models\EntityWithVerifiedFlag::class, function (\Faker\Generator $faker) {
13+
return [
14+
'name' => $faker->word,
15+
'is_verified' => true,
16+
];
17+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Laravel Eloquent Flag.
5+
*
6+
* (c) CyberCog <[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+
use Illuminate\Database\Migrations\Migration;
13+
14+
/**
15+
* Class CreateEntityWithVerifiedFlagTable.
16+
*/
17+
class CreateEntityWithVerifiedFlagTable extends Migration
18+
{
19+
/**
20+
* Run the migrations.
21+
*
22+
* @return void
23+
*/
24+
public function up()
25+
{
26+
Schema::create('entity_with_verified_flag', function ($table) {
27+
$table->increments('id');
28+
$table->string('name');
29+
$table->boolean('is_verified');
30+
$table->timestamps();
31+
});
32+
}
33+
34+
/**
35+
* Reverse the migrations.
36+
*
37+
* @return void
38+
*/
39+
public function down()
40+
{
41+
Schema::drop('entity_with_verified_flag');
42+
}
43+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Laravel Eloquent Flag.
5+
*
6+
* (c) CyberCog <[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\Tests\Stubs\Models;
13+
14+
use Cog\Flag\Traits\HasVerifiedFlag;
15+
use Illuminate\Database\Eloquent\Model;
16+
17+
/**
18+
* Class EntityWithVerifiedFlag.
19+
*
20+
* @package Cog\Flag\Tests\Stubs\Models
21+
*/
22+
class EntityWithVerifiedFlag extends Model
23+
{
24+
use HasVerifiedFlag;
25+
26+
/**
27+
* The table associated with the model.
28+
*
29+
* @var string
30+
*/
31+
protected $table = 'entity_with_verified_flag';
32+
33+
/**
34+
* The attributes that are mass assignable.
35+
*
36+
* @var array
37+
*/
38+
protected $fillable = [
39+
'name',
40+
];
41+
42+
/**
43+
* The attributes that should be cast to native types.
44+
*
45+
* @var array
46+
*/
47+
protected $casts = [
48+
'is_verified' => 'bool',
49+
];
50+
}

0 commit comments

Comments
 (0)