From 8d340360d93d7b644a8fb2c9dbb000672ad226b3 Mon Sep 17 00:00:00 2001 From: bjorn Date: Wed, 28 Jul 2021 10:20:05 +0200 Subject: [PATCH] Refactor filters to some traits enable checking for implemented trait and asking column and operator configuration. --- src/Filters/Concerns/HasColumn.php | 21 ++++++++ src/Filters/Concerns/HasOperator.php | 73 +++++++++++++++++++++++++ src/Filters/Where.php | 81 +--------------------------- src/Filters/WhereIn.php | 14 +---- 4 files changed, 97 insertions(+), 92 deletions(-) create mode 100644 src/Filters/Concerns/HasColumn.php create mode 100644 src/Filters/Concerns/HasOperator.php diff --git a/src/Filters/Concerns/HasColumn.php b/src/Filters/Concerns/HasColumn.php new file mode 100644 index 0000000..ddb2a01 --- /dev/null +++ b/src/Filters/Concerns/HasColumn.php @@ -0,0 +1,21 @@ +column; + } +} diff --git a/src/Filters/Concerns/HasOperator.php b/src/Filters/Concerns/HasOperator.php new file mode 100644 index 0000000..159550e --- /dev/null +++ b/src/Filters/Concerns/HasOperator.php @@ -0,0 +1,73 @@ +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; + } +} diff --git a/src/Filters/Where.php b/src/Filters/Where.php index 7321138..84a80b6 100644 --- a/src/Filters/Where.php +++ b/src/Filters/Where.php @@ -26,6 +26,8 @@ class Where implements Filter { use Concerns\DeserializesValue; + use Concerns\HasColumn; + use Concerns\HasOperator; use Concerns\IsSingular; /** @@ -33,16 +35,6 @@ class Where implements Filter */ private string $name; - /** - * @var string - */ - private string $column; - - /** - * @var string - */ - private string $operator; - /** * Create a new filter. * @@ -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 */ @@ -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 */ diff --git a/src/Filters/WhereIn.php b/src/Filters/WhereIn.php index d7d9033..40f4034 100644 --- a/src/Filters/WhereIn.php +++ b/src/Filters/WhereIn.php @@ -26,6 +26,7 @@ class WhereIn implements Filter { use Concerns\DeserializesValue; + use Concerns\HasColumn; use Concerns\HasDelimiter; /** @@ -33,11 +34,6 @@ class WhereIn implements Filter */ private string $name; - /** - * @var string - */ - private string $column; - /** * Create a new filter. * @@ -89,14 +85,6 @@ public function apply($query, $value) ); } - /** - * @return string - */ - protected function column(): string - { - return $this->column; - } - /** * Deserialize the fitler value. *