1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Bukashk0zzzFilterBundle |
4
|
|
|
* |
5
|
|
|
* (c) Denis Golubovskiy <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Bukashk0zzz\FilterBundle\Service; |
12
|
|
|
|
13
|
|
|
use Bukashk0zzz\FilterBundle\Annotation\FilterAnnotation; |
14
|
|
|
use Doctrine\Common\Annotations\CachedReader; |
15
|
|
|
use Doctrine\Common\Util\ClassUtils; |
16
|
|
|
use Zend\Filter\AbstractFilter; |
17
|
|
|
use Zend\Filter\FilterInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Filter |
21
|
|
|
*/ |
22
|
|
|
class Filter |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var CachedReader Cached annotation reader |
26
|
|
|
*/ |
27
|
|
|
protected $annotationReader; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Filter constructor. |
31
|
|
|
* |
32
|
|
|
* @param CachedReader $annotationReader |
33
|
|
|
*/ |
34
|
|
|
public function __construct(CachedReader $annotationReader) |
35
|
|
|
{ |
36
|
|
|
$this->annotationReader = $annotationReader; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param mixed $object |
41
|
|
|
*/ |
42
|
|
|
public function filterEntity($object): void |
43
|
|
|
{ |
44
|
|
|
if ($object === null) { |
45
|
|
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$reflectionClass = ClassUtils::newReflectionClass(\get_class($object)); |
49
|
|
|
foreach ($reflectionClass->getProperties() as $property) { |
50
|
|
|
foreach ($this->annotationReader->getPropertyAnnotations($property) as $annotation) { |
51
|
|
|
if ($annotation instanceof FilterAnnotation) { |
52
|
|
|
$property->setAccessible(true); |
53
|
|
|
$value = $property->getValue($object); |
54
|
|
|
|
55
|
|
|
if ($value) { |
56
|
|
|
$filter = $annotation->getFilter(); |
57
|
|
|
$options = $annotation->getOptions(); |
58
|
|
|
$property->setValue($object, $this->getZendInstance($filter, $options)->filter($value)); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $class |
67
|
|
|
* @param mixed[]|null $options |
68
|
|
|
* |
69
|
|
|
* @return \Zend\Filter\FilterInterface |
70
|
|
|
*/ |
71
|
|
|
protected function getZendInstance(string $class, ?array $options): FilterInterface |
72
|
|
|
{ |
73
|
|
|
/** @var AbstractFilter $filter */ |
74
|
|
|
$filter = new $class(); |
75
|
|
|
|
76
|
|
|
$abstractFilterClass = AbstractFilter::class; |
77
|
|
|
if (!$filter instanceof $abstractFilterClass) { |
78
|
|
|
throw new \InvalidArgumentException("Filter class must extend $abstractFilterClass: $class"); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
try { |
82
|
|
|
if ($options !== null && \count($options) !== 0) { |
83
|
|
|
$filter->setOptions($options); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $filter; |
87
|
|
|
} catch (\Throwable $e) { |
88
|
|
|
return new $class($options); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|