1<?php
2
3/**
4 * @see       https://github.com/laminas/laminas-log for the canonical source repository
5 * @copyright https://github.com/laminas/laminas-log/blob/master/COPYRIGHT.md
6 * @license   https://github.com/laminas/laminas-log/blob/master/LICENSE.md New BSD License
7 */
8
9namespace Laminas\Log\Filter;
10
11use Laminas\Log\Exception;
12use Traversable;
13
14class SuppressFilter implements FilterInterface
15{
16    /**
17     * @var bool
18     */
19    protected $accept = true;
20
21    /**
22     * This is a simple boolean filter.
23     *
24     * @param int|array|Traversable $suppress
25     * @throws Exception\InvalidArgumentException
26     */
27    public function __construct($suppress = false)
28    {
29        if ($suppress instanceof Traversable) {
30            $suppress = iterator_to_array($suppress);
31        }
32        if (is_array($suppress)) {
33            $suppress = isset($suppress['suppress']) ? $suppress['suppress'] : false;
34        }
35        if (! is_bool($suppress)) {
36            throw new Exception\InvalidArgumentException(
37                sprintf('Suppress must be a boolean; received "%s"', gettype($suppress))
38            );
39        }
40
41        $this->suppress($suppress);
42    }
43
44    /**
45     * This is a simple boolean filter.
46     *
47     * Call suppress(true) to suppress all log events.
48     * Call suppress(false) to accept all log events.
49     *
50     * @param  bool $suppress Should all log events be suppressed?
51     * @return void
52     */
53    public function suppress($suppress)
54    {
55        $this->accept = ! (bool) $suppress;
56    }
57
58    /**
59     * Returns TRUE to accept the message, FALSE to block it.
60     *
61     * @param array $event event data
62     * @return bool accepted?
63     */
64    public function filter(array $event)
65    {
66        return $this->accept;
67    }
68}
69