Commit 1f48e47d authored by Guillaume Perréal's avatar Guillaume Perréal

Factorisation de code dans les filtres.

parent bf0b3729
<?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;
}
/**
* @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);
}
......@@ -8,7 +8,6 @@
namespace Irstea\PlantUmlBundle\Model\Filter;
use Irstea\PlantUmlBundle\Model\ClassFilterInterface;
use ReflectionClass;
/**
......@@ -16,35 +15,20 @@ use ReflectionClass;
*
* @author Guillaume Perréal <guillaume.perreal@irstea.fr>
*/
class ClassFilter implements ClassFilterInterface
class ClassFilter extends AbstractListFilter
{
/**
* @var string[]
*/
private $classes = [];
/**
* @var boolena
*/
private $notFound;
protected function extract(ReflectionClass $class)
{
return $class->getName();
}
public function __construct(array $classes, $notFound = false)
protected function matches($tested, $reference)
{
$this->classes = array_map(
function ($class) {
return trim($class, '\\');
},
$classes
);
$this->notFound = $notFound;
return $tested === $reference;
}
public function accept(ReflectionClass $class)
protected function normalize($className)
{
$className = $class->getName();
if (in_array($className, $this->classes)) {
return !$this->notFound;
}
return $this->notFound;
return trim($className, '\\');
}
}
......@@ -8,7 +8,6 @@
namespace Irstea\PlantUmlBundle\Model\Filter;
use Irstea\PlantUmlBundle\Model\ClassFilterInterface;
use ReflectionClass;
/**
......@@ -16,37 +15,20 @@ use ReflectionClass;
*
* @author Guillaume Perréal <guillaume.perreal@irstea.fr>
*/
class DirectoryFilter implements ClassFilterInterface
class DirectoryFilter extends AbstractListFilter
{
/**
* @var string[]
*/
private $paths = [];
/**
* @var boolean
*/
private $notFound;
protected function extract(ReflectionClass $class)
{
return dirname($class->getFileName());
}
public function __construct(array $paths, $notFound = false)
protected function matches($tested, $reference)
{
$this->paths = array_map(
function ($path) {
return rtrim(strtr($path, '/\\', DIRECTORY_SEPARATOR), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
},
$paths
);
$this->notFound = $notFound;
return strpos($tested, $reference) === 0;
}
public function accept(ReflectionClass $class)
protected function normalize($path)
{
$filepath = dirname($class->getFileName());
foreach($this->paths as $path) {
if (strpos($filepath, $path) === 0) {
return !$this->notFound;
}
}
return $this->notFound;
return rtrim(strtr($path, '/\\', DIRECTORY_SEPARATOR), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
}
}
......@@ -8,7 +8,6 @@
namespace Irstea\PlantUmlBundle\Model\Filter;
use Irstea\PlantUmlBundle\Model\ClassFilterInterface;
use ReflectionClass;
/**
......@@ -16,37 +15,20 @@ use ReflectionClass;
*
* @author Guillaume Perréal <guillaume.perreal@irstea.fr>
*/
class NamespaceFilter implements ClassFilterInterface
class NamespaceFilter extends AbstractListFilter
{
/**
* @var string[]
*/
private $namespaces = [];
/**
* @var boolena
*/
private $notFound;
protected function extract(ReflectionClass $class)
{
return $class->getNamespaceName();
}
public function __construct(array $namespaces, $notFound = false)
protected function matches($tested, $reference)
{
$this->namespaces = array_map(
function ($namespace) {
return trim($namespace, '\\').'\\';
},
$namespaces
);
$this->notFound = $notFound;
return strpos($tested, $reference) === 0;
}
public function accept(ReflectionClass $class)
protected function normalize($namespace)
{
$className = $class->getNamespaceName().'\\';
foreach($this->namespaces as $namespace) {
if (strpos($className, $namespace) === 0) {
return !$this->notFound;
}
}
return $this->notFound;
return trim($namespace, '\\').'\\';
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment