Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
adammatysiak committed Jan 22, 2018
2 parents ad53d6c + bd28e24 commit d74ae0d
Show file tree
Hide file tree
Showing 39 changed files with 518 additions and 576 deletions.
29 changes: 12 additions & 17 deletions src/Sequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,27 @@

namespace HighSolutions\EloquentSequence;

use HighSolutions\EloquentSequence\SequenceObserver;
use HighSolutions\EloquentSequence\SequenceService;
use Illuminate\Database\Eloquent\Builder;

trait Sequence
{

/**
* Return the sequence configuration array for this model.
*
* @return array
*/
abstract public function sequence();

/**
* Boot Sequence Observer for event handling.
*
* @return void
*/
public static function bootSequence()
{
self::observe(SequenceObserver::class);
}
/**
* Boot Sequence Observer for event handling.
*
* @return void
*/
public static function bootSequence()
{
self::observe(SequenceObserver::class);
}

/**
* Scope a query to order by sequence attribute
/**
* Scope a query to order by sequence attribute.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $direction Sorting order
Expand All @@ -36,6 +31,7 @@ public static function bootSequence()
public function scopeSequenced($query, $direction = 'asc')
{
$fieldName = isset($this->sequence()['fieldName']) ? $this->sequence()['fieldName'] : 'seq';

return $query->orderBy($fieldName, $direction);
}

Expand Down Expand Up @@ -104,5 +100,4 @@ public static function refreshSequence()
{
return (new SequenceService)->refresh(get_called_class());
}

}
34 changes: 16 additions & 18 deletions src/SequenceObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,44 @@

namespace HighSolutions\EloquentSequence;

use HighSolutions\EloquentSequence\SequenceService;
use Illuminate\Database\Eloquent\Model;

class SequenceObserver {
class SequenceObserver
{
/**
* @var \App\Packages\UpAndDown\SequnceService
*/
protected $service;
protected $service;

/**
* SequenceObserver constructor.
*
* @param \App\Packages\UpAndDown\SequnceService $service
*/
public function __construct(SequenceService $service)
{
$this->service = $service;
}
public function __construct(SequenceService $service)
{
$this->service = $service;
}

/**
* Listen to the Sequence creating event.
*
* @param Model $obj
* @return void
*/
public function creating(Model $obj)
{
$this->service->assignSequence($obj);
}
public function creating(Model $obj)
{
$this->service->assignSequence($obj);
}

/**
* Listen to the Sequence deleting event.
*
* @param Model $obj
* @return void
*/
public function deleting(Model $obj)
{
$this->service->updateSequences($obj);
}

}
public function deleting(Model $obj)
{
$this->service->updateSequences($obj);
}
}
Loading

0 comments on commit d74ae0d

Please sign in to comment.