Skip to content

Commit

Permalink
issue #7 - add shortcuts for collections terms, unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pounard committed Sep 6, 2022
1 parent 25201f5 commit 9ed2029
Show file tree
Hide file tree
Showing 4 changed files with 276 additions and 230 deletions.
1 change: 0 additions & 1 deletion src/AbstractFuzzyQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/
abstract class AbstractFuzzyQuery extends AbstractQuery
{

/**
* Automatic fuzzy distance.
*/
Expand Down
229 changes: 229 additions & 0 deletions src/CollectionQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,235 @@ public function count(): int
return count($this->elements);
}

/**
* Create new term collection statement
*
* @return TermCollectionQuery
*/
public function createTermCollection($operator = Query::OP_AND)
{
$statement = new TermCollectionQuery();
$statement->setOperator($operator);

$this->add($statement);

return $statement;
}

/**
* Create new term collection statement
*
* @return CollectionQuery
*/
public function createCollection($operator = Query::OP_AND)
{
$statement = new CollectionQuery();
$statement->setOperator($operator);

$this->add($statement);

return $statement;
}

/**
* Create new term statement
*
* @return TermQuery
*/
public function createTerm()
{
$statement = new TermQuery();

$this->add($statement);

return $statement;
}

/**
* Create new arbitrary range statement
*
* @return RangeQuery
*/
public function createRange()
{
$statement = new RangeQuery();

$this->add($statement);

return $statement;
}

/**
* Create new arbitrary range statement
*
* @return DateRangeQuery
*/
public function createDateRange($field = null)
{
$statement = new DateRangeQuery();
$statement->setField($field);

$this->add($statement);

return $statement;
}

/**
* Match single term to this query
*
* @param string $field
* @param string|TermQuery $term
* @param float $boost
* @param float $fuzzyness
*
* @return $this
*/
public function matchTerm($field = null, $term = null, $boost = null, $fuzzyness = null)
{
$this
->createTerm()
->setValue($term)
->setFuzzyness($fuzzyness)
->setBoost($boost)
->setField($field);

return $this;
}

/**
* Require range
*
* @param string $field
* @param mixed $start
* @param mixed $stop
* @param boolean $inclusive
*
* @return $this
*/
public function requireRange($field = null, $start = null, $stop = null, $inclusive = true)
{
$this
->createRange()
->setField($field)
->setInclusive($inclusive)
->setRange($start, $stop);

return $this;
}

/**
* Require date range
*
* @param string $field
* @param int|string|\DateTime $start
* Timestamp, \DateTime parsable string or \DateTime object
* @param int|string|\DateTime $stop
* Timestamp, \DateTime parsable string or \DateTime object
* @param boolean $inclusive
*
* @return $this
*/
public function requireDateRange($field = null, $start = null, $stop = null, $inclusive = true)
{
$this
->createDateRange()
->setInclusive($inclusive)
->setRange($start, $stop)
->setField($field);

return $this;
}

/**
* Require single term to this query
*
* @param string $field
* @param string|TermQuery $term
*
* @return $this
*/
public function requireTerm($field = null, $term = null)
{
$this
->createTerm()
->setValue($term)
->setExclusion(self::OP_REQUIRE)
->setField($field);

return $this;
}

/**
* Prohibit single term to this query
*
* @param string $field
* @param string|TermQuery $term
*
* @return $this
*/
public function prohibitTerm($field = null, $term = null)
{
$this
->createTerm()
->setValue($term)
->setField($field)
->setExclusion(self::OP_PROHIBIT);

return $this;
}

/**
* Match term collection (OR by default)
*
* @param string $field
* @param string[]|TermQuery[] $terms
* @param float $boost
* @param string $operator
*
* @return $this
*/
public function matchTermCollection($field = null, $terms = [], $boost = null, $operator = self::OP_OR)
{
if (!is_array($terms)) {
$terms = [$terms];
}

$this
->createTermCollection()
->addAll($terms)
->setOperator($operator)
->setField($field)
->setBoost($boost);

return $this;
}

/**
* Require term collection (OR by default)
*
* @param string $field
* @param string[]|TermQuery[] $terms
* @param float $boost
* @param string $operator
*
* @return $this
*/
public function requireTermCollection($field = null, $terms = [], $operator = self::OP_OR)
{
if (!is_array($terms)) {
$terms = [$terms];
}

$this
->createTermCollection()
->addAll($terms)
->setOperator($operator)
->setField($field)
->setExclusion(self::OP_REQUIRE);

return $this;
}

/**
* Adds an element to the internal list
*
Expand Down
Loading

0 comments on commit 9ed2029

Please sign in to comment.