Skip to content

Commit

Permalink
Around() Syntax (Range Shorthand)
Browse files Browse the repository at this point in the history
Around() is a Shorthand for Range() with between functionality utilizing 'lte' and 'gte' with a tolerance threshold.
  • Loading branch information
karemont authored and Jeroen-G committed Nov 25, 2024
1 parent 25cc12c commit 82dcdbb
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/Domain/Syntax/Around.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace JeroenG\Explorer\Domain\Syntax;

use Webmozart\Assert\Assert;

class Around implements SyntaxInterface {
private string $field;

private mixed $value;

private float $tolerance;

private bool $percentage;

private bool $date;

private ?float $boost;

public function __construct(string $field, $value, float $tolerance = 5, ?bool $percentage = false, ?bool $date = false, ?float $boost = 1.0) {
$this->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],
]];
}
}

0 comments on commit 82dcdbb

Please sign in to comment.