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

Factorisation de code dans les filtres.

No related merge requests found
Showing with 92 additions and 79 deletions
+92 -79
<?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 @@ ...@@ -8,7 +8,6 @@
namespace Irstea\PlantUmlBundle\Model\Filter; namespace Irstea\PlantUmlBundle\Model\Filter;
use Irstea\PlantUmlBundle\Model\ClassFilterInterface;
use ReflectionClass; use ReflectionClass;
/** /**
...@@ -16,35 +15,20 @@ use ReflectionClass; ...@@ -16,35 +15,20 @@ use ReflectionClass;
* *
* @author Guillaume Perréal <guillaume.perreal@irstea.fr> * @author Guillaume Perréal <guillaume.perreal@irstea.fr>
*/ */
class ClassFilter implements ClassFilterInterface class ClassFilter extends AbstractListFilter
{ {
/** protected function extract(ReflectionClass $class)
* @var string[] {
*/ return $class->getName();
private $classes = []; }
/**
* @var boolena
*/
private $notFound;
public function __construct(array $classes, $notFound = false) protected function matches($tested, $reference)
{ {
$this->classes = array_map( return $tested === $reference;
function ($class) {
return trim($class, '\\');
},
$classes
);
$this->notFound = $notFound;
} }
public function accept(ReflectionClass $class) protected function normalize($className)
{ {
$className = $class->getName(); return trim($className, '\\');
if (in_array($className, $this->classes)) {
return !$this->notFound;
}
return $this->notFound;
} }
} }
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
namespace Irstea\PlantUmlBundle\Model\Filter; namespace Irstea\PlantUmlBundle\Model\Filter;
use Irstea\PlantUmlBundle\Model\ClassFilterInterface;
use ReflectionClass; use ReflectionClass;
/** /**
...@@ -16,37 +15,20 @@ use ReflectionClass; ...@@ -16,37 +15,20 @@ use ReflectionClass;
* *
* @author Guillaume Perréal <guillaume.perreal@irstea.fr> * @author Guillaume Perréal <guillaume.perreal@irstea.fr>
*/ */
class DirectoryFilter implements ClassFilterInterface class DirectoryFilter extends AbstractListFilter
{ {
/** protected function extract(ReflectionClass $class)
* @var string[] {
*/ return dirname($class->getFileName());
private $paths = []; }
/**
* @var boolean
*/
private $notFound;
public function __construct(array $paths, $notFound = false) protected function matches($tested, $reference)
{ {
$this->paths = array_map( return strpos($tested, $reference) === 0;
function ($path) {
return rtrim(strtr($path, '/\\', DIRECTORY_SEPARATOR), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
},
$paths
);
$this->notFound = $notFound;
} }
public function accept(ReflectionClass $class) protected function normalize($path)
{ {
$filepath = dirname($class->getFileName()); return rtrim(strtr($path, '/\\', DIRECTORY_SEPARATOR), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
foreach($this->paths as $path) {
if (strpos($filepath, $path) === 0) {
return !$this->notFound;
}
}
return $this->notFound;
} }
} }
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
namespace Irstea\PlantUmlBundle\Model\Filter; namespace Irstea\PlantUmlBundle\Model\Filter;
use Irstea\PlantUmlBundle\Model\ClassFilterInterface;
use ReflectionClass; use ReflectionClass;
/** /**
...@@ -16,37 +15,20 @@ use ReflectionClass; ...@@ -16,37 +15,20 @@ use ReflectionClass;
* *
* @author Guillaume Perréal <guillaume.perreal@irstea.fr> * @author Guillaume Perréal <guillaume.perreal@irstea.fr>
*/ */
class NamespaceFilter implements ClassFilterInterface class NamespaceFilter extends AbstractListFilter
{ {
/** protected function extract(ReflectionClass $class)
* @var string[] {
*/ return $class->getNamespaceName();
private $namespaces = []; }
/**
* @var boolena
*/
private $notFound;
public function __construct(array $namespaces, $notFound = false) protected function matches($tested, $reference)
{ {
$this->namespaces = array_map( return strpos($tested, $reference) === 0;
function ($namespace) {
return trim($namespace, '\\').'\\';
},
$namespaces
);
$this->notFound = $notFound;
} }
public function accept(ReflectionClass $class) protected function normalize($namespace)
{ {
$className = $class->getNamespaceName().'\\'; return trim($namespace, '\\').'\\';
foreach($this->namespaces as $namespace) {
if (strpos($className, $namespace) === 0) {
return !$this->notFound;
}
}
return $this->notFound;
} }
} }
Supports Markdown
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