AbstractListFilter.php 1.65 KB
Newer Older
1
<?php declare(strict_types=1);
2
/*
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
3
4
 * Copyright (C) 2016-2017 IRSTEA
 * All rights reserved.
5
6
7
8
9
10
11
12
 */

namespace Irstea\PlantUmlBundle\Model\Filter;

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

/**
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
13
 * Description of AbstractListFilter.
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
 */
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;
            }
        }
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
41

42
43
44
        return $this->notFound;
    }

45
46
47
48
49
50
51
52
53
    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;
    }

54
55
    /**
     * @param string $value
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
56
     *
57
58
59
60
61
62
     * @return string
     */
    abstract protected function normalize($value);

    /**
     * @param ReflectionClass $class
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
63
     *
64
65
66
67
68
69
     * @return string
     */
    abstract protected function extract(ReflectionClass $class);

    /**
     * @param string $value
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
70
71
72
     * @param mixed  $tested
     * @param mixed  $reference
     *
73
74
75
76
     * @return bool
     */
    abstract protected function matches($tested, $reference);
}