Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ended and invited flags #31

Merged
merged 2 commits into from
Nov 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ vendor/
composer.lock
composer.phar
phpunit.xml
.idea/
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ Eloquent boolean & timestamp flagged attributes behavior. Enhance eloquent model
| `HasApprovedFlag` | Classic | `is_approved` | Boolean | `HasApprovedAt` |
| `HasClosedAt` | Inverse | `closed_at` | Timestamp | `HasClosedFlag` |
| `HasClosedFlag` | Inverse | `is_closed` | Boolean | `HasClosedAt` |
| `HasEndedAt` | Inverse | `ended_at` | Timestamp | `HasEndedFlag` |
| `HasEndededFlag` | Inverse | `is_ended` | Boolean | `HasEndedAt` |
| `HasExpiredAt` | Inverse | `expired_at` | Timestamp | `HasExpiredFlag` |
| `HasExpiredFlag` | Inverse | `is_expired` | Boolean | `HasExpiredAt` |
| `HasInvitedAt` | Classic | `invited_at` | Timestamp | `HasInvitedFlag` |
| `HasInvitedFlag` | Classic | `is_invited` | Boolean | `HasInvitedAt` |
| `HasKeptFlag` | Classic | `is_kept` | Boolean | - |
| `HasPublishedAt` | Classic | `published_at` | Timestamp | `HasPublishedFlag` |
| `HasPublishedFlag` | Classic | `is_published` | Boolean | `HasPublishedAt` |
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"closed",
"opened",
"expiry",
"expired"
"expired",
"ended"
],
"authors": [
{
Expand Down
128 changes: 128 additions & 0 deletions src/Scopes/Classic/InvitedAtScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?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 InvitedAtScope.
*
* @package Cog\Flag\Scopes\Classic
*/
class InvitedAtScope implements Scope
{
/**
* All of the extensions to be added to the builder.
*
* @var array
*/
protected $extensions = ['Invite', 'Uninvite', 'WithUninvited', 'WithoutUninvited', 'OnlyUninvited'];

/**
* 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)
{
if (method_exists($model, 'shouldApplyInvitedAtScope') && !$model->shouldApplyInvitedAtScope()) {
return $builder;
}

return $builder->whereNotNull('invited_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 `invite` extension to the builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
protected function addInvite(Builder $builder)
{
$builder->macro('invite', function (Builder $builder) {
$builder->withUninvited();

return $builder->update(['invited_at' => Carbon::now()]);
});
}

/**
* Add the `uninvite` extension to the builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
protected function addUninvite(Builder $builder)
{
$builder->macro('uninvite', function (Builder $builder) {
return $builder->update(['invited_at' => null]);
});
}

/**
* Add the `withUninvited` extension to the builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
protected function addWithUninvited(Builder $builder)
{
$builder->macro('withUninvited', function (Builder $builder) {
return $builder->withoutGlobalScope($this);
});
}

/**
* Add the `withoutUninvited` extension to the builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
protected function addWithoutUninvited(Builder $builder)
{
$builder->macro('withoutUninvited', function (Builder $builder) {
return $builder->withoutGlobalScope($this)->whereNotNull('invited_at');
});
}

/**
* Add the `onlyUninvited` extension to the builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
protected function addOnlyUninvited(Builder $builder)
{
$builder->macro('onlyUninvited', function (Builder $builder) {
return $builder->withoutGlobalScope($this)->whereNull('invited_at');
});
}
}
127 changes: 127 additions & 0 deletions src/Scopes/Classic/InvitedFlagScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?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 Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

/**
* Class InvitedFlagScope.
*
* @package Cog\Flag\Scopes\Classic
*/
class InvitedFlagScope implements Scope
{
/**
* All of the extensions to be added to the builder.
*
* @var array
*/
protected $extensions = ['Invite', 'Uninvite', 'WithUninvited', 'WithoutUninvited', 'OnlyUninvited'];

/**
* 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)
{
if (method_exists($model, 'shouldApplyInvitedFlagScope') && !$model->shouldApplyInvitedFlagScope()) {
return $builder;
}

return $builder->where('is_invited', 1);
}

/**
* 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 `invite` extension to the builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
protected function addInvite(Builder $builder)
{
$builder->macro('invite', function (Builder $builder) {
$builder->withUninvited();

return $builder->update(['is_invited' => 1]);
});
}

/**
* Add the `uninvite` extension to the builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
protected function addUninvite(Builder $builder)
{
$builder->macro('uninvite', function (Builder $builder) {
return $builder->update(['is_invited' => 0]);
});
}

/**
* Add the `withUninvited` extension to the builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
protected function addWithUninvited(Builder $builder)
{
$builder->macro('withUninvited', function (Builder $builder) {
return $builder->withoutGlobalScope($this);
});
}

/**
* Add the `withoutUninvited` extension to the builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
protected function addWithoutUninvited(Builder $builder)
{
$builder->macro('withoutUninvited', function (Builder $builder) {
return $builder->withoutGlobalScope($this)->where('is_invited', 1);
});
}

/**
* Add the `onlyUninvited` extension to the builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
protected function addOnlyUninvited(Builder $builder)
{
$builder->macro('onlyUninvited', function (Builder $builder) {
return $builder->withoutGlobalScope($this)->where('is_invited', 0);
});
}
}
Loading