Skip to content

Commit

Permalink
ParamBag: Avoid duplicate parameters after merge (vufind-org#3368)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrojan-ub authored Apr 3, 2024
1 parent d66bcdd commit 5ae871e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
10 changes: 7 additions & 3 deletions module/VuFindSearch/src/VuFindSearch/ParamBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,13 @@ public function remove($name)
/**
* Add parameter value.
*
* @param string $name Parameter name
* @param mixed $value Parameter value
* @param string $name Parameter name
* @param mixed $value Parameter value
* @param bool $deduplicate Deduplicate parameter values
*
* @return void
*/
public function add($name, $value)
public function add($name, $value, $deduplicate = true)
{
if (!isset($this->params[$name])) {
$this->params[$name] = [];
Expand All @@ -166,6 +167,9 @@ public function add($name, $value)
} else {
$this->params[$name][] = $value;
}
if ($deduplicate) {
$this->params[$name] = array_values(array_unique($this->params[$name]));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,39 @@ public function testCountability()
$bag->set('xyzzy', 'baz');
$this->assertCount(2, $bag);
}

/**
* Test deduplication
*
* @return void
*/
public function testDeduplication()
{
$bag = new ParamBag();
$bag->add('foo', 'bar');
$bag->add('foo', 'bar');
$bag->add('foo', ['bar', 'bar', 'bar']);
$this->assertEquals(['bar'], $bag->get('foo'));
$bag->add('foo', ['bar', 'baz', 'bar', 'baz']);
$this->assertEquals(['bar', 'baz'], $bag->get('foo'));
}

/**
* Test disabling deduplication
*
* @return void
*/
public function testDisabledDeduplication()
{
$bag = new ParamBag();
$bag->add('foo', 'bar', false);
$bag->add('foo', 'bar', false);
$bag->add('foo', ['bar', 'bar', 'bar'], false);
$this->assertEquals(['bar', 'bar', 'bar', 'bar', 'bar'], $bag->get('foo'));
$bag->add('foo', ['bar', 'baz', 'bar', 'baz'], false);
$this->assertEquals(['bar', 'bar', 'bar', 'bar', 'bar', 'bar', 'baz', 'bar', 'baz'], $bag->get('foo'));
// Now deduplicate everything:
$bag->add('foo', 'bar');
$this->assertEquals(['bar', 'baz'], $bag->get('foo'));
}
}

0 comments on commit 5ae871e

Please sign in to comment.