Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Added contstructor to suppressfilter #3594

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions library/Zend/Log/Filter/SuppressFilter.php
Expand Up @@ -10,6 +10,7 @@


namespace Zend\Log\Filter; namespace Zend\Log\Filter;


use Zend\Log\Exception;


/** /**
* @category Zend * @category Zend
Expand All @@ -23,6 +24,29 @@ class SuppressFilter implements FilterInterface
*/ */
protected $accept = true; protected $accept = true;


/**
* This is a simple boolean filter.
*
* @param int|array|Traversable $suppress
* @throws Exception\InvalidArgumentException
*/
public function __construct($suppress = false)
{
if ($suppress instanceof Traversable) {
$suppress = iterator_to_array($suppress);
}
if (is_array($suppress)) {
$suppress = isset($suppress['suppress']) ? $suppress['suppress'] : false;
}
if (!is_bool($suppress)) {
throw new Exception\InvalidArgumentException(sprintf(
'Suppress must be an boolean; received "%s"', gettype($suppress)
));
}

$this->suppress($suppress);
}

/** /**
* This is a simple boolean filter. * This is a simple boolean filter.
* *
Expand Down
20 changes: 20 additions & 0 deletions tests/ZendTest/Log/Filter/SuppressFilterTest.php
Expand Up @@ -30,6 +30,26 @@ public function testSuppressIsInitiallyOff()
$this->assertTrue($this->filter->filter(array())); $this->assertTrue($this->filter->filter(array()));
} }


public function testSuppressByConstructorBoolean()
{
$this->filter = new SuppressFilter(true);
$this->assertFalse($this->filter->filter(array()));
$this->assertFalse($this->filter->filter(array()));
}

public function testSuppressByConstructorArray()
{
$this->filter = new SuppressFilter(array('suppress' => true));
$this->assertFalse($this->filter->filter(array()));
$this->assertFalse($this->filter->filter(array()));
}

public function testConstructorThrowsOnInvalidSuppressValue()
{
$this->setExpectedException('Zend\Log\Exception\InvalidArgumentException', 'Suppress must be an boolean');
new SuppressFilter('foo');
}

public function testSuppressOn() public function testSuppressOn()
{ {
$this->filter->suppress(true); $this->filter->suppress(true);
Expand Down