From 82dcdbbf542997b92d2769e6651a4852bc97dae7 Mon Sep 17 00:00:00 2001 From: Kareem Montaser <102875573+karemont@users.noreply.github.com> Date: Fri, 23 Jun 2023 19:31:24 +0300 Subject: [PATCH] Around() Syntax (Range Shorthand) Around() is a Shorthand for Range() with between functionality utilizing 'lte' and 'gte' with a tolerance threshold. --- src/Domain/Syntax/Around.php | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/Domain/Syntax/Around.php diff --git a/src/Domain/Syntax/Around.php b/src/Domain/Syntax/Around.php new file mode 100644 index 0000000..8e2b029 --- /dev/null +++ b/src/Domain/Syntax/Around.php @@ -0,0 +1,46 @@ +field = $field; + $this->value = $value; + $this->tolerance = $tolerance; + $this->percentage = $percentage; + $this->date = $date; + $this->boost = $boost; + } + + public function build(): array { + if ($this->date) { + $days = (int) $this->tolerance; + $lte = date('Y-m-d H:i:s', strtotime("+{$days} day", strtotime($this->value))); + $gte = date('Y-m-d H:i:s', strtotime("-{$days} day", strtotime($this->value))); + } else { + $modifier = $this->percentage ? $this->value * $this->tolerance : $this->tolerance; + $lte = $this->value + $modifier; + $gte = $this->value - $modifier; + } + + return ['range' => [ + $this->field => ['lte' => $lte, 'gte' => $gte, 'boost' => $this->boost], + ]]; + } +}