AbstractListFilter.php 2.58 KB
Newer Older
1
<?php declare(strict_types=1);
2
/*
Guillaume Perréal's avatar
CS.  
Guillaume Perréal committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * This file is part of "irstea/plantuml-bundle".
 *
 * Copyright (C) 2016-2019 IRSTEA
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License and the GNU
 * Lesser General Public License along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
19 20 21 22 23 24 25 26
 */

namespace Irstea\PlantUmlBundle\Model\Filter;

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

/**
Guillaume Perréal's avatar
CS.  
Guillaume Perréal committed
27
 * Description of AbstractListFilter.
28 29 30 31 32 33 34 35 36
 */
abstract class AbstractListFilter implements ClassFilterInterface
{
    /**
     * @var string[]
     */
    private $allowed = [];

    /**
37
     * @var bool
38 39 40
     */
    private $notFound;

41 42 43 44 45
    /**
     * AbstractListFilter constructor.
     * @param array $allowed
     * @param bool $notFound
     */
46 47 48 49 50 51
    public function __construct(array $allowed, $notFound = false)
    {
        $this->allowed = array_map([$this, 'normalize'], $allowed);
        $this->notFound = $notFound;
    }

52 53 54 55
    /**
     * @param ReflectionClass $class
     * @return bool
     */
56 57 58 59 60 61 62 63
    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
64

65 66 67
        return $this->notFound;
    }

68 69 70
    /**
     * @param array $conf
     */
71 72 73 74 75 76 77 78 79
    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;
    }

80 81
    /**
     * @param string $value
Guillaume Perréal's avatar
CS.  
Guillaume Perréal committed
82
     *
83 84
     * @return string
     */
85
    abstract protected function normalize($value): string;
86 87 88

    /**
     * @param ReflectionClass $class
Guillaume Perréal's avatar
CS.  
Guillaume Perréal committed
89
     *
90 91
     * @return string
     */
92
    abstract protected function extract(ReflectionClass $class): string;
93 94

    /**
95 96
     * @param mixed $tested
     * @param mixed $reference
Guillaume Perréal's avatar
CS.  
Guillaume Perréal committed
97
     *
98 99
     * @return bool
     */
100
    abstract protected function matches($tested, $reference): bool;
101
}