Skip to content

Commit 543ea15

Browse files
zagreusinozAnton Komarev
authored andcommitted
Added additional flags (#32)
* Added drafted flag * Added archived flag
1 parent 6747c1b commit 543ea15

40 files changed

+2373
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ vendor/
44
composer.lock
55
composer.phar
66
phpunit.xml
7+
.idea/

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Eloquent boolean & timestamp flagged attributes behavior. Enhance eloquent model
2323
- [Contributing](#contributing)
2424
- [Testing](#testing)
2525
- [Security](#security)
26-
- [Contributors](#contributors)
26+
- [Credits](#credits)
2727
- [Alternatives](#alternatives)
2828
- [License](#license)
2929
- [About CyberCog](#about-cybercog)
@@ -50,8 +50,12 @@ Eloquent boolean & timestamp flagged attributes behavior. Enhance eloquent model
5050
| `HasActiveFlag` | Classic | `is_active` | Boolean | - |
5151
| `HasApprovedAt` | Classic | `approved_at` | Timestamp | `HasApprovedFlag` |
5252
| `HasApprovedFlag` | Classic | `is_approved` | Boolean | `HasApprovedAt` |
53+
| `HasArchivedAt` | Inverse | `archived_at` | Timestamp | `HasArchivedFlag` |
54+
| `HasArchivedFlag` | Inverse | `is_archived` | Boolean | `HasArchivedAt` |
5355
| `HasClosedAt` | Inverse | `closed_at` | Timestamp | `HasClosedFlag` |
5456
| `HasClosedFlag` | Inverse | `is_closed` | Boolean | `HasClosedAt` |
57+
| `HasDraftedAt` | Inverse | `drafted_at` | Timestamp | `HasDraftedFlag` |
58+
| `HasDraftedFlag` | Inverse | `is_drafted` | Boolean | `HasDraftedAt` |
5559
| `HasEndedAt` | Inverse | `ended_at` | Timestamp | `HasEndedFlag` |
5660
| `HasEndededFlag` | Inverse | `is_ended` | Boolean | `HasEndedAt` |
5761
| `HasExpiredAt` | Inverse | `expired_at` | Timestamp | `HasExpiredFlag` |
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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\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 ArchivedAtScope.
21+
*
22+
* @package Cog\Flag\Scopes\Inverse
23+
*/
24+
class ArchivedAtScope implements Scope
25+
{
26+
/**
27+
* All of the extensions to be added to the builder.
28+
*
29+
* @var array
30+
*/
31+
protected $extensions = ['Unarchive', 'Archive', 'WithArchived', 'WithoutArchived', 'OnlyArchived'];
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+
if (method_exists($model, 'shouldApplyArchivedAtScope') && !$model->shouldApplyArchivedAtScope()) {
43+
return $builder;
44+
}
45+
46+
return $builder->whereNull('archived_at');
47+
}
48+
49+
/**
50+
* Extend the query builder with the needed functions.
51+
*
52+
* @param \Illuminate\Database\Eloquent\Builder $builder
53+
* @return void
54+
*/
55+
public function extend(Builder $builder)
56+
{
57+
foreach ($this->extensions as $extension) {
58+
$this->{"add{$extension}"}($builder);
59+
}
60+
}
61+
62+
/**
63+
* Add the `unarchive` extension to the builder.
64+
*
65+
* @param \Illuminate\Database\Eloquent\Builder $builder
66+
* @return void
67+
*/
68+
protected function addUnarchive(Builder $builder)
69+
{
70+
$builder->macro('unarchive', function (Builder $builder) {
71+
$builder->withArchived();
72+
73+
return $builder->update(['archived_at' => null]);
74+
});
75+
}
76+
77+
/**
78+
* Add the `archive` extension to the builder.
79+
*
80+
* @param \Illuminate\Database\Eloquent\Builder $builder
81+
* @return void
82+
*/
83+
protected function addArchive(Builder $builder)
84+
{
85+
$builder->macro('archive', function (Builder $builder) {
86+
return $builder->update(['archived_at' => Carbon::now()]);
87+
});
88+
}
89+
90+
/**
91+
* Add the `withArchived` extension to the builder.
92+
*
93+
* @param \Illuminate\Database\Eloquent\Builder $builder
94+
* @return void
95+
*/
96+
protected function addWithArchived(Builder $builder)
97+
{
98+
$builder->macro('withArchived', function (Builder $builder) {
99+
return $builder->withoutGlobalScope($this);
100+
});
101+
}
102+
103+
/**
104+
* Add the `withoutArchived` extension to the builder.
105+
*
106+
* @param \Illuminate\Database\Eloquent\Builder $builder
107+
* @return void
108+
*/
109+
protected function addWithoutArchived(Builder $builder)
110+
{
111+
$builder->macro('withoutArchived', function (Builder $builder) {
112+
return $builder->withoutGlobalScope($this)->whereNull('archived_at');
113+
});
114+
}
115+
116+
/**
117+
* Add the `onlyArchived` extension to the builder.
118+
*
119+
* @param \Illuminate\Database\Eloquent\Builder $builder
120+
* @return void
121+
*/
122+
protected function addOnlyArchived(Builder $builder)
123+
{
124+
$builder->macro('onlyArchived', function (Builder $builder) {
125+
return $builder->withoutGlobalScope($this)->whereNotNull('archived_at');
126+
});
127+
}
128+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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\Inverse;
13+
14+
use Illuminate\Database\Eloquent\Builder;
15+
use Illuminate\Database\Eloquent\Model;
16+
use Illuminate\Database\Eloquent\Scope;
17+
18+
/**
19+
* Class ArchivedFlagScope.
20+
*
21+
* @package Cog\Flag\Scopes\Inverse
22+
*/
23+
class ArchivedFlagScope implements Scope
24+
{
25+
/**
26+
* All of the extensions to be added to the builder.
27+
*
28+
* @var array
29+
*/
30+
protected $extensions = ['Unarchive', 'Archive', 'WithArchived', 'WithoutArchived', 'OnlyArchived'];
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+
if (method_exists($model, 'shouldApplyArchivedFlagScope') && !$model->shouldApplyArchivedFlagScope()) {
42+
return $builder;
43+
}
44+
45+
return $builder->where('is_archived', 0);
46+
}
47+
48+
/**
49+
* Extend the query builder with the needed functions.
50+
*
51+
* @param \Illuminate\Database\Eloquent\Builder $builder
52+
* @return void
53+
*/
54+
public function extend(Builder $builder)
55+
{
56+
foreach ($this->extensions as $extension) {
57+
$this->{"add{$extension}"}($builder);
58+
}
59+
}
60+
61+
/**
62+
* Add the `unarchive` extension to the builder.
63+
*
64+
* @param \Illuminate\Database\Eloquent\Builder $builder
65+
* @return void
66+
*/
67+
protected function addUnarchive(Builder $builder)
68+
{
69+
$builder->macro('unarchive', function (Builder $builder) {
70+
$builder->withArchived();
71+
72+
return $builder->update(['is_archived' => 0]);
73+
});
74+
}
75+
76+
/**
77+
* Add the `archive` extension to the builder.
78+
*
79+
* @param \Illuminate\Database\Eloquent\Builder $builder
80+
* @return void
81+
*/
82+
protected function addArchive(Builder $builder)
83+
{
84+
$builder->macro('archive', function (Builder $builder) {
85+
return $builder->update(['is_archived' => 1]);
86+
});
87+
}
88+
89+
/**
90+
* Add the `withArchived` extension to the builder.
91+
*
92+
* @param \Illuminate\Database\Eloquent\Builder $builder
93+
* @return void
94+
*/
95+
protected function addWithArchived(Builder $builder)
96+
{
97+
$builder->macro('withArchived', function (Builder $builder) {
98+
return $builder->withoutGlobalScope($this);
99+
});
100+
}
101+
102+
/**
103+
* Add the `withoutArchived` extension to the builder.
104+
*
105+
* @param \Illuminate\Database\Eloquent\Builder $builder
106+
* @return void
107+
*/
108+
protected function addWithoutArchived(Builder $builder)
109+
{
110+
$builder->macro('withoutArchived', function (Builder $builder) {
111+
return $builder->withoutGlobalScope($this)->where('is_archived', 0);
112+
});
113+
}
114+
115+
/**
116+
* Add the `onlyArchived` extension to the builder.
117+
*
118+
* @param \Illuminate\Database\Eloquent\Builder $builder
119+
* @return void
120+
*/
121+
protected function addOnlyArchived(Builder $builder)
122+
{
123+
$builder->macro('onlyArchived', function (Builder $builder) {
124+
return $builder->withoutGlobalScope($this)->where('is_archived', 1);
125+
});
126+
}
127+
}

0 commit comments

Comments
 (0)