|
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\Helpers\Sanitizer; |
|
15
|
|
|
|
|
16
|
|
|
use Maslosoft\Addendum\Utilities\ClassChecker; |
|
17
|
|
|
use Maslosoft\Gazebo\PluginFactory; |
|
18
|
|
|
use Maslosoft\Mangan\Exceptions\ManganException; |
|
19
|
|
|
use Maslosoft\Mangan\Mangan; |
|
20
|
|
|
use Maslosoft\Mangan\Meta\DocumentPropertyMeta; |
|
21
|
|
|
use Maslosoft\Mangan\Meta\DocumentTypeMeta; |
|
22
|
|
|
use Maslosoft\Mangan\Sanitizers\ArraySanitizer; |
|
23
|
|
|
use Maslosoft\Mangan\Sanitizers\BooleanSanitizer; |
|
24
|
|
|
use Maslosoft\Mangan\Sanitizers\DoubleSanitizer; |
|
25
|
|
|
use Maslosoft\Mangan\Sanitizers\IntegerSanitizer; |
|
26
|
|
|
use Maslosoft\Mangan\Sanitizers\PassThrough; |
|
27
|
|
|
use Maslosoft\Mangan\Sanitizers\StringSanitizer; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Factory |
|
31
|
|
|
* |
|
32
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
33
|
|
|
*/ |
|
34
|
|
|
class Factory |
|
35
|
|
|
{ |
|
36
|
|
|
private static $c = []; |
|
37
|
98 |
|
public static function create(DocumentPropertyMeta $meta, DocumentTypeMeta $modelMeta, $transformatorClass) |
|
38
|
|
|
{ |
|
39
|
98 |
|
$key = $modelMeta->name . $modelMeta->connectionId . $meta->name . $transformatorClass; |
|
40
|
|
|
|
|
41
|
98 |
|
if(isset(self::$c[$key])) |
|
42
|
|
|
{ |
|
43
|
|
|
return self::$c[$key]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
98 |
|
$sanitizerClass = self::_resolve($meta, $modelMeta); |
|
47
|
98 |
|
if(false === $sanitizerClass) |
|
48
|
|
|
{ |
|
49
|
44 |
|
self::$c[$key] = false; |
|
50
|
44 |
|
return false; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
// Remap sanitizer if needed |
|
55
|
97 |
|
$mapConfig = []; |
|
56
|
97 |
|
$map = Mangan::fly($modelMeta->connectionId)->sanitizersMap; |
|
57
|
97 |
|
if (isset($map[$transformatorClass]) && isset($map[$transformatorClass][$sanitizerClass])) |
|
58
|
|
|
{ |
|
59
|
13 |
|
$mapConfig = $map[$transformatorClass][$sanitizerClass]; |
|
60
|
13 |
|
if (is_string($mapConfig)) |
|
61
|
|
|
{ |
|
62
|
13 |
|
$mapClass = $mapConfig; |
|
63
|
|
|
$mapConfig = [ |
|
64
|
13 |
|
'class' => $mapClass |
|
65
|
|
|
]; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
97 |
|
if (is_array($meta->sanitizer)) |
|
69
|
|
|
{ |
|
70
|
76 |
|
$sanitizerConfig = $meta->sanitizer; |
|
71
|
76 |
|
$sanitizerConfig['class'] = $sanitizerClass; |
|
72
|
|
|
} |
|
73
|
|
|
else |
|
74
|
|
|
{ |
|
75
|
80 |
|
$sanitizerConfig = []; |
|
76
|
80 |
|
$sanitizerConfig['class'] = $sanitizerClass; |
|
77
|
|
|
} |
|
78
|
97 |
|
if (!empty($mapConfig)) |
|
79
|
|
|
{ |
|
80
|
13 |
|
$sanitizerConfig = array_merge($sanitizerConfig, $mapConfig); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// Sanitize as array |
|
84
|
97 |
|
if ($meta->sanitizeArray) |
|
85
|
|
|
{ |
|
86
|
|
|
$sanitizerConfig = [ |
|
87
|
2 |
|
'class' => ArraySanitizer::class, |
|
88
|
2 |
|
'sanitizer' => $sanitizerConfig |
|
89
|
|
|
]; |
|
90
|
|
|
} |
|
91
|
|
|
$config = [ |
|
92
|
|
|
$transformatorClass => [ |
|
93
|
97 |
|
$sanitizerConfig |
|
94
|
|
|
] |
|
95
|
|
|
]; |
|
96
|
97 |
|
$sanitizer = PluginFactory::fly($modelMeta->connectionId)->instance($config, $transformatorClass)[0]; |
|
97
|
97 |
|
self::$c[$key] = $sanitizer; |
|
98
|
97 |
|
return $sanitizer; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
98 |
|
private static function _resolve(DocumentPropertyMeta $meta, DocumentTypeMeta $modelMeta) |
|
102
|
|
|
{ |
|
103
|
|
|
// Sanitizer is explicitly set |
|
104
|
98 |
|
if (!empty($meta->sanitizer)) |
|
105
|
|
|
{ |
|
106
|
76 |
|
if (is_array($meta->sanitizer)) |
|
107
|
|
|
{ |
|
108
|
76 |
|
$name = $meta->sanitizer['class']; |
|
109
|
|
|
} |
|
110
|
|
|
else |
|
111
|
|
|
{ |
|
112
|
|
|
$name = $meta->sanitizer; |
|
113
|
|
|
} |
|
114
|
|
|
// If short name is used add default namespace |
|
115
|
76 |
|
if (strstr($name, '\\') === false) |
|
116
|
|
|
{ |
|
117
|
16 |
|
$className = sprintf('%s\%s', PassThrough::Ns, $name); |
|
118
|
|
|
} |
|
119
|
|
|
else |
|
120
|
|
|
{ |
|
121
|
64 |
|
$className = $name; |
|
122
|
|
|
} |
|
123
|
76 |
|
if (!ClassChecker::exists($className)) |
|
124
|
|
|
{ |
|
125
|
|
|
throw new ManganException(sprintf("Sanitizer class `%s` not found for field `%s` in model `%s`", $className, $meta->name, $modelMeta->name)); |
|
126
|
|
|
} |
|
127
|
76 |
|
return $className; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
// Guess sanitizer |
|
131
|
87 |
|
switch (gettype($meta->default)) |
|
132
|
|
|
{ |
|
133
|
87 |
|
case 'boolean': |
|
134
|
33 |
|
return BooleanSanitizer::class; |
|
135
|
86 |
|
case 'double': |
|
136
|
8 |
|
return DoubleSanitizer::class; |
|
137
|
86 |
|
case 'integer': |
|
138
|
29 |
|
return IntegerSanitizer::class; |
|
139
|
83 |
|
case 'string': |
|
140
|
76 |
|
return StringSanitizer::class; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
// None |
|
144
|
44 |
|
return false; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
|