1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under AGPL or Commercial license. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/mangan |
7
|
|
|
* @licence AGPL or Commercial |
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> |
9
|
|
|
* @copyright Copyright (c) Maslosoft |
10
|
|
|
* @copyright Copyright (c) Others as mentioned in code |
11
|
|
|
* @link https://maslosoft.com/mangan/ |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Maslosoft\Mangan\Annotations; |
15
|
|
|
|
16
|
|
|
use Maslosoft\Addendum\Helpers\ParamsExpander; |
17
|
|
|
use Maslosoft\Addendum\Utilities\ClassChecker; |
18
|
|
|
use Maslosoft\Mangan\Meta\ManganPropertyAnnotation; |
19
|
|
|
use Maslosoft\Mangan\Sanitizers\PassThrough; |
20
|
|
|
use UnexpectedValueException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Use `Sanitizer` annotation to enforce particular data type. |
24
|
|
|
* |
25
|
|
|
* There are numerous built-in sanitizers which can be used, as well as any |
26
|
|
|
* custom sanitizer can be build. First annotation value must be sanitizer class |
27
|
|
|
* literal, class name as string or short string literal based for built-in |
28
|
|
|
* sanitizers. Some sanitizers can also have some parameters. |
29
|
|
|
* To get list of parameters, read particular sanitizer documentation. |
30
|
|
|
* |
31
|
|
|
* **Note: There can be only one sanitizer per field.** |
32
|
|
|
* |
33
|
|
|
* Example usage: |
34
|
|
|
* ``` |
35
|
|
|
* @Sanitizer(MongoObjectId) |
36
|
|
|
* ``` |
37
|
|
|
* |
38
|
|
|
* For built-in sanitizers, also short string notation can be used, |
39
|
|
|
* without importing class: |
40
|
|
|
* |
41
|
|
|
* ``` |
42
|
|
|
* @Sanitizer('MongoObjectId') |
43
|
|
|
* ``` |
44
|
|
|
* |
45
|
|
|
* To skip variable sanitization either make default value `null` or define |
46
|
|
|
* `None` sanitizer: |
47
|
|
|
* ``` |
48
|
|
|
* @Sanitizer(None) |
49
|
|
|
* @Sanitizer('None') |
50
|
|
|
* ``` |
51
|
|
|
* |
52
|
|
|
* Example of using sanitizer with parameters: |
53
|
|
|
* ``` |
54
|
|
|
* @Sanitizer(MongoObjectId, nullable = true) |
55
|
|
|
* ``` |
56
|
|
|
* |
57
|
|
|
* @template Sanitizer(${SanitizerClass}) |
58
|
|
|
* @Target('property') |
59
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
60
|
|
|
*/ |
61
|
|
|
class SanitizerAnnotation extends ManganPropertyAnnotation |
62
|
|
|
{ |
63
|
|
|
|
64
|
|
|
public $value = null; |
65
|
|
|
public $class; |
66
|
|
|
|
67
|
93 |
|
public function init() |
68
|
|
|
{ |
69
|
|
|
$params = [ |
70
|
93 |
|
'class' |
71
|
|
|
]; |
72
|
93 |
|
if (is_string($this->value)) |
73
|
|
|
{ |
74
|
93 |
|
$this->class = $this->value; |
75
|
|
|
} |
76
|
7 |
|
elseif (is_array($this->value)) |
77
|
|
|
{ |
78
|
7 |
|
foreach (array_keys($this->value) as $key) |
79
|
|
|
{ |
80
|
7 |
|
if (!is_numeric($key)) |
81
|
|
|
{ |
82
|
7 |
|
$params[] = $key; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
93 |
|
$config = ParamsExpander::expand($this, $params); |
87
|
93 |
|
if (empty($config['class'])) |
88
|
|
|
{ |
89
|
|
|
throw new UnexpectedValueException(sprintf('@Sanitizer expects class name for model `%s` field `%s`', $this->getMeta()->type()->name, $this->getEntity()->name)); |
90
|
|
|
} |
91
|
93 |
|
elseif ($config['class'] !== 'None' && !ClassChecker::exists($config['class']) && !ClassChecker::exists(sprintf('%s\\%s', PassThrough::Ns, $config['class']))) |
92
|
|
|
{ |
93
|
1 |
|
throw new UnexpectedValueException(sprintf('Class `%s` for @Sanitizer not found on model `%s` field `%s`', $config['class'], $this->getMeta()->type()->name, $this->getEntity()->name)); |
94
|
|
|
} |
95
|
93 |
|
$this->getEntity()->sanitizer = $config; |
96
|
93 |
|
} |
97
|
|
|
|
98
|
|
|
} |
99
|
|
|
|