Skip to content
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

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

## 3.3.0 - 2017-01-12

### Added

- `verified_at` classic timestamp flag added.

## 3.2.0 - 2017-01-12

### Added
Expand Down
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ Eloquent boolean & timestamp flagged attributes behavior. Enhance eloquent model
| `HasKeptFlag` | Classic | `is_kept` | Boolean | - |
| `HasPublishedAt` | Classic | `published_at` | Timestamp | `HasPublishedFlag` |
| `HasPublishedFlag` | Classic | `is_published` | Boolean | `HasPublishedAt` |
| `HasVerifiedFlag` | Classic | `is_verified` | Boolean | - |
| `HasVerifiedAt` | Classic | `verified_at` | Timestamp | `HasVerifiedFlag` |
| `HasVerifiedFlag` | Classic | `is_verified` | Boolean | `HasVerifiedAt` |

Any entity can has more than one flag at the same time. If flags can't work for the same entity simultaneously they are listed in `Conflict` column.

Expand Down Expand Up @@ -353,6 +354,8 @@ Post::where('id', 4)->unpublish();

### Setup a verifiable model

#### With boolean flag

```php
<?php

Expand All @@ -369,6 +372,24 @@ class Post extends Model

*Model must have boolean `is_verified` column in database table.*

#### With timestamp flag

```php
<?php

namespace App\Models;

use Cog\Flag\Traits\Classic\HasVerifiedAt;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
use HasVerifiedAt;
}
```

*Model must have nullable timestamp `verified_at` column in database table.*

### Available functions

#### Get only verified models
Expand Down
124 changes: 124 additions & 0 deletions src/Scopes/Classic/VerifiedAtScope.php
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 VerifiedAtScope.
*
* @package Cog\Flag\Scopes\Classic
*/
class VerifiedAtScope implements Scope
{
/**
* All of the extensions to be added to the builder.
*
* @var array
*/
protected $extensions = ['Verify', 'Unverify', 'WithUnverified', 'WithoutUnverified', 'OnlyUnverified'];

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

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

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

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

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

/**
* Add the `onlyUnverified` extension to the builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
protected function addOnlyUnverified(Builder $builder)
{
$builder->macro('onlyUnverified', function (Builder $builder) {
return $builder->withoutGlobalScope($this)->whereNull('verified_at');
});
}
}
2 changes: 1 addition & 1 deletion src/Traits/Classic/HasActiveFlagScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
trait HasActiveFlagScope
{
/**
* Boot the HasKeptFlagScope trait for a model.
* Boot the HasActiveFlagScope trait for a model.
*
* @return void
*/
Expand Down
23 changes: 23 additions & 0 deletions src/Traits/Classic/HasVerifiedAt.php
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 HasVerifiedAt.
*
* @package Cog\Flag\Traits\Classic
*/
trait HasVerifiedAt
{
use HasVerifiedAtHelpers,
HasVerifiedAtScope;
}
90 changes: 90 additions & 0 deletions src/Traits/Classic/HasVerifiedAtHelpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?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 Carbon\Carbon;

/**
* Class HasVerifiedAtHelpers.
*
* @package Cog\Flag\Traits\Classic
*/
trait HasVerifiedAtHelpers
{
/**
* Set verified flag.
*
* @return static
*/
public function setVerifiedFlag()
{
$this->verified_at = Carbon::now();

return $this;
}

/**
* Unset verified flag.
*
* @return static
*/
public function unsetVerifiedFlag()
{
$this->verified_at = null;

return $this;
}

/**
* If entity is verified.
*
* @return bool
*/
public function isVerified()
{
return !is_null($this->verified_at);
}

/**
* If entity is unverified.
*
* @return bool
*/
public function isUnverified()
{
return !$this->isVerified();
}

/**
* Mark entity as verified.
*
* @return void
*/
public function verify()
{
$this->setVerifiedFlag()->save();

// :TODO: Fire an event here
}

/**
* Mark entity as rejected.
*
* @return void
*/
public function unverify()
{
$this->unsetVerifiedFlag()->save();

// :TODO: Fire an event here
}
}
32 changes: 32 additions & 0 deletions src/Traits/Classic/HasVerifiedAtScope.php
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\VerifiedAtScope;

/**
* Class HasVerifiedAtScope.
*
* @package Cog\Flag\Traits\Classic
*/
trait HasVerifiedAtScope
{
/**
* Boot the HasVerifiedAtScope for a model.
*
* @return void
*/
public static function bootHasVerifiedAtScope()
{
static::addGlobalScope(new VerifiedAtScope);
}
}
Loading