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
<?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;
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
use Maslosoft\Mangan\Exceptions\TransformatorException;
use Maslosoft\Mangan\Interfaces\NameAwareInterface;
use Maslosoft\Mangan\Interfaces\Transformators\TransformatorInterface;
use Maslosoft\Mangan\Meta\DocumentPropertyMeta;
use Maslosoft\Mangan\Meta\DocumentTypeMeta;
use Maslosoft\Mangan\Meta\ManganMeta;
/**
* Transformator
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
abstract class Transformator
{
/**
* Metadata for document
* @var ManganMeta
*/
private $meta = null;
/**
* Hash map of sanitizers
* @var object[]
*/
private $transformators = [];
/**
* Model
* @var object
*/
private $model = null;
/**
* Transormator class name
* @var string
*/
private $transformatorClass = TransformatorInterface::class;
private static $c = [];
/**
* Class constructor
* @param AnnotatedInterface $model
* @param string $transformatorClass
*/
public function __construct(AnnotatedInterface $model, $transformatorClass = TransformatorInterface::class, $meta = null)
{
if (null === $meta)
{
$this->meta = ManganMeta::create($model);
}
else
{
$this->meta = $meta;
}
$this->transformatorClass = $transformatorClass;
$this->model = $model;
}
/**
* Get transformator class
* @return string
*/
public function getTransformatorClass()
{
return $this->transformatorClass;
}
/**
* Get model instance
* @return AnnotatedInterface
*/
public function getModel()
{
return $this->model;
}
/**
* Get Mangan meta data
* @return ManganMeta
*/
public function getMeta()
{
return $this->meta;
}
/**
*
* @param string $name
* @return mixed
*/
public function getFor($name)
{
$key = static::class . get_class($this->model) . $name . $this->transformatorClass;
if (isset(self::$c[$key]))
{
return self::$c[$key];
}
if (!array_key_exists($name, $this->transformators))
{
if (!$this->meta->$name)
{
throw new TransformatorException(sprintf('There is not metadata for field `%s` of model `%s`, have you declared this field?', $name, get_class($this->getModel())));
}
$this->transformators[$name] = $this->_getTransformer($this->transformatorClass, $this->meta->type(), $this->meta->$name);
}
// Support for setting name in sanitizers etc.
if ($this->transformators[$name] instanceof NameAwareInterface)
{
$this->transformators[$name]->setName($name);
}
self::$c[$key] = $this->transformators[$name];
return $this->transformators[$name];
}
/**
* Get transformer
* @param string $transformatorClass
* @param DocumentTypeMeta $modelMeta
* @param DocumentPropertyMeta $fieldMeta
* @return object
*/
abstract protected function _getTransformer($transformatorClass, DocumentTypeMeta $modelMeta, DocumentPropertyMeta $fieldMeta);
}
API documentation generated by ApiGen