-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PublishedAt timestamp flag
- Loading branch information
Pe Ell
authored
Jan 11, 2017
1 parent
dccf78d
commit 479564e
Showing
19 changed files
with
529 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Laravel Eloquent Flag. | ||
* | ||
* (c) Anton Komarev <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Cog\Flag\Scopes\Classic; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Scope; | ||
|
||
/** | ||
* Class PublishedAtScope. | ||
* | ||
* @package Cog\Flag\Scopes\Classic | ||
*/ | ||
class PublishedAtScope implements Scope | ||
{ | ||
/** | ||
* All of the extensions to be added to the builder. | ||
* | ||
* @var array | ||
*/ | ||
protected $extensions = ['Publish', 'Unpublish', 'WithUnpublished', 'WithoutUnpublished', 'OnlyUnpublished']; | ||
|
||
/** | ||
* Apply the scope to a given Eloquent query builder. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $builder | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @return \Illuminate\Database\Eloquent\Builder | ||
*/ | ||
public function apply(Builder $builder, Model $model) | ||
{ | ||
return $builder->whereNotNull('published_at'); | ||
} | ||
|
||
/** | ||
* Extend the query builder with the needed functions. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $builder | ||
* @return void | ||
*/ | ||
public function extend(Builder $builder) | ||
{ | ||
foreach ($this->extensions as $extension) { | ||
$this->{"add{$extension}"}($builder); | ||
} | ||
} | ||
|
||
/** | ||
* Add the `publish` extension to the builder. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $builder | ||
* @return void | ||
*/ | ||
protected function addPublish(Builder $builder) | ||
{ | ||
$builder->macro('publish', function (Builder $builder) { | ||
$builder->withUnpublished(); | ||
|
||
return $builder->update(['published_at' => Carbon::now()]); | ||
}); | ||
} | ||
|
||
/** | ||
* Add the `unpublish` extension to the builder. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $builder | ||
* @return void | ||
*/ | ||
protected function addUnpublish(Builder $builder) | ||
{ | ||
$builder->macro('unpublish', function (Builder $builder) { | ||
return $builder->update(['published_at' => null]); | ||
}); | ||
} | ||
|
||
/** | ||
* Add the `withUnpublished` extension to the builder. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $builder | ||
* @return void | ||
*/ | ||
protected function addWithUnpublished(Builder $builder) | ||
{ | ||
$builder->macro('withUnpublished', function (Builder $builder) { | ||
return $builder->withoutGlobalScope($this); | ||
}); | ||
} | ||
|
||
/** | ||
* Add the `withoutUnpublished` extension to the builder. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $builder | ||
* @return void | ||
*/ | ||
protected function addWithoutUnpublished(Builder $builder) | ||
{ | ||
$builder->macro('withoutUnpublished', function (Builder $builder) { | ||
return $builder->withoutGlobalScope($this)->whereNotNull('published_at'); | ||
}); | ||
} | ||
|
||
/** | ||
* Add the `onlyUnpublished` extension to the builder. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $builder | ||
* @return void | ||
*/ | ||
protected function addOnlyUnpublished(Builder $builder) | ||
{ | ||
$builder->macro('onlyUnpublished', function (Builder $builder) { | ||
return $builder->withoutGlobalScope($this)->whereNull('published_at'); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Laravel Eloquent Flag. | ||
* | ||
* (c) Anton Komarev <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Cog\Flag\Traits\Classic; | ||
|
||
/** | ||
* Class HasPublishedAt. | ||
* | ||
* @package Cog\Flag\Traits\Classic | ||
*/ | ||
trait HasPublishedAt | ||
{ | ||
use HasPublishedAtHelpers, | ||
HasPublishedAtScope; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Laravel Eloquent Flag. | ||
* | ||
* (c) Anton Komarev <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Cog\Flag\Traits\Classic; | ||
|
||
/** | ||
* Class HasPublishedAtHelpers. | ||
* | ||
* @package Cog\Flag\Traits\Classic | ||
*/ | ||
trait HasPublishedAtHelpers | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Laravel Eloquent Flag. | ||
* | ||
* (c) Anton Komarev <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Cog\Flag\Traits\Classic; | ||
|
||
use Cog\Flag\Scopes\Classic\PublishedAtScope; | ||
|
||
/** | ||
* Class HasPublishedAtScope. | ||
* | ||
* @package Cog\Flag\Traits\Classic | ||
*/ | ||
trait HasPublishedAtScope | ||
{ | ||
/** | ||
* Boot the HasPublishedAtScope trait for a model. | ||
* | ||
* @return void | ||
*/ | ||
public static function bootHasPublishedAtScope() | ||
{ | ||
static::addGlobalScope(new PublishedAtScope); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Laravel Eloquent Flag. | ||
* | ||
* (c) Anton Komarev <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
$factory->define(\Cog\Flag\Tests\Stubs\Models\Classic\EntityWithPublishedAt::class, function (\Faker\Generator $faker) { | ||
return [ | ||
'name' => $faker->word, | ||
'published_at' => null, | ||
]; | ||
}); |
Oops, something went wrong.