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