Completed
Push — master ( 79b3f6...e22a5e )
by Peter
11:15
created

Factory   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 93.62%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 6
dl 0
loc 109
ccs 44
cts 47
cp 0.9362
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B create() 0 58 8
D _resolve() 0 45 9
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 70
	public static function create(DocumentPropertyMeta $meta, DocumentTypeMeta $modelMeta, $transformatorClass)
38
	{
39 70
		$key = $modelMeta->name . $modelMeta->connectionId . $meta->name . $transformatorClass;
40
41 70
		if(isset(self::$c[$key]))
42
		{
43
			return self::$c[$key];
44
		}
45
46 70
		$sanitizerClass = self::_resolve($meta, $modelMeta);
47
48
49
		// Remap sanitizer if needed
50 70
		$mapConfig = [];
51 70
		$map = Mangan::fly($modelMeta->connectionId)->sanitizersMap;
52 70
		if (isset($map[$transformatorClass]) && isset($map[$transformatorClass][$sanitizerClass]))
53
		{
54 12
			$mapConfig = $map[$transformatorClass][$sanitizerClass];
55 12
			if (is_string($mapConfig))
56
			{
57 12
				$mapClass = $mapConfig;
58
				$mapConfig = [
59 12
					'class' => $mapClass
60
				];
61
			}
62
		}
63 70
		if (is_array($meta->sanitizer))
64
		{
65 58
			$sanitizerConfig = $meta->sanitizer;
66 58
			$sanitizerConfig['class'] = $sanitizerClass;
67
		}
68
		else
69
		{
70 64
			$sanitizerConfig = [];
71 64
			$sanitizerConfig['class'] = $sanitizerClass;
72
		}
73 70
		if (!empty($mapConfig))
74
		{
75 12
			$sanitizerConfig = array_merge($sanitizerConfig, $mapConfig);
76
		}
77
78
		// Sanitize as array
79 70
		if ($meta->sanitizeArray)
80
		{
81
			$sanitizerConfig = [
82 1
				'class' => ArraySanitizer::class,
83 1
				'sanitizer' => $sanitizerConfig
84
			];
85
		}
86
		$config = [
87
			$transformatorClass => [
88 70
				$sanitizerConfig
89
			]
90
		];
91 70
		$sanitizer = PluginFactory::fly($modelMeta->connectionId)->instance($config, $transformatorClass)[0];
92 70
		self::$c[$key] = $sanitizer;
93 70
		return $sanitizer;
94
	}
95
96 70
	private static function _resolve(DocumentPropertyMeta $meta, DocumentTypeMeta $modelMeta)
97
	{
98
		// Sanitizer is explicitly set
99 70
		if (!empty($meta->sanitizer))
100
		{
101 58
			if (is_array($meta->sanitizer))
102
			{
103 58
				$name = $meta->sanitizer['class'];
104
			}
105
			else
106
			{
107
				$name = $meta->sanitizer;
108
			}
109
			// If short name is used add default namespace
110 58
			if (strstr($name, '\\') === false)
111
			{
112 14
				$className = sprintf('%s\%s', PassThrough::Ns, $name);
113
			}
114
			else
115
			{
116 48
				$className = $name;
117
			}
118 58
			if (!ClassChecker::exists($className))
119
			{
120
				throw new ManganException(sprintf("Sanitizer class `%s` not found for field `%s` in model `%s`", $className, $meta->name, $modelMeta->name));
121
			}
122 58
			return $className;
123
		}
124
125
		// Guess sanitizer
126 64
		switch (gettype($meta->default))
127
		{
128 64
			case 'boolean':
129 29
				return BooleanSanitizer::class;
130 63
			case 'double':
131 8
				return DoubleSanitizer::class;
132 63
			case 'integer':
133 26
				return IntegerSanitizer::class;
134 61
			case 'string':
135 58
				return StringSanitizer::class;
136
		}
137
138
		// Fallback to passthrough
139 35
		return PassThrough::class;
140
	}
141
142
}
143