Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Like and Unlike filter classes for wildcard characters match #33

Merged
merged 1 commit into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 110 additions & 6 deletions src/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
use ipl\Stdlib\Filter\GreaterThanOrEqual;
use ipl\Stdlib\Filter\LessThan;
use ipl\Stdlib\Filter\LessThanOrEqual;
use ipl\Stdlib\Filter\Similar;
use ipl\Stdlib\Filter\None;
use ipl\Stdlib\Filter\Rule;
use ipl\Stdlib\Filter\Unequal;
use ipl\Stdlib\Filter\Unlike;

class Filter
{
Expand Down Expand Up @@ -146,8 +148,6 @@ protected function matchNone(None $rules, $row)
/**
* Create a rule that matches rows with a column that **equals** the given value
*
* Performs a wildcard search if the value contains asterisks.
*
* @param string $column
* @param array|bool|float|int|string $value
*
Expand Down Expand Up @@ -194,6 +194,57 @@ protected function matchEqual($rule, $row)
return false;
}

/**
* Create a rule that matches rows with a column that is **similar** to the given value
*
* Performs a wildcard search if the value contains asterisks.
*
* @param string $column
* @param string|string[] $value
*
* @return Condition
*/
public static function similar($column, $value)
{
return new Similar($column, $value);
}

/**
* Return whether the given rule's value is similar to the given item's value
*
* @param Similar|Unlike $rule
* @param object $row
*
* @return bool
*/
protected function matchSimilar($rule, $row)
{
if (! $rule instanceof Similar && ! $rule instanceof Unlike) {
throw new InvalidArgumentException(sprintf(
'Rule must be of type %s or %s, got %s instead',
Similar::class,
Unlike::class,
get_php_type($rule)
));
}

$rowValue = $this->extractValue($rule->getColumn(), $row);
$value = $rule->getValue();
$this->normalizeTypes($rowValue, $value);

if (! is_array($rowValue)) {
$rowValue = [$rowValue];
}

foreach ($rowValue as $rowVal) {
if ($this->performSimilarityMatch($value, $rowVal, $rule->ignoresCase())) {
return true;
}
}

return false;
}

/**
* Apply equality matching rules on the given row value
*
Expand All @@ -217,11 +268,34 @@ protected function performEqualityMatch($value, $rowValue, $ignoreCase = false)
} elseif (! is_string($value)) {
if (is_string($rowValue)) {
$value = (string) $value;
} else {
return $rowValue === $value;
}
}

return $rowValue === $value;
}

/**
* Apply similarity matching rules on the given row value
*
* @param string|string[] $value
* @param string $rowValue
* @param bool $ignoreCase
*
* @return bool
*/
protected function performSimilarityMatch($value, $rowValue, $ignoreCase = false)
{
if ($ignoreCase) {
$rowValue = strtolower($rowValue);
$value = is_array($value)
? array_map('strtolower', $value)
: strtolower($value);
}

if (is_array($value)) {
return in_array($rowValue, $value, true);
}

$wildcardSubSegments = preg_split('~\*~', $value);
if (count($wildcardSubSegments) === 1) {
return $rowValue === $value;
Expand All @@ -240,8 +314,6 @@ protected function performEqualityMatch($value, $rowValue, $ignoreCase = false)
/**
* Create a rule that matches rows with a column that is **unequal** with the given value
*
* Performs a wildcard search if the value contains asterisks.
*
* @param string $column
* @param array|bool|float|int|string $value
*
Expand All @@ -265,6 +337,34 @@ protected function matchUnequal(Unequal $rule, $row)
return ! $this->matchEqual($rule, $row);
}

/**
* Create a rule that matches rows with a column that is **unlike** with the given value
*
* Performs a wildcard search if the value contains asterisks.
*
* @param string $column
* @param string|string[] $value
*
* @return Condition
*/
public static function unlike($column, $value)
{
return new Unlike($column, $value);
}

/**
* Return whether the given rule's value is unlike the given item's value
*
* @param Unlike $rule
* @param object $row
*
* @return bool
*/
protected function matchUnlike(Unlike $rule, $row)
{
return ! $this->matchSimilar($rule, $row);
}

/**
* Create a rule that matches rows with a column that is **greater** than the given value
*
Expand Down Expand Up @@ -394,6 +494,8 @@ protected function performMatch(Rule $rule, $row)
return $this->matchAll($rule, $row);
case $rule instanceof Any:
return $this->matchAny($rule, $row);
case $rule instanceof Similar:
return $this->matchSimilar($rule, $row);
case $rule instanceof Equal:
return $this->matchEqual($rule, $row);
case $rule instanceof GreaterThan:
Expand All @@ -408,6 +510,8 @@ protected function performMatch(Rule $rule, $row)
return $this->matchNone($rule, $row);
case $rule instanceof Unequal:
return $this->matchUnequal($rule, $row);
case $rule instanceof Unlike:
return $this->matchUnlike($rule, $row);
default:
throw new InvalidArgumentException(sprintf(
'Unable to match filter. Rule type %s is unknown',
Expand Down
31 changes: 31 additions & 0 deletions src/Filter/Similar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace ipl\Stdlib\Filter;

class Similar extends Condition
{
/** @var bool */
protected $ignoreCase = false;

/**
* Ignore case on both sides of the equation
*
* @return $this
*/
public function ignoreCase()
{
$this->ignoreCase = true;

return $this;
}

/**
* Return whether this rule ignores case
*
* @return bool
*/
public function ignoresCase()
{
return $this->ignoreCase;
}
}
31 changes: 31 additions & 0 deletions src/Filter/Unlike.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace ipl\Stdlib\Filter;

class Unlike extends Condition
{
/** @var bool */
protected $ignoreCase = false;

/**
* Ignore case on both sides of the equation
*
* @return $this
*/
public function ignoreCase()
{
$this->ignoreCase = true;

return $this;
}

/**
* Return whether this rule ignores case
*
* @return bool
*/
public function ignoresCase()
{
return $this->ignoreCase;
}
}
Loading