FilterAnnotation::setOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php declare(strict_types = 1);
2
3
namespace Bukashk0zzz\FilterBundle\Annotation;
4
5
use Attribute;
6
use Doctrine\ORM\Mapping\Annotation;
7
use Doctrine\ORM\Mapping\MappingAttribute;
8
9
/**
10
 * FilterAnnotation.
11
 *
12
 * @Annotation()
13
 * @Target({"PROPERTY", "METHOD"})
14
 */
15
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
16
final class FilterAnnotation implements MappingAttribute
17
{
18
    /**
19
     * @var string LaminasFilter name
20
     */
21
    private $filter;
22
23
    /**
24
     * @var array<mixed> Options
25
     */
26
    private $options;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param array<mixed> $parameters Filter parameters
32
     */
33
    public function __construct(array $parameters)
34
    {
35
        if (!\array_key_exists('value', $parameters) && !\array_key_exists('filter', $parameters)) {
36
            throw new \LogicException('Either "value" or "filter" option must be set.');
37
        }
38
39
        if (\array_key_exists('value', $parameters)) {
40
            $this->setFilter($parameters['value']);
41
        } elseif (\array_key_exists('filter', $parameters)) {
42
            $this->setFilter($parameters['filter']);
43
        }
44
45
        if (!\array_key_exists('options', $parameters)) {
46
            return;
47
        }
48
49
        $this->setOptions($parameters['options']);
50
    }
51
52
    public function getFilter(): string
53
    {
54
        return $this->filter;
55
    }
56
57
    public function setFilter(mixed $filter): self
58
    {
59
        if (!\is_string($filter)) {
60
            throw new \InvalidArgumentException('Filter must be string');
61
        }
62
63
        if ($filter && \mb_strpos($filter, '\\') === false) {
64
            $filter = 'Laminas\Filter\\'.$filter;
65
        }
66
67
        if (!\class_exists($filter)) {
68
            throw new \InvalidArgumentException("Could not find or autoload: {$filter}");
69
        }
70
71
        $this->filter = $filter;
72
73
        return $this;
74
    }
75
76
    /**
77
     * @return array<mixed>|null
78
     */
79
    public function getOptions(): ?array
80
    {
81
        return $this->options;
82
    }
83
84
    /**
85
     * @param array<mixed>|null $options
86
     */
87
    public function setOptions(?array $options): self
88
    {
89
        $this->options = $options;
90
91
        return $this;
92
    }
93
}
94