Skip to content

Commit cba251b

Browse files
committed
Add closed at inverse flag
1 parent 0e20cd6 commit cba251b

19 files changed

+804
-20
lines changed

CHANGELOG.md

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,35 @@
1-
# Changelog
1+
# Change Log
22

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

5-
## 3.3.0 - 2017-01-12
5+
## [3.4.0] - 2017-01-13
6+
7+
### Added
8+
9+
- `closed_at` inverse timestamp flag added.
10+
- `is_closed` inverse boolean flag helpers added.
11+
12+
## [3.3.0] - 2017-01-12
613

714
### Added
815

916
- `verified_at` classic timestamp flag added.
17+
- `is_verified` classic boolean flag helpers added.
1018

11-
## 3.2.0 - 2017-01-12
19+
## [3.2.0] - 2017-01-12
1220

1321
### Added
1422

1523
- `accepted_at` classic timestamp flag added.
1624

17-
## 3.1.0 - 2017-01-11
25+
## [3.1.0] - 2017-01-11
1826

1927
### Added
2028

2129
- `Timestamp` flag types introduced.
2230
- `published_at` classic timestamp flag added.
2331

24-
## 3.0.0 - 2017-01-07
32+
## [3.0.0] - 2017-01-07
2533

2634
### Added
2735

@@ -35,11 +43,11 @@ All notable changes to `laravel-eloquent-flag` will be documented in this file.
3543
- Kept Flag trait was spliced on 3 additional traits, because events were pulled out to `HasKeptFlagBehavior` trait.
3644
- Flags `Classic\Accepted`, `Classic\Active` & `Classic\Approved` methods were changed. Details in the [Upgrade Guide](UPGRADE.md).
3745

38-
## 2.1.0 - 2017-01-04
46+
## [2.1.0] - 2017-01-04
3947

4048
- `is_closed` inverse boolean flag added.
4149

42-
## 2.0.0 - 2017-01-04
50+
## [2.0.0] - 2017-01-04
4351

4452
### Breaking changes
4553

@@ -51,26 +59,39 @@ All notable changes to `laravel-eloquent-flag` will be documented in this file.
5159
- `Inverse Logic` flags group. Hides entities if flag not set.
5260
- `is_expired` inverse boolean flag added.
5361

54-
## 1.5.0 - 2016-12-31
62+
## [1.5.0] - 2016-12-31
5563

5664
- `is_approved` boolean flag added.
5765

58-
## 1.4.0 - 2016-12-26
66+
## [1.4.0] - 2016-12-26
5967

6068
- `is_verified` boolean flag added.
6169

62-
## 1.3.0 - 2016-12-14
70+
## [1.3.0] - 2016-12-14
6371

6472
- `is_accepted` boolean flag added.
6573

66-
## 1.2.0 - 2016-12-10
74+
## [1.2.0] - 2016-12-10
6775

6876
- `is_kept` boolean flag added.
6977

70-
## 1.1.0 - 2016-09-25
78+
## [1.1.0] - 2016-09-25
7179

7280
- `is_published` boolean flag added.
7381

7482
## 1.0.0 - 2016-09-25
7583

7684
- `is_active` boolean flag added.
85+
86+
[3.4.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/3.3.0...3.4.0
87+
[3.3.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/3.2.0...3.3.0
88+
[3.2.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/3.1.0...3.2.0
89+
[3.1.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/3.0.0...3.1.0
90+
[3.0.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/2.1.0...3.0.0
91+
[2.1.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/2.0.0...2.1.0
92+
[2.0.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/1.5.0...2.0.0
93+
[1.5.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/1.4.0...1.5.0
94+
[1.4.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/1.3.0...1.4.0
95+
[1.3.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/1.2.0...1.3.0
96+
[1.2.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/1.1.0...1.2.0
97+
[1.1.0]: https://github.com/cybercog/laravel-eloquent-flag/compare/1.0.0...1.1.0

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ Eloquent boolean & timestamp flagged attributes behavior. Enhance eloquent model
2828
| `HasAcceptedFlag` | Classic | `is_accepted` | Boolean | `HasAcceptedAt` |
2929
| `HasActiveFlag` | Classic | `is_active` | Boolean | - |
3030
| `HasApprovedFlag` | Classic | `is_approved` | Boolean | - |
31-
| `HasClosedFlag` | Inverse | `is_closed` | Boolean | - |
31+
| `HasClosedAt` | Inverse | `closed_at` | Timestamp | `HasClosedFlag` |
32+
| `HasClosedFlag` | Inverse | `is_closed` | Boolean | `HasClosedAt` |
3233
| `HasExpiredFlag` | Inverse | `is_expired` | Boolean | - |
3334
| `HasKeptFlag` | Classic | `is_kept` | Boolean | - |
3435
| `HasPublishedAt` | Classic | `published_at` | Timestamp | `HasPublishedFlag` |
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\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 ClosedAtScope.
21+
*
22+
* @package Cog\Flag\Scopes\Inverse
23+
*/
24+
class ClosedAtScope implements Scope
25+
{
26+
/**
27+
* All of the extensions to be added to the builder.
28+
*
29+
* @var array
30+
*/
31+
protected $extensions = ['Open', 'Close', 'WithClosed', 'WithoutClosed', 'OnlyClosed'];
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->whereNull('closed_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 `open` extension to the builder.
60+
*
61+
* @param \Illuminate\Database\Eloquent\Builder $builder
62+
* @return void
63+
*/
64+
protected function addOpen(Builder $builder)
65+
{
66+
$builder->macro('open', function (Builder $builder) {
67+
$builder->withClosed();
68+
69+
return $builder->update(['closed_at' => null]);
70+
});
71+
}
72+
73+
/**
74+
* Add the `close` extension to the builder.
75+
*
76+
* @param \Illuminate\Database\Eloquent\Builder $builder
77+
* @return void
78+
*/
79+
protected function addClose(Builder $builder)
80+
{
81+
$builder->macro('close', function (Builder $builder) {
82+
return $builder->update(['closed_at' => Carbon::now()]);
83+
});
84+
}
85+
86+
/**
87+
* Add the `withClosed` extension to the builder.
88+
*
89+
* @param \Illuminate\Database\Eloquent\Builder $builder
90+
* @return void
91+
*/
92+
protected function addWithClosed(Builder $builder)
93+
{
94+
$builder->macro('withClosed', function (Builder $builder) {
95+
return $builder->withoutGlobalScope($this);
96+
});
97+
}
98+
99+
/**
100+
* Add the `withoutClosed` extension to the builder.
101+
*
102+
* @param \Illuminate\Database\Eloquent\Builder $builder
103+
* @return void
104+
*/
105+
protected function addWithoutClosed(Builder $builder)
106+
{
107+
$builder->macro('withoutClosed', function (Builder $builder) {
108+
return $builder->withoutGlobalScope($this)->whereNull('closed_at');
109+
});
110+
}
111+
112+
/**
113+
* Add the `onlyClosed` extension to the builder.
114+
*
115+
* @param \Illuminate\Database\Eloquent\Builder $builder
116+
* @return void
117+
*/
118+
protected function addOnlyClosed(Builder $builder)
119+
{
120+
$builder->macro('onlyClosed', function (Builder $builder) {
121+
return $builder->withoutGlobalScope($this)->whereNotNull('closed_at');
122+
});
123+
}
124+
}

src/Traits/Inverse/HasClosedAt.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\Inverse;
13+
14+
/**
15+
* Class HasClosedAt.
16+
*
17+
* @package Cog\Flag\Traits\Inverse
18+
*/
19+
trait HasClosedAt
20+
{
21+
use HasClosedAtHelpers,
22+
HasClosedAtScope;
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\Inverse;
13+
14+
use Carbon\Carbon;
15+
16+
/**
17+
* Class HasClosedFlagHelpers.
18+
*
19+
* @package Cog\Flag\Traits\Inverse
20+
*/
21+
trait HasClosedAtHelpers
22+
{
23+
/**
24+
* Set closed flag.
25+
*
26+
* @return static
27+
*/
28+
public function setClosedFlag()
29+
{
30+
$this->closed_at = Carbon::now();
31+
32+
return $this;
33+
}
34+
35+
/**
36+
* Unset closed flag.
37+
*
38+
* @return static
39+
*/
40+
public function unsetClosedFlag()
41+
{
42+
$this->closed_at = null;
43+
44+
return $this;
45+
}
46+
47+
/**
48+
* If entity is closed.
49+
*
50+
* @return bool
51+
*/
52+
public function isClosed()
53+
{
54+
return !is_null($this->closed_at);
55+
}
56+
57+
/**
58+
* If entity is opened.
59+
*
60+
* @return bool
61+
*/
62+
public function isOpened()
63+
{
64+
return !$this->isClosed();
65+
}
66+
67+
/**
68+
* Mark entity as closed.
69+
*
70+
* @return void
71+
*/
72+
public function close()
73+
{
74+
$this->setClosedFlag()->save();
75+
76+
// :TODO: Fire an event here
77+
}
78+
79+
/**
80+
* Mark entity as opened.
81+
*
82+
* @return void
83+
*/
84+
public function open()
85+
{
86+
$this->unsetClosedFlag()->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\Inverse;
13+
14+
use Cog\Flag\Scopes\Inverse\ClosedAtScope;
15+
16+
/**
17+
* Class HasClosedAtScope.
18+
*
19+
* @package Cog\Flag\Traits\Inverse
20+
*/
21+
trait HasClosedAtScope
22+
{
23+
/**
24+
* Boot the HasClosedAtScope trait for a model.
25+
*
26+
* @return void
27+
*/
28+
public static function bootHasClosedAtScope()
29+
{
30+
static::addGlobalScope(new ClosedAtScope);
31+
}
32+
}

0 commit comments

Comments
 (0)