|
4 | 4 |
|
5 | 5 | namespace PHPModelGenerator\Model; |
6 | 6 |
|
| 7 | +use PHPModelGenerator\Exception\InvalidFilterException; |
| 8 | +use PHPModelGenerator\PropertyProcessor\Filter\FilterInterface; |
| 9 | +use PHPModelGenerator\PropertyProcessor\Filter\TrimFilter; |
7 | 10 | use PHPModelGenerator\Utils\ClassNameGenerator; |
8 | 11 | use PHPModelGenerator\Utils\ClassNameGeneratorInterface; |
9 | 12 | use PHPModelGenerator\Exception\ErrorRegistryException; |
@@ -34,13 +37,54 @@ class GeneratorConfiguration |
34 | 37 | protected $serialization = false; |
35 | 38 | /** @var ClassNameGeneratorInterface */ |
36 | 39 | protected $classNameGenerator; |
| 40 | + /** @var FilterInterface[] */ |
| 41 | + protected $filter; |
37 | 42 |
|
38 | 43 | /** |
39 | 44 | * GeneratorConfiguration constructor. |
40 | 45 | */ |
41 | 46 | public function __construct() |
42 | 47 | { |
43 | 48 | $this->classNameGenerator = new ClassNameGenerator(); |
| 49 | + |
| 50 | + $this->addFilter(new TrimFilter()); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Add an additional filter |
| 55 | + * |
| 56 | + * @param FilterInterface $filter |
| 57 | + * |
| 58 | + * @return $this |
| 59 | + * |
| 60 | + * @throws InvalidFilterException |
| 61 | + */ |
| 62 | + public function addFilter(FilterInterface $filter): self |
| 63 | + { |
| 64 | + // TODO: check accepted types |
| 65 | + if (!(count($filter->getFilter()) === 2) || |
| 66 | + !is_string($filter->getFilter()[0]) || |
| 67 | + !is_string($filter->getFilter()[1]) || |
| 68 | + !is_callable($filter->getFilter()) |
| 69 | + ) { |
| 70 | + throw new InvalidFilterException("Invalid filter callback for filter {$filter->getToken()}"); |
| 71 | + } |
| 72 | + |
| 73 | + $this->filter[$filter->getToken()] = $filter; |
| 74 | + |
| 75 | + return $this; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Get a filter by the given token |
| 80 | + * |
| 81 | + * @param string $token |
| 82 | + * |
| 83 | + * @return FilterInterface|null |
| 84 | + */ |
| 85 | + public function getFilter(string $token): ?FilterInterface |
| 86 | + { |
| 87 | + return $this->filter[$token] ?? null; |
44 | 88 | } |
45 | 89 |
|
46 | 90 | /** |
|
0 commit comments