1<?php
2
3/**
4 * @see       https://github.com/laminas/laminas-filter for the canonical source repository
5 * @copyright https://github.com/laminas/laminas-filter/blob/master/COPYRIGHT.md
6 * @license   https://github.com/laminas/laminas-filter/blob/master/LICENSE.md New BSD License
7 */
8
9namespace Laminas\Filter;
10
11use Laminas\Stdlib\ArrayUtils;
12use Traversable;
13
14class Blacklist extends AbstractFilter
15{
16    /**
17     * @var bool
18     */
19    protected $strict = false;
20
21    /**
22     * @var array
23     */
24    protected $list = [];
25
26    /**
27     * @param null|array|Traversable $options
28     */
29    public function __construct($options = null)
30    {
31        if (null !== $options) {
32            $this->setOptions($options);
33        }
34    }
35
36    /**
37     * Determine whether the in_array() call should be "strict" or not. See in_array docs.
38     *
39     * @param  bool $strict
40     */
41    public function setStrict($strict = true)
42    {
43        $this->strict = (bool) $strict;
44    }
45
46    /**
47     * Returns whether the in_array() call should be "strict" or not. See in_array docs.
48     *
49     * @return boolean
50     */
51    public function getStrict()
52    {
53        return $this->strict;
54    }
55
56    /**
57     * Set the list of items to black-list.
58     *
59     * @param  array|Traversable $list
60     */
61    public function setList($list = [])
62    {
63        if (! is_array($list)) {
64            $list = ArrayUtils::iteratorToArray($list);
65        }
66
67        $this->list = $list;
68    }
69
70    /**
71     * Get the list of items to black-list
72     *
73     * @return array
74     */
75    public function getList()
76    {
77        return $this->list;
78    }
79
80    /**
81     * {@inheritDoc}
82     *
83     * Will return null if $value is present in the black-list. If $value is NOT present then it will return $value.
84     */
85    public function filter($value)
86    {
87        return in_array($value, $this->getList(), $this->getStrict()) ? null : $value;
88    }
89}
90