Skip to content

Commit

Permalink
AttributeFilterCollection: added support for arguments passed to defa…
Browse files Browse the repository at this point in the history
…ult filters
  • Loading branch information
JoshyPHP committed Mar 23, 2024
1 parent 9dc8b1a commit 5a93731
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
3 changes: 3 additions & 0 deletions docs/testdox.txt
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ Attribute Filter Collection (s9e\TextFormatter\Tests\Configurator\Collections\At
[x] get() throws an exception if the filter name is neither callable not starts with # and is entirely composed of letters and digits
[x] get() throws an exception on unknown filter
[x] get() returns a clone of the filter, not the original instance
[x] getDefaultFilter('range') returns a filter with no range set
[x] getDefaultFilter('range', [1, 5]) returns a filter with range 1...5
[x] getDefaultFilter('range', ['max' => 5, 'min' => 1]) returns a filter with range 1...5

Attribute List (s9e\TextFormatter\Tests\Configurator\Collections\AttributeList)
[x] Attribute names are normalized for storage
Expand Down
4 changes: 2 additions & 2 deletions src/Configurator/Collections/AttributeFilterCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function get($key)
* @param string $filterName Filter name, e.g. "int" or "color"
* @return AttributeFilter
*/
public static function getDefaultFilter($filterName)
public static function getDefaultFilter(string $filterName, ?array $constructorArgs = []): AttributeFilter
{
$filterName = ucfirst(strtolower($filterName));
$className = 's9e\\TextFormatter\\Configurator\\Items\\AttributeFilters\\' . $filterName . 'Filter';
Expand All @@ -59,7 +59,7 @@ public static function getDefaultFilter($filterName)
throw new InvalidArgumentException("Unknown attribute filter '" . $filterName . "'");
}

return new $className;
return new $className(...$constructorArgs);
}

/**
Expand Down
36 changes: 36 additions & 0 deletions tests/Configurator/Collections/AttributeFilterCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,40 @@ public function testGetClone()
$collection->get('#number')
);
}

/**
* @testdox getDefaultFilter('range') returns a filter with no range set
*/
public function testGetDefaultFilter()
{
$filter = AttributeFilterCollection::getDefaultFilter('range');
$this->assertEquals(
[],
$filter->getVars()
);
}

/**
* @testdox getDefaultFilter('range', [1, 5]) returns a filter with range 1...5
*/
public function testGetDefaultFilterConstructorPositionalArgs()
{
$filter = AttributeFilterCollection::getDefaultFilter('range', [1, 5]);
$this->assertEquals(
['min' => 1, 'max' => 5],
$filter->getVars()
);
}

/**
* @testdox getDefaultFilter('range', ['max' => 5, 'min' => 1]) returns a filter with range 1...5
*/
public function testGetDefaultFilterConstructorNamedArgs()
{
$filter = AttributeFilterCollection::getDefaultFilter('range', ['max' => 5, 'min' => 1]);
$this->assertEquals(
['min' => 1, 'max' => 5],
$filter->getVars()
);
}
}

0 comments on commit 5a93731

Please sign in to comment.