diff --git a/README.md b/README.md index 7f31d3e3..6a53c540 100644 --- a/README.md +++ b/README.md @@ -337,6 +337,10 @@ The following options are supported by all filters except `Callback` and `Finder multiple values. If disabled, and multiple values are being passed, the filter will fall back to using the default value defined by the `defaultValue` option. +- `multiValueSeparator` (`string`, defaults to `null`) Defines whether the filter should + auto-tokenize multiple values using a specific separator string. If disabled, the data + must be an in form of an array. + - `field` (`string|array`), defaults to the name passed to the first argument of the add filter method) The name of the field to use for searching. Works like the base `field` option but also accepts multiple field names as an array. When defining @@ -389,6 +393,10 @@ The following options are supported by all filters except `Callback` and `Finder - `multiValue` (`bool`, defaults to `false`) Defines whether the filter accepts multiple values. If disabled, and multiple values are being passed, the filter will fall back to using the default value defined by the `defaultValue` option. + +- `multiValueSeparator` (`string`, defaults to `null`) Defines whether the filter should + auto-tokenize multiple values using a specific separator string. If disabled, the data + must be an in form of an array. - `mode` (`string`, defaults to `OR`) The conditional mode to use when matching against multiple fields. Valid values are `OR` and `AND`. diff --git a/src/Model/Filter/Base.php b/src/Model/Filter/Base.php index 50893d59..c7c44b58 100644 --- a/src/Model/Filter/Base.php +++ b/src/Model/Filter/Base.php @@ -51,6 +51,7 @@ abstract class Base * @param string $name Name. * @param \Search\Manager $manager Manager. * @param array $config Config. + * @throws \InvalidArgumentException */ public function __construct($name, Manager $manager, array $config = []) { @@ -65,6 +66,7 @@ public function __construct($name, Manager $manager, array $config = []) 'filterEmpty' => false, 'defaultValue' => null, 'multiValue' => false, + 'multiValueSeparator' => null, 'flatten' => true, ]; $config += $defaults; @@ -188,13 +190,32 @@ public function skip() public function value() { $value = $this->_config['defaultValue']; - if (isset($this->_args[$this->name()])) { - $passedValue = $this->_args[$this->name()]; - if (!is_array($passedValue) || - $this->getConfig('multiValue') - ) { - return $passedValue; - } + + $passedValue = $this->passedValue(); + if ($passedValue === null) { + return $value; + } + + return $passedValue; + } + + /** + * @return string|array|null + */ + protected function passedValue() + { + if (!isset($this->_args[$this->name()])) { + return null; + } + + $value = $this->_args[$this->name()]; + + if (is_array($value)) { + return $this->getConfig('multiValue') ? $value : null; + } + + if ($this->getConfig('multiValueSeparator')) { + return explode($this->getConfig('multiValueSeparator'), $value); } return $value; diff --git a/src/Model/Filter/Like.php b/src/Model/Filter/Like.php index 24bfc197..dd7b74ca 100644 --- a/src/Model/Filter/Like.php +++ b/src/Model/Filter/Like.php @@ -143,6 +143,7 @@ protected function _formatWildcards($value) * set configuration for escape driver name * * @return void + * @throws \InvalidArgumentException */ protected function _setEscaper() { diff --git a/tests/TestCase/Model/Filter/BaseTest.php b/tests/TestCase/Model/Filter/BaseTest.php index 013abfe5..f2174dd4 100644 --- a/tests/TestCase/Model/Filter/BaseTest.php +++ b/tests/TestCase/Model/Filter/BaseTest.php @@ -172,12 +172,58 @@ public function testValue() $filter->setArgs(['field' => ['value1', 'value2']]); $this->assertEquals('default', $filter->value()); + } + + /** + * @return void + */ + public function testValueMultiValue() + { + $filter = new TestFilter( + 'field', + $this->Manager, + ['defaultValue' => 'default'] + ); $filter->setConfig('multiValue', true); $filter->setArgs(['field' => ['value1', 'value2']]); $this->assertEquals(['value1', 'value2'], $filter->value()); } + /** + * @return void + */ + public function testValueMultiValueSeparator() + { + $filter = new TestFilter( + 'field', + $this->Manager, + ['defaultValue' => 'default'] + ); + + $filter->setConfig('multiValueSeparator', '|'); + + $filter->setArgs(['field' => 'value1|value2']); + $this->assertEquals(['value1', 'value2'], $filter->value()); + } + + /** + * @return void + */ + public function testValueMultiValueSeparatorInvalid() + { + $filter = new TestFilter( + 'field', + $this->Manager, + ['defaultValue' => 'default'] + ); + + $filter->setConfig('multiValue', true); + + $filter->setArgs(['field' => 'value1|value2']); + $this->assertEquals('value1|value2', $filter->value()); + } + /** * @return void */