Skip to content

Commit

Permalink
[zend-form] fix instantaing filterPHP 8.0+ changes behaviour of Refle…
Browse files Browse the repository at this point in the history
…ctionClass::newInstanceArgs (#194)

Copied logic from addValidator() to addFilter() which checks for array arguments and wraps them with additional array when necessary.
It's interesting that `addValidator()` was made future-proof already long time ago (even before the github history goes) but `addFilter()` was not
  • Loading branch information
marcing authored Feb 25, 2024
1 parent 65b9409 commit c8d2177
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
17 changes: 16 additions & 1 deletion packages/zend-form/library/Zend/Form/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -2113,7 +2113,22 @@ protected function _loadFilter(array $filter)
} else {
$r = new ReflectionClass($name);
if ($r->hasMethod('__construct')) {
$instance = $r->newInstanceArgs((array) $filter['options']);
$numeric = false;
if (is_array($filter['options'])) {
$keys = array_keys($filter['options']);
foreach($keys as $key) {
if (is_numeric($key)) {
$numeric = true;
break;
}
}
}

if ($numeric) {
$instance = $r->newInstanceArgs((array) $filter['options']);
} else {
$instance = $r->newInstance($filter['options']);
}
} else {
$instance = $r->newInstance();
}
Expand Down
48 changes: 45 additions & 3 deletions tests/Zend/Form/ElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1226,15 +1226,57 @@ public function testOptionsAreCastToArrayWhenAddingFilter()
$this->assertTrue($filter->allowWhiteSpace);
}

public function testShouldUseFilterConstructorOptionsAsPassedToAddFilter()
public function testCanPassFirstAndSecondArgument()
{
$this->element->addFilter('HtmlEntities', array(array('quotestyle' => ENT_QUOTES, 'charset' => 'UTF-8')));
try {
// public function __construct($searchSeparator = ' ', $replacementSeparator = '-')
$this->element->addFilter('Word_SeparatorToSeparator', array('-', '_'));
} catch (Exception $e) {
$this->fail('Should be able to add array filter options');
}
$filter = $this->element->getFilter('SeparatorToSeparator');
$this->assertEquals('-', $filter->getSearchSeparator());
$this->assertEquals('_', $filter->getReplacementSeparator());
}

public function testCanPassFirstOptionAsFirstArgument()
{
try {
// public function __construct($options = null)
$this->element->addFilter('Boolean', array('type' => Zend_Filter_Boolean::PHP + Zend_Filter_Boolean::FALSE_STRING));
} catch (Exception $e) {
$this->fail('Should be able to add array filter options');
}
$filter = $this->element->getFilter('Boolean');
$this->assertTrue($filter instanceof Zend_Filter_Boolean);
$this->assertSame($filter->getType(), Zend_Filter_Boolean::PHP + Zend_Filter_Boolean::FALSE_STRING);
}

public function testCanPassMultipleOptionsAsAnAssociativeArray()
{
// public function __construct($options = array())
$this->element->addFilter('HtmlEntities', array('quotestyle' => ENT_QUOTES, 'charset' => 'MacRoman'));
$filter = $this->element->getFilter('HtmlEntities');
$this->assertTrue($filter instanceof Zend_Filter_HtmlEntities);
$this->assertEquals(ENT_QUOTES, $filter->getQuoteStyle());
$this->assertEquals('UTF-8', $filter->getCharSet());
$this->assertEquals('MacRoman', $filter->getCharSet());
}

public function testAnyOptionIsHandledByConstructorWhenPassedInOptionsArray()
{
try {
// pass an option, which is not the same as first argument, and it's value is not the same as default one
// to check if it's really set
// public function __construct($options = array())
$this->element->addFilter('HtmlEntities', array('encoding' => 'MacRoman'));
} catch (Exception $e) {
$this->fail('Should be able to add array filter options');
}
$filter = $this->element->getFilter('HtmlEntities');
$this->assertTrue($filter instanceof Zend_Filter_HtmlEntities);
$this->assertSame($filter->getEncoding(), 'MacRoman');
}

public function testCanAddMultipleFilters()
{
$this->_checkZf2794();
Expand Down

0 comments on commit c8d2177

Please sign in to comment.