1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Bukashk0zzzLiipImagineSerializationBundle |
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\LiipImagineSerializationBundle\Annotation; |
12
|
|
|
|
13
|
|
|
use Doctrine\ORM\Mapping\Annotation; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* LiipImagineSerializableField |
17
|
|
|
* |
18
|
|
|
* @Annotation() |
19
|
|
|
* @Target({"PROPERTY", "METHOD"}) |
20
|
|
|
*/ |
21
|
|
|
final class LiipImagineSerializableField implements Annotation |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string|string[]|null LiipImagine Filter |
25
|
|
|
*/ |
26
|
|
|
private $filter; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string Field |
30
|
|
|
*/ |
31
|
|
|
private $vichUploaderField; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string Virtual Field |
35
|
|
|
*/ |
36
|
|
|
private $virtualField; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var mixed[] Options |
40
|
|
|
*/ |
41
|
|
|
private $options; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor |
45
|
|
|
* |
46
|
|
|
* @param mixed[] $options Options |
47
|
|
|
*/ |
48
|
|
|
public function __construct(array $options) |
49
|
|
|
{ |
50
|
|
|
$this->options = $options; |
51
|
|
|
|
52
|
|
|
if (!\array_key_exists('value', $this->options) && !\array_key_exists('filter', $this->options)) { |
53
|
|
|
throw new \LogicException(\sprintf('Either "value" or "filter" option must be set.')); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($this->checkOption('value', true)) { |
57
|
|
|
$this->setFilter($options['value']); |
58
|
|
|
} elseif ($this->checkOption('filter', true)) { |
59
|
|
|
$this->setFilter($this->options['filter']); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($this->checkOption('vichUploaderField', false)) { |
63
|
|
|
$this->setVichUploaderField($this->options['vichUploaderField']); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($this->checkOption('virtualField', false)) { |
67
|
|
|
$this->setVirtualField($this->options['virtualField']); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string|string[]|null |
73
|
|
|
*/ |
74
|
|
|
public function getFilter() |
75
|
|
|
{ |
76
|
|
|
return $this->filter; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string|string[] $filter |
81
|
|
|
*/ |
82
|
|
|
public function setFilter($filter): LiipImagineSerializableField |
83
|
|
|
{ |
84
|
|
|
$this->filter = $filter; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getVichUploaderField(): ?string |
90
|
|
|
{ |
91
|
|
|
return $this->vichUploaderField; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function setVichUploaderField(string $vichUploaderField): LiipImagineSerializableField |
95
|
|
|
{ |
96
|
|
|
$this->vichUploaderField = $vichUploaderField; |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getVirtualField(): ?string |
102
|
|
|
{ |
103
|
|
|
return $this->virtualField; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function setVirtualField(string $virtualField): LiipImagineSerializableField |
107
|
|
|
{ |
108
|
|
|
$this->virtualField = $virtualField; |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private function checkOption(string $optionName, bool $canBeArray): bool |
114
|
|
|
{ |
115
|
|
|
if (\array_key_exists($optionName, $this->options)) { |
116
|
|
|
if (!\is_string($this->options[$optionName])) { |
117
|
|
|
if ($canBeArray && !\is_array($this->options[$optionName])) { |
118
|
|
|
throw new \InvalidArgumentException(\sprintf(\sprintf('Option "%s" must be a array or string.', $optionName))); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (!$canBeArray) { |
122
|
|
|
throw new \InvalidArgumentException(\sprintf(\sprintf('Option "%s" must be a string.', $optionName))); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|