AbstractListFilter.php 1.65 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php

/*
 * © 2016 IRSTEA
 * Guillaume Perréal <guillaume.perreal@irstea.fr>
 * Tous droits réservés.
 */

namespace Irstea\PlantUmlBundle\Model\Filter;

use Irstea\PlantUmlBundle\Model\ClassFilterInterface;
use ReflectionClass;

/**
 * Description of AbstractListFilter
 *
 * @author Guillaume Perréal <guillaume.perreal@irstea.fr>
 */
abstract class AbstractListFilter implements ClassFilterInterface
{
    /**
     * @var string[]
     */
    private $allowed = [];

    /**
     * @var boolena
     */
    private $notFound;

    public function __construct(array $allowed, $notFound = false)
    {
        $this->allowed = array_map([$this, 'normalize'], $allowed);
        $this->notFound = $notFound;
    }

    public function accept(ReflectionClass $class)
    {
        $tested = $this->normalize($this->extract($class));
        foreach ($this->allowed as $reference) {
            if ($this->matches($tested, $reference)) {
                return !$this->notFound;
            }
        }
        return $this->notFound;
    }

48
49
50
51
52
53
54
55
56
    public function toConfig(array &$conf)
    {
        $key = $this->notFound ? 'exclude' : 'include';
        if (!array_key_exists($key, $conf)) {
            $conf[$key] = [];
        }
        $conf[$key][static::CONF_TYPE] = $this->allowed;
    }

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    /**
     * @param string $value
     * @return string
     */
    abstract protected function normalize($value);

    /**
     * @param ReflectionClass $class
     * @return string
     */
    abstract protected function extract(ReflectionClass $class);

    /**
     * @param string $value
     * @return bool
     */
    abstract protected function matches($tested, $reference);
}