Skip to content

Commit 7d7b17c

Browse files
committed
Separate flag scopes, helpers and behavior traits
1 parent bfc70ad commit 7d7b17c

21 files changed

+333
-80
lines changed

CHANGELOG.md

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

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

5+
## 3.0.0 - [Unreleased]
6+
7+
### Added
8+
9+
- `Has{Name}FlagScope` traits which include global scopes.
10+
- `Has{Name}FlagHelpers` traits which include flag related helper methods.
11+
- `Has{Name}FlagBehavior` traits which include flag specific behavior.
12+
13+
### Changed
14+
15+
- Each Flag trait was spliced on 2 additional traits: `Has{Name}Flag` = `Has{Name}FlagScope` + `Has{Name}FlagHelpers`
16+
- Kept Flag trait was spliced on 3 additional traits, because events were pulled out to `HasKeptFlagBehavior` trait.
17+
518
## 2.1.0 - 2016-01-04
619

720
- `is_closed` inverse boolean flag added.

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cybercog/laravel-eloquent-flag",
3-
"description": "Laravel Eloquent flagged attributes behavior",
3+
"description": "Laravel Eloquent flagged attributes behavior.",
44
"type": "library",
55
"license": "MIT",
66
"keywords": [
@@ -50,14 +50,14 @@
5050
"docs": "https://github.com/cybercog/laravel-eloquent-flag/wiki"
5151
},
5252
"require": {
53-
"php": "^5.6|^7.0",
54-
"illuminate/database": "~5.2.0|~5.3.0|~5.4.0"
53+
"illuminate/database": "~5.2.0|~5.3.0|~5.4.0",
54+
"php": "^5.6|^7.0"
5555
},
5656
"require-dev": {
5757
"friendsofphp/php-cs-fixer": "^1.11",
58-
"phpunit/phpunit": "^5.2",
58+
"mockery/mockery": "^0.9.5",
5959
"orchestra/testbench": "~3.0",
60-
"mockery/mockery": "^0.9.5"
60+
"phpunit/phpunit": "^5.2"
6161
},
6262
"autoload": {
6363
"psr-4": {

src/Traits/Classic/HasAcceptedFlag.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
*/
1919
trait HasAcceptedFlag
2020
{
21-
use HasAcceptedFlagScope;
21+
use HasAcceptedFlagHelpers,
22+
HasAcceptedFlagScope;
2223
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Laravel Eloquent Flag.
5+
*
6+
* (c) CyberCog <support@cybercog.su>
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\Classic;
13+
14+
/**
15+
* Class HasAcceptedFlagHelpers.
16+
*
17+
* @package Cog\Flag\Traits\Classic
18+
*/
19+
trait HasAcceptedFlagHelpers
20+
{
21+
//
22+
}

src/Traits/Classic/HasActiveFlag.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
*/
1919
trait HasActiveFlag
2020
{
21-
use HasActiveFlagScope;
21+
use HasActiveFlagHelpers,
22+
HasActiveFlagScope;
2223
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Laravel Eloquent Flag.
5+
*
6+
* (c) CyberCog <support@cybercog.su>
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\Classic;
13+
14+
/**
15+
* Class HasActiveFlagHelpers.
16+
*
17+
* @package Cog\Flag\Traits\Classic
18+
*/
19+
trait HasActiveFlagHelpers
20+
{
21+
//
22+
}

src/Traits/Classic/HasApprovedFlag.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
*/
1919
trait HasApprovedFlag
2020
{
21-
use HasApprovedFlagScope;
21+
use HasApprovedFlagHelpers,
22+
HasApprovedFlagScope;
2223
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Laravel Eloquent Flag.
5+
*
6+
* (c) CyberCog <support@cybercog.su>
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\Classic;
13+
14+
/**
15+
* Class HasApprovedFlagHelpers.
16+
*
17+
* @package Cog\Flag\Traits\Classic
18+
*/
19+
trait HasApprovedFlagHelpers
20+
{
21+
//
22+
}

src/Traits/Classic/HasKeptFlag.php

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,58 +11,14 @@
1111

1212
namespace Cog\Flag\Traits\Classic;
1313

14-
use Carbon\Carbon;
15-
use Illuminate\Database\Eloquent\Builder;
16-
1714
/**
1815
* Class HasKeptFlag.
1916
*
2017
* @package Cog\Flag\Traits\Classic
2118
*/
2219
trait HasKeptFlag
2320
{
24-
use HasKeptFlagScope;
25-
26-
/**
27-
* Boot the HasKeptFlag trait for a model.
28-
*
29-
* @return void
30-
*/
31-
public static function bootHasKeptFlag()
32-
{
33-
static::creating(function ($entity) {
34-
if (!$entity->is_kept) {
35-
$entity->is_kept = false;
36-
}
37-
});
38-
39-
static::updating(function ($entity) {
40-
if (!$entity->is_kept) {
41-
$entity->is_kept = true;
42-
}
43-
});
44-
}
45-
46-
/**
47-
* Determine if the model instance has `is_kept` state.
48-
*
49-
* @return bool
50-
*/
51-
public function isKept()
52-
{
53-
return (bool) $this->is_kept;
54-
}
55-
56-
/**
57-
* Get unkept models that are older than the given number of hours.
58-
*
59-
* @param \Illuminate\Database\Eloquent\Builder $builder
60-
* @param int $hours
61-
* @return \Illuminate\Database\Eloquent\Builder
62-
*/
63-
public function scopeOnlyUnkeptOlderThanHours(Builder $builder, $hours)
64-
{
65-
return $builder->onlyUnkept()
66-
->where(static::getCreatedAtColumn(), '<=', Carbon::now()->subHours($hours)->toDateTimeString());
67-
}
21+
use HasKeptFlagBehavior,
22+
HasKeptFlagHelpers,
23+
HasKeptFlagScope;
6824
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Laravel Eloquent Flag.
5+
*
6+
* (c) CyberCog <support@cybercog.su>
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\Classic;
13+
14+
/**
15+
* Class HasKeptFlagBehavior.
16+
*
17+
* @package Cog\Flag\Traits\Classic
18+
*/
19+
trait HasKeptFlagBehavior
20+
{
21+
/**
22+
* Boot the bootHasKeptFlagBehavior trait for a model.
23+
*
24+
* @return void
25+
*/
26+
public static function bootHasKeptFlagBehavior()
27+
{
28+
static::creating(function ($entity) {
29+
if (!$entity->is_kept) {
30+
$entity->is_kept = false;
31+
}
32+
});
33+
34+
static::updating(function ($entity) {
35+
if (!$entity->is_kept) {
36+
$entity->is_kept = true;
37+
}
38+
});
39+
}
40+
}

0 commit comments

Comments
 (0)