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 149 150 151
<?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\Decorator;
use Maslosoft\Mangan\Decorators\Undecorated;
use Maslosoft\Mangan\Exceptions\ManganException;
use Maslosoft\Mangan\Interfaces\Decorators\Model\ModelDecoratorInterface;
use Maslosoft\Mangan\Interfaces\Decorators\Property\DecoratorInterface;
use Maslosoft\Mangan\Mangan;
use Maslosoft\Mangan\Meta\DocumentPropertyMeta;
use Maslosoft\Mangan\Meta\DocumentTypeMeta;
use ReflectionClass;
/**
* Factory for creating decorators
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class Factory
{
/**
* Decorator class names
* @var bool[][][]
*/
private static $configs = [];
/**
* Model decorators
* @var ModelDecoratorInterface[]
*/
private static $modelDecorators = [];
private static $c = [];
/**
* Create decorator
* @param string $transformatorClass
* @param DocumentTypeMeta $modelMeta
* @param DocumentPropertyMeta $meta
* @return bool|CompoundDecorator|DecoratorInterface
*/
public static function createForField($transformatorClass, DocumentTypeMeta $modelMeta, DocumentPropertyMeta $meta)
{
$key = $modelMeta->name . $modelMeta->connectionId . $meta->name . $transformatorClass;
if(isset(self::$c[$key]))
{
return self::$c[$key];
}
if ($meta->decorators)
{
$activeDecorators = self::getManganDecorators($modelMeta->connectionId, $transformatorClass);
$decorators = [];
/**
* TODO This is workaround, it not should be required to do array_unique
* Further investigation needed
*/
if (!is_array($meta->decorators))
{
throw new ManganException('Meta `decorators` should be array');
}
foreach (array_unique($meta->decorators) as $decoratorName)
{
if (!isset($activeDecorators[$decoratorName]))
{
continue;
}
$decorators[] = new $decoratorName;
}
if ($decorators)
{
$decorator = new CompoundDecorator($decorators);
self::$c[$key] = $decorator;
return $decorator;
}
}
self::$c[$key] = false;
return false;
}
/**
* Create decorators for model. This returns all de
* @param string $transformatorClass
* @param DocumentTypeMeta $modelMeta
* @return CompoundModelDecorator
*/
public static function createForModel($transformatorClass, DocumentTypeMeta $modelMeta)
{
if (!isset(self::$modelDecorators[$modelMeta->connectionId]) || !isset(self::$modelDecorators[$modelMeta->connectionId][$transformatorClass]))
{
$decorators = [];
foreach (self::getManganDecorators($modelMeta->connectionId, $transformatorClass) as $decoratorName => $active)
{
if ((new ReflectionClass($decoratorName))->implementsInterface(ModelDecoratorInterface::class))
{
$decorators[] = new $decoratorName;
}
}
self::$modelDecorators[$modelMeta->connectionId][$transformatorClass] = new CompoundModelDecorator($decorators);
}
return self::$modelDecorators[$modelMeta->connectionId][$transformatorClass];
}
/**
* Get mangan decorators for selected connection id
* @param string $connectionId
* @param string $transformatorClass
* @return bool[]
*/
private static function getManganDecorators($connectionId, $transformatorClass)
{
if (!isset(self::$configs[$connectionId]))
{
self::$configs[$connectionId] = [];
}
if (!isset(self::$configs[$connectionId][$transformatorClass]))
{
self::$configs[$connectionId] = [];
self::$configs[$connectionId][$transformatorClass] = [];
$mangan = Mangan::fly($connectionId);
$transformator = new $transformatorClass;
foreach ($mangan->decorators as $implementer => $decoratorClasses)
{
foreach ($decoratorClasses as $decoratorClass)
{
if ($transformator instanceof $implementer)
{
self::$configs[$connectionId][$transformatorClass][$decoratorClass] = true;
}
}
}
}
return self::$configs[$connectionId][$transformatorClass];
}
}
API documentation generated by ApiGen