MetadataFilter::filter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Doctrine\ODM\CouchDB\Tools\Console;
4
5
/**
6
 * Used by CLI Tools to restrict entity-based commands to given patterns.
7
 *
8
 * @since       1.0
9
 * @author      Benjamin Eberlei <[email protected]>
10
 * @author      Guilherme Blanco <[email protected]>
11
 * @author      Jonathan Wage <[email protected]>
12
 * @author      Roman Borschel <[email protected]>
13
 */
14
class MetadataFilter extends \FilterIterator implements \Countable
15
{
16
    /**
17
     * Filter Metadatas by one or more filter options.
18
     *
19
     * @param array $metadatas
20
     * @param array|string $filter
21
     * @return array
22
     */
23
    public static function filter(array $metadatas, $filter)
24
    {
25
        $metadatas = new MetadataFilter(new \ArrayIterator($metadatas), $filter);
26
        return iterator_to_array($metadatas);
27
    }
28
29
    /**
30
     * @var array
31
     */
32
    private $_filter = array();
33
34
    /**
35
     * @param \ArrayIterator $metadata
36
     * @param array|string $filter
37
     */
38
    public function __construct(\ArrayIterator $metadata, $filter)
39
    {
40
        $this->_filter = (array) $filter;
41
        parent::__construct($metadata);
42
    }
43
44
    /**
45
     * @return bool
46
     */
47
    public function accept()
48
    {
49
        if (count($this->_filter) == 0) {
50
            return true;
51
        }
52
53
        $it = $this->getInnerIterator();
54
        $metadata = $it->current();
55
56
        foreach ($this->_filter AS $filter) {
57
            if (strpos($metadata->name, $filter) !== false) {
58
                return true;
59
            }
60
        }
61
        return false;
62
    }
63
64
    /**
65
     * @return int
66
     */
67
    public function count()
68
    {
69
        return count($this->getInnerIterator());
70
    }
71
}
72