Skip to content

Commit

Permalink
Allow a setting of 'auto' for fuzziness. (#1)
Browse files Browse the repository at this point in the history
Allow a setting of 'auto' for fuzziness.
  • Loading branch information
m4olivei authored and pounard committed Sep 26, 2017
1 parent 31a7fb8 commit 31ecc79
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/AbstractFuzzyQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,30 @@
*/
abstract class AbstractFuzzyQuery extends AbstractQuery
{

/**
* Automatic fuzzy distance.
*/
const FUZZY_AUTO = 'auto';

/**
* @var int
* @var mixed
*/
protected $fuzzyness;

/**
* Set fuzzyness or roaming value, both uses the same operator, only the
* type of data (phrase or term) on which you apply it matters
*
* @param int $fuzzyness
* Positive integer or null to unset
* @param mixed $fuzzyness
* Positive integer or null to unset. You may also pass in 'auto' to leave
* the fuzzy distance up to the implementation.
*
* @return $this
*/
public function setFuzzyness($fuzzyness)
{
if ($fuzzyness != NULL && (! is_numeric($fuzzyness) || $fuzzyness < 0)) {
if ($fuzzyness != NULL && $fuzzyness !== self::FUZZY_AUTO && (! is_numeric($fuzzyness) || $fuzzyness < 0)) {
throw new \InvalidArgumentException("Fuzyness/roaming value must be a positive integer, " . print_r($fuzzyness, TRUE) . " given");
}

Expand Down Expand Up @@ -68,7 +75,12 @@ public function __toString()
$raw = $this->exclusion . $raw;
} else {
if (!empty($this->fuzzyness)) {
$raw .= '~' . $this->fuzzyness;
if ($this->fuzzyness === self::FUZZY_AUTO) {
$raw .= '~';
}
else {
$raw .= '~' . $this->fuzzyness;
}
}
if (!empty($this->boost)) {
$raw .= '^' . $this->boost;
Expand Down

0 comments on commit 31ecc79

Please sign in to comment.