Skip to content

Commit c05b9d9

Browse files
author
Pe Ell
authored
Merge pull request #6 from cybercog/feature/is-approved-flag
Add Approved flag
2 parents 731cd11 + f048762 commit c05b9d9

19 files changed

+487
-18
lines changed

CHANGELOG.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,26 @@
22

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

5+
## 1.5.0 - 2016-12-31
6+
7+
- `is_approved` boolean flag added.
8+
59
## 1.4.0 - 2016-12-26
610

7-
- `is_verified` flag added.
11+
- `is_verified` boolean flag added.
812

913
## 1.3.0 - 2016-12-14
1014

11-
- `is_accepted` flag added.
15+
- `is_accepted` boolean flag added.
1216

1317
## 1.2.0 - 2016-12-10
1418

15-
- `is_kept` flag added.
19+
- `is_kept` boolean flag added.
1620

1721
## 1.1.0 - 2016-09-25
1822

19-
- `is_published` flag added.
23+
- `is_published` boolean flag added.
2024

2125
## 1.0.0 - 2016-09-25
2226

23-
- Initial release with only `is_active` flag.
27+
- `is_active` boolean flag added.

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Eloquent flagged attributes behavior. Add commonly used flags to models very qui
2222
| ---------- | ---------------- | --------- |
2323
| `HasAcceptedFlag` | `is_accepted` | Boolean |
2424
| `HasActiveFlag` | `is_active` | Boolean |
25+
| `HasApprovedFlag` | `is_approved` | Boolean |
2526
| `HasKeptFlag` | `is_kept` | Boolean |
2627
| `HasPublishedFlag` | `is_published` | Boolean |
2728
| `HasVerifiedFlag` | `is_verified` | Boolean |
@@ -154,6 +155,57 @@ Post::where('id', 4)->accept();
154155
Post::where('id', 4)->unaccept();
155156
```
156157
158+
### Setup an approvable model
159+
160+
```php
161+
<?php
162+
163+
namespace App\Models;
164+
165+
use Cog\Flag\Traits\HasApprovedFlag;
166+
use Illuminate\Database\Eloquent\Model;
167+
168+
class Post extends Model
169+
{
170+
use HasApprovedFlag;
171+
}
172+
```
173+
174+
*Model must have boolean `is_approved` column in database table.*
175+
176+
### Available functions
177+
178+
#### Get only approved models
179+
180+
```shell
181+
Post::all();
182+
Post::withoutUnapproved();
183+
```
184+
185+
#### Get only unapproved models
186+
187+
```shell
188+
Post::onlyUnapproved();
189+
```
190+
191+
#### Get approved + unapproved models
192+
193+
```shell
194+
Post::withUnapproved();
195+
```
196+
197+
#### Approve model
198+
199+
```shell
200+
Post::where('id', 4)->approve();
201+
```
202+
203+
#### Unapprove model
204+
205+
```shell
206+
Post::where('id', 4)->unapprove();
207+
```
208+
157209
### Setup a publishable model
158210
159211
```php
@@ -365,6 +417,8 @@ If you discover any security related issues, please email [email protected] in
365417
366418
*Not found.*
367419
420+
*Feel free to add more alternatives as Pull Request.*
421+
368422
## License
369423
370424
- `Laravel Eloquent Flag` package is open-sourced software licensed under the [MIT license](LICENSE).

composer.json

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,32 @@
11
{
22
"name": "cybercog/laravel-eloquent-flag",
33
"description": "Laravel Eloquent flagged attributes behavior",
4+
"type": "library",
5+
"license": "MIT",
6+
"keywords": [
7+
"cybercog",
8+
"cog",
9+
"laravel",
10+
"trait",
11+
"scopes",
12+
"flag",
13+
"boolean",
14+
"timestamp",
15+
"accepted",
16+
"unaccepted",
17+
"active",
18+
"activated",
19+
"deactivated",
20+
"kept",
21+
"unkept",
22+
"published",
23+
"unpublished",
24+
"verified",
25+
"unverified",
26+
"persisted",
27+
"approved",
28+
"unapproved"
29+
],
430
"authors": [
531
{
632
"name": "Anton Komarev",
@@ -9,11 +35,17 @@
935
"role": "Developer"
1036
}
1137
],
12-
"license": "MIT",
13-
"type": "library",
38+
"homepage": "https://github.com/cybercog/laravel-eloquent-flag",
39+
"support": {
40+
"email": "[email protected]",
41+
"issues": "https://github.com/cybercog/laravel-eloquent-flag/issues",
42+
"wiki": "https://github.com/cybercog/laravel-eloquent-flag/wiki",
43+
"source": "https://github.com/cybercog/laravel-eloquent-flag",
44+
"docs": "https://github.com/cybercog/laravel-eloquent-flag/wiki"
45+
},
1446
"require": {
1547
"php": "^5.6|^7.0",
16-
"illuminate/database": "~5.2.0|~5.3.0"
48+
"illuminate/database": "~5.2.0|~5.3.0|~5.4.0"
1749
},
1850
"require-dev": {
1951
"friendsofphp/php-cs-fixer": "^1.11",
@@ -30,5 +62,8 @@
3062
"psr-4": {
3163
"Cog\\Flag\\Tests\\": "tests/"
3264
}
65+
},
66+
"config": {
67+
"sort-packages": true
3368
}
3469
}

src/Scopes/ApprovedFlagScope.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 ApprovedFlagScope.
20+
*
21+
* @package Cog\Flag\Scopes
22+
*/
23+
class ApprovedFlagScope implements Scope
24+
{
25+
/**
26+
* All of the extensions to be added to the builder.
27+
*
28+
* @var array
29+
*/
30+
protected $extensions = ['Approve', 'Unapprove', 'WithUnapproved', 'WithoutUnapproved', 'OnlyUnapproved'];
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_approved', 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 `approve` extension to the builder.
59+
*
60+
* @param \Illuminate\Database\Eloquent\Builder $builder
61+
* @return void
62+
*/
63+
protected function addApprove(Builder $builder)
64+
{
65+
$builder->macro('approve', function (Builder $builder) {
66+
$builder->withUnapproved();
67+
68+
return $builder->update(['is_approved' => 1]);
69+
});
70+
}
71+
72+
/**
73+
* Add the `unapprove` extension to the builder.
74+
*
75+
* @param \Illuminate\Database\Eloquent\Builder $builder
76+
* @return void
77+
*/
78+
protected function addUnapprove(Builder $builder)
79+
{
80+
$builder->macro('unapprove', function (Builder $builder) {
81+
return $builder->update(['is_approved' => 0]);
82+
});
83+
}
84+
85+
/**
86+
* Add the `withUnapproved` extension to the builder.
87+
*
88+
* @param \Illuminate\Database\Eloquent\Builder $builder
89+
* @return void
90+
*/
91+
protected function addWithUnapproved(Builder $builder)
92+
{
93+
$builder->macro('withUnapproved', function (Builder $builder) {
94+
return $builder->withoutGlobalScope($this);
95+
});
96+
}
97+
98+
/**
99+
* Add the `withoutUnapproved` extension to the builder.
100+
*
101+
* @param \Illuminate\Database\Eloquent\Builder $builder
102+
* @return void
103+
*/
104+
protected function addWithoutUnapproved(Builder $builder)
105+
{
106+
$builder->macro('withoutUnapproved', function (Builder $builder) {
107+
return $builder->withoutGlobalScope($this)->where('is_approved', 1);
108+
});
109+
}
110+
111+
/**
112+
* Add the `onlyUnapproved` extension to the builder.
113+
*
114+
* @param \Illuminate\Database\Eloquent\Builder $builder
115+
* @return void
116+
*/
117+
protected function addOnlyUnapproved(Builder $builder)
118+
{
119+
$builder->macro('onlyUnapproved', function (Builder $builder) {
120+
return $builder->withoutGlobalScope($this)->where('is_approved', 0);
121+
});
122+
}
123+
}

src/Traits/HasAcceptedFlag.php

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

src/Traits/HasActiveFlag.php

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

src/Traits/HasApprovedFlag.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\ApprovedFlagScope;
15+
16+
/**
17+
* Class HasApprovedFlag.
18+
*
19+
* @package Cog\Flag\Traits
20+
*/
21+
trait HasApprovedFlag
22+
{
23+
/**
24+
* Boot the HasApprovedFlag trait for a model.
25+
*
26+
* @return void
27+
*/
28+
public static function bootHasApprovedFlag()
29+
{
30+
static::addGlobalScope(new ApprovedFlagScope);
31+
}
32+
}

src/Traits/HasKeptFlag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
trait HasKeptFlag
2424
{
2525
/**
26-
* Boot the has kept flag trait for a model.
26+
* Boot the HasKeptFlag trait for a model.
2727
*
2828
* @return void
2929
*/

src/Traits/HasPublishedFlag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
trait HasPublishedFlag
2222
{
2323
/**
24-
* Boot the has published flag trait for a model.
24+
* Boot the HasPublishedFlag trait for a model.
2525
*
2626
* @return void
2727
*/
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\EntityWithApprovedFlag::class, function (\Faker\Generator $faker) {
13+
return [
14+
'name' => $faker->word,
15+
'is_approved' => true,
16+
];
17+
});

0 commit comments

Comments
 (0)