Skip to content

Commit 489c8bc

Browse files
zagreusinozAnton Komarev
authored andcommitted
Add ended and invited flags (#31)
Implemented Ended and Invited flags
1 parent 9ac38fc commit 489c8bc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2577
-1
lines changed

.gitignore

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

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,12 @@ Eloquent boolean & timestamp flagged attributes behavior. Enhance eloquent model
5252
| `HasApprovedFlag` | Classic | `is_approved` | Boolean | `HasApprovedAt` |
5353
| `HasClosedAt` | Inverse | `closed_at` | Timestamp | `HasClosedFlag` |
5454
| `HasClosedFlag` | Inverse | `is_closed` | Boolean | `HasClosedAt` |
55+
| `HasEndedAt` | Inverse | `ended_at` | Timestamp | `HasEndedFlag` |
56+
| `HasEndededFlag` | Inverse | `is_ended` | Boolean | `HasEndedAt` |
5557
| `HasExpiredAt` | Inverse | `expired_at` | Timestamp | `HasExpiredFlag` |
5658
| `HasExpiredFlag` | Inverse | `is_expired` | Boolean | `HasExpiredAt` |
59+
| `HasInvitedAt` | Classic | `invited_at` | Timestamp | `HasInvitedFlag` |
60+
| `HasInvitedFlag` | Classic | `is_invited` | Boolean | `HasInvitedAt` |
5761
| `HasKeptFlag` | Classic | `is_kept` | Boolean | - |
5862
| `HasPublishedAt` | Classic | `published_at` | Timestamp | `HasPublishedFlag` |
5963
| `HasPublishedFlag` | Classic | `is_published` | Boolean | `HasPublishedAt` |

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"closed",
3434
"opened",
3535
"expiry",
36-
"expired"
36+
"expired",
37+
"ended"
3738
],
3839
"authors": [
3940
{
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\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 InvitedAtScope.
21+
*
22+
* @package Cog\Flag\Scopes\Classic
23+
*/
24+
class InvitedAtScope implements Scope
25+
{
26+
/**
27+
* All of the extensions to be added to the builder.
28+
*
29+
* @var array
30+
*/
31+
protected $extensions = ['Invite', 'Uninvite', 'WithUninvited', 'WithoutUninvited', 'OnlyUninvited'];
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, 'shouldApplyInvitedAtScope') && !$model->shouldApplyInvitedAtScope()) {
43+
return $builder;
44+
}
45+
46+
return $builder->whereNotNull('invited_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 `invite` extension to the builder.
64+
*
65+
* @param \Illuminate\Database\Eloquent\Builder $builder
66+
* @return void
67+
*/
68+
protected function addInvite(Builder $builder)
69+
{
70+
$builder->macro('invite', function (Builder $builder) {
71+
$builder->withUninvited();
72+
73+
return $builder->update(['invited_at' => Carbon::now()]);
74+
});
75+
}
76+
77+
/**
78+
* Add the `uninvite` extension to the builder.
79+
*
80+
* @param \Illuminate\Database\Eloquent\Builder $builder
81+
* @return void
82+
*/
83+
protected function addUninvite(Builder $builder)
84+
{
85+
$builder->macro('uninvite', function (Builder $builder) {
86+
return $builder->update(['invited_at' => null]);
87+
});
88+
}
89+
90+
/**
91+
* Add the `withUninvited` extension to the builder.
92+
*
93+
* @param \Illuminate\Database\Eloquent\Builder $builder
94+
* @return void
95+
*/
96+
protected function addWithUninvited(Builder $builder)
97+
{
98+
$builder->macro('withUninvited', function (Builder $builder) {
99+
return $builder->withoutGlobalScope($this);
100+
});
101+
}
102+
103+
/**
104+
* Add the `withoutUninvited` extension to the builder.
105+
*
106+
* @param \Illuminate\Database\Eloquent\Builder $builder
107+
* @return void
108+
*/
109+
protected function addWithoutUninvited(Builder $builder)
110+
{
111+
$builder->macro('withoutUninvited', function (Builder $builder) {
112+
return $builder->withoutGlobalScope($this)->whereNotNull('invited_at');
113+
});
114+
}
115+
116+
/**
117+
* Add the `onlyUninvited` extension to the builder.
118+
*
119+
* @param \Illuminate\Database\Eloquent\Builder $builder
120+
* @return void
121+
*/
122+
protected function addOnlyUninvited(Builder $builder)
123+
{
124+
$builder->macro('onlyUninvited', function (Builder $builder) {
125+
return $builder->withoutGlobalScope($this)->whereNull('invited_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\Classic;
13+
14+
use Illuminate\Database\Eloquent\Builder;
15+
use Illuminate\Database\Eloquent\Model;
16+
use Illuminate\Database\Eloquent\Scope;
17+
18+
/**
19+
* Class InvitedFlagScope.
20+
*
21+
* @package Cog\Flag\Scopes\Classic
22+
*/
23+
class InvitedFlagScope implements Scope
24+
{
25+
/**
26+
* All of the extensions to be added to the builder.
27+
*
28+
* @var array
29+
*/
30+
protected $extensions = ['Invite', 'Uninvite', 'WithUninvited', 'WithoutUninvited', 'OnlyUninvited'];
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, 'shouldApplyInvitedFlagScope') && !$model->shouldApplyInvitedFlagScope()) {
42+
return $builder;
43+
}
44+
45+
return $builder->where('is_invited', 1);
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 `invite` extension to the builder.
63+
*
64+
* @param \Illuminate\Database\Eloquent\Builder $builder
65+
* @return void
66+
*/
67+
protected function addInvite(Builder $builder)
68+
{
69+
$builder->macro('invite', function (Builder $builder) {
70+
$builder->withUninvited();
71+
72+
return $builder->update(['is_invited' => 1]);
73+
});
74+
}
75+
76+
/**
77+
* Add the `uninvite` extension to the builder.
78+
*
79+
* @param \Illuminate\Database\Eloquent\Builder $builder
80+
* @return void
81+
*/
82+
protected function addUninvite(Builder $builder)
83+
{
84+
$builder->macro('uninvite', function (Builder $builder) {
85+
return $builder->update(['is_invited' => 0]);
86+
});
87+
}
88+
89+
/**
90+
* Add the `withUninvited` extension to the builder.
91+
*
92+
* @param \Illuminate\Database\Eloquent\Builder $builder
93+
* @return void
94+
*/
95+
protected function addWithUninvited(Builder $builder)
96+
{
97+
$builder->macro('withUninvited', function (Builder $builder) {
98+
return $builder->withoutGlobalScope($this);
99+
});
100+
}
101+
102+
/**
103+
* Add the `withoutUninvited` extension to the builder.
104+
*
105+
* @param \Illuminate\Database\Eloquent\Builder $builder
106+
* @return void
107+
*/
108+
protected function addWithoutUninvited(Builder $builder)
109+
{
110+
$builder->macro('withoutUninvited', function (Builder $builder) {
111+
return $builder->withoutGlobalScope($this)->where('is_invited', 1);
112+
});
113+
}
114+
115+
/**
116+
* Add the `onlyUninvited` extension to the builder.
117+
*
118+
* @param \Illuminate\Database\Eloquent\Builder $builder
119+
* @return void
120+
*/
121+
protected function addOnlyUninvited(Builder $builder)
122+
{
123+
$builder->macro('onlyUninvited', function (Builder $builder) {
124+
return $builder->withoutGlobalScope($this)->where('is_invited', 0);
125+
});
126+
}
127+
}

0 commit comments

Comments
 (0)