Skip to content

Commit

Permalink
Merge pull request #240 from FriendsOfCake/feature/mv-separator
Browse files Browse the repository at this point in the history
Add multi value separator
  • Loading branch information
ADmad authored Oct 9, 2018
2 parents cc2decf + 10fa0d5 commit f4a6b15
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`.
Expand Down
35 changes: 28 additions & 7 deletions src/Model/Filter/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [])
{
Expand All @@ -65,6 +66,7 @@ public function __construct($name, Manager $manager, array $config = [])
'filterEmpty' => false,
'defaultValue' => null,
'multiValue' => false,
'multiValueSeparator' => null,
'flatten' => true,
];
$config += $defaults;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/Model/Filter/Like.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ protected function _formatWildcards($value)
* set configuration for escape driver name
*
* @return void
* @throws \InvalidArgumentException
*/
protected function _setEscaper()
{
Expand Down
46 changes: 46 additions & 0 deletions tests/TestCase/Model/Filter/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit f4a6b15

Please sign in to comment.