diff --git a/src/Filter/Chain.php b/src/Filter/Chain.php index c0060e6..911f5f1 100644 --- a/src/Filter/Chain.php +++ b/src/Filter/Chain.php @@ -42,6 +42,35 @@ public function __clone() } } + public function sameAs(Rule $rule): bool + { + if (! $rule instanceof static) { + return false; + } + + if (count($rule) !== count($this->rules)) { + return false; + } + + $theirRules = iterator_to_array($rule); + foreach ($this->rules as $myRule) { + $found = false; + foreach ($theirRules as $i => $theirRule) { + if ($myRule->sameAs($theirRule)) { + unset($theirRules[$i]); + $found = true; + break; + } + } + + if (! $found) { + return false; + } + } + + return true; + } + /** * Get an iterator this chain's rules * diff --git a/src/Filter/Condition.php b/src/Filter/Condition.php index cc35610..b42b6c3 100644 --- a/src/Filter/Condition.php +++ b/src/Filter/Condition.php @@ -34,6 +34,13 @@ public function __clone() } } + public function sameAs(Rule $rule): bool + { + return $rule instanceof static + && $rule->getColumn() === $this->getColumn() + && $rule->getValue() === $this->getValue(); + } + /** * Set this condition's column * diff --git a/src/Filter/Equal.php b/src/Filter/Equal.php index 71da490..f1ed137 100644 --- a/src/Filter/Equal.php +++ b/src/Filter/Equal.php @@ -28,4 +28,21 @@ public function ignoresCase() { return $this->ignoreCase; } + + public function sameAs(Rule $rule): bool + { + if (! $rule instanceof static) { + return false; + } + + if ($this->ignoresCase() !== $rule->ignoresCase()) { + return false; + } + + if (is_array($this->value)) { + return array_diff($this->value, (array) $rule->getValue()) === []; + } + + return parent::sameAs($rule); + } } diff --git a/src/Filter/Like.php b/src/Filter/Like.php index 7a06279..9336921 100644 --- a/src/Filter/Like.php +++ b/src/Filter/Like.php @@ -28,4 +28,21 @@ public function ignoresCase() { return $this->ignoreCase; } + + public function sameAs(Rule $rule): bool + { + if (! $rule instanceof static) { + return false; + } + + if ($rule->ignoresCase() !== $this->ignoresCase()) { + return false; + } + + if (is_array($this->value)) { + return array_diff($this->value, (array) $rule->getValue()) === []; + } + + return parent::sameAs($rule); + } } diff --git a/src/Filter/Rule.php b/src/Filter/Rule.php index dc83c80..60b215b 100644 --- a/src/Filter/Rule.php +++ b/src/Filter/Rule.php @@ -4,4 +4,12 @@ interface Rule { + /** + * Get whether the given rule is semantically the same as this one + * + * @param Rule $rule + * + * @return bool + */ + public function sameAs(Rule $rule): bool; } diff --git a/src/Filter/Unequal.php b/src/Filter/Unequal.php index 5e37cbd..e231b8b 100644 --- a/src/Filter/Unequal.php +++ b/src/Filter/Unequal.php @@ -28,4 +28,21 @@ public function ignoresCase() { return $this->ignoreCase; } + + public function sameAs(Rule $rule): bool + { + if (! $rule instanceof static) { + return false; + } + + if ($this->ignoresCase() !== $rule->ignoresCase()) { + return false; + } + + if (is_array($this->value)) { + return array_diff($this->value, (array) $rule->getValue()) === []; + } + + return parent::sameAs($rule); + } } diff --git a/src/Filter/Unlike.php b/src/Filter/Unlike.php index 16b9fb3..1737ed6 100644 --- a/src/Filter/Unlike.php +++ b/src/Filter/Unlike.php @@ -28,4 +28,21 @@ public function ignoresCase() { return $this->ignoreCase; } + + public function sameAs(Rule $rule): bool + { + if (! $rule instanceof static) { + return false; + } + + if ($rule->ignoresCase() !== $this->ignoresCase()) { + return false; + } + + if (is_array($this->value)) { + return array_diff($this->value, (array) $rule->getValue()) === []; + } + + return parent::sameAs($rule); + } }