Skip to content

Commit

Permalink
Refactor filters to some traits enable checking for implemented trait…
Browse files Browse the repository at this point in the history
… and asking column and operator configuration.
  • Loading branch information
bbrala committed Jul 30, 2021
1 parent 47b8e53 commit 8d34036
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 92 deletions.
21 changes: 21 additions & 0 deletions src/Filters/Concerns/HasColumn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php


namespace LaravelJsonApi\Eloquent\Filters\Concerns;


trait HasColumn {

/**
* @var string
*/
private string $column;

/**
* @return string
*/
public function column(): string
{
return $this->column;
}
}
73 changes: 73 additions & 0 deletions src/Filters/Concerns/HasOperator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php


namespace LaravelJsonApi\Eloquent\Filters\Concerns;

trait HasOperator {

/**
* @var string
*/
private string $operator;

/**
* @return $this
*/
public function eq(): self
{
return $this->using('=');
}

/**
* @return $this
*/
public function gt(): self
{
return $this->using('>');
}

/**
* @return $this
*/
public function gte(): self
{
return $this->using('>=');
}

/**
* @return $this
*/
public function lt(): self
{
return $this->using('<');
}

/**
* @return $this
*/
public function lte(): self
{
return $this->using('<=');
}

/**
* Use the provided operator for the filter.
*
* @param string $operator
* @return $this
*/
public function using(string $operator): self
{
$this->operator = $operator;

return $this;
}

/**
* @return string
*/
public function operator(): string
{
return $this->operator;
}
}
81 changes: 2 additions & 79 deletions src/Filters/Where.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,15 @@ class Where implements Filter
{

use Concerns\DeserializesValue;
use Concerns\HasColumn;
use Concerns\HasOperator;
use Concerns\IsSingular;

/**
* @var string
*/
private string $name;

/**
* @var string
*/
private string $column;

/**
* @var string
*/
private string $operator;

/**
* Create a new filter.
*
Expand All @@ -68,59 +60,6 @@ public function __construct(string $name, string $column = null)
$this->operator = '=';
}

/**
* @return $this
*/
public function eq(): self
{
return $this->using('=');
}

/**
* @return $this
*/
public function gt(): self
{
return $this->using('>');
}

/**
* @return $this
*/
public function gte(): self
{
return $this->using('>=');
}

/**
* @return $this
*/
public function lt(): self
{
return $this->using('<');
}

/**
* @return $this
*/
public function lte(): self
{
return $this->using('<=');
}

/**
* Use the provided operator for the filter.
*
* @param string $operator
* @return $this
*/
public function using(string $operator): self
{
$this->operator = $operator;

return $this;
}

/**
* @inheritDoc
*/
Expand All @@ -141,22 +80,6 @@ public function apply($query, $value)
);
}

/**
* @return string
*/
protected function column(): string
{
return $this->column;
}

/**
* @return string
*/
protected function operator(): string
{
return $this->operator;
}

/**
* @return string
*/
Expand Down
14 changes: 1 addition & 13 deletions src/Filters/WhereIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,14 @@ class WhereIn implements Filter
{

use Concerns\DeserializesValue;
use Concerns\HasColumn;
use Concerns\HasDelimiter;

/**
* @var string
*/
private string $name;

/**
* @var string
*/
private string $column;

/**
* Create a new filter.
*
Expand Down Expand Up @@ -89,14 +85,6 @@ public function apply($query, $value)
);
}

/**
* @return string
*/
protected function column(): string
{
return $this->column;
}

/**
* Deserialize the fitler value.
*
Expand Down

0 comments on commit 8d34036

Please sign in to comment.