Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
FilterableTrait
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
6 / 6
11
100.00% covered (success)
100.00%
1 / 1
 addFilter
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 addFilters
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 hasFilters
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFilters
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 clearFilters
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 filterAll
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 filter
n/a
0 / 0
n/a
0 / 0
0
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Filter;
15
16/**
17 * Filterable trait
18 *
19 * @category   Pop
20 * @package    Pop\Filter
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    3.2.0
25 */
26trait FilterableTrait
27{
28
29    /**
30     * Form filters
31     * @var array
32     */
33    protected $filters = [];
34
35    /**
36     * Add filter
37     *
38     * @param  mixed $filter
39     * @throws \InvalidArgumentException
40     * @return FilterableTrait
41     */
42    public function addFilter($filter)
43    {
44        if (!($filter instanceof FilterInterface) && is_callable($filter)) {
45            $filter = new Filter($filter);
46        }
47        if (!($filter instanceof FilterInterface)) {
48            throw new \InvalidArgumentException(
49                'Error: The filter must be a callable or an instance of Pop\Filter\FilterInterface.'
50            );
51        }
52        $this->filters[] = $filter;
53        return $this;
54    }
55
56    /**
57     * Add filters
58     *
59     * @param  array $filters
60     * @return FilterableTrait
61     */
62    public function addFilters(array $filters)
63    {
64        foreach ($filters as $filter) {
65            $this->addFilter($filter);
66        }
67        return $this;
68    }
69
70    /**
71     * Has filters
72     *
73     * @return boolean
74     */
75    public function hasFilters()
76    {
77        return (count($this->filters) > 0);
78    }
79
80    /**
81     * Get filters
82     *
83     * @return array
84     */
85    public function getFilters()
86    {
87        return $this->filters;
88    }
89
90    /**
91     * Clear filters
92     *
93     * @return FilterableTrait
94     */
95    public function clearFilters()
96    {
97        $this->filters = [];
98        return $this;
99    }
100
101    /**
102     * Filter all values, ignoring excludes
103     *
104     * @param  array $values
105     * @return array
106     */
107    public function filterAll(array $values)
108    {
109        foreach ($this->filters as $filter) {
110            $values = array_map([$filter, 'filter'], $values);
111        }
112
113        return $values;
114    }
115
116    /**
117     * Filter values
118     *
119     * @param  mixed $values
120     * @return array
121     */
122    abstract public function filter($values);
123
124}