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

Transformator::getFor()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.1158

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 10
cts 12
cp 0.8333
rs 8.439
c 0
b 0
f 0
cc 5
eloc 12
nc 6
nop 1
crap 5.1158
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;
15
16
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
17
use Maslosoft\Mangan\Exceptions\TransformatorException;
18
use Maslosoft\Mangan\Interfaces\NameAwareInterface;
19
use Maslosoft\Mangan\Interfaces\Transformators\TransformatorInterface;
20
use Maslosoft\Mangan\Meta\DocumentPropertyMeta;
21
use Maslosoft\Mangan\Meta\DocumentTypeMeta;
22
use Maslosoft\Mangan\Meta\ManganMeta;
23
24
/**
25
 * Transformator
26
 *
27
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
28
 */
29
abstract class Transformator
30
{
31
32
	/**
33
	 * Metadata for document
34
	 * @var ManganMeta
35
	 */
36
	private $meta = null;
37
38
	/**
39
	 * Hash map of sanitizers
40
	 * @var object[]
41
	 */
42
	private $transformators = [];
43
44
	/**
45
	 * Model
46
	 * @var object
47
	 */
48
	private $model = null;
49
50
	/**
51
	 * Transormator class name
52
	 * @var string
53
	 */
54
	private $transformatorClass = TransformatorInterface::class;
55
56
	private static $c = [];
57
58
	/**
59
	 * Class constructor
60
	 * @param AnnotatedInterface $model
61
	 * @param string $transformatorClass
62
	 */
63 130
	public function __construct(AnnotatedInterface $model, $transformatorClass = TransformatorInterface::class, $meta = null)
64
	{
65 130
		if (null === $meta)
66
		{
67 74
			$this->meta = ManganMeta::create($model);
68
		}
69
		else
70
		{
71 128
			$this->meta = $meta;
72
		}
73 130
		$this->transformatorClass = $transformatorClass;
74 130
		$this->model = $model;
75 130
	}
76
77
	/**
78
	 * Get transformator class
79
	 * @return string
80
	 */
81 128
	public function getTransformatorClass()
82
	{
83 128
		return $this->transformatorClass;
84
	}
85
86
	/**
87
	 * Get model instance
88
	 * @return AnnotatedInterface
89
	 */
90 130
	public function getModel()
91
	{
92 130
		return $this->model;
93
	}
94
95
	/**
96
	 * Get Mangan meta data
97
	 * @return ManganMeta
98
	 */
99 128
	public function getMeta()
100
	{
101 128
		return $this->meta;
102
	}
103
104
	/**
105
	 *
106
	 * @param string $name
107
	 * @return mixed
108
	 */
109 130
	public function getFor($name)
110
	{
111 130
		$key = static::class . get_class($this->model) . $name . $this->transformatorClass;
112
113 130
		if(isset(self::$c[$key]))
114
		{
115 121
			return self::$c[$key];
116
		}
117
118 71
		if (!array_key_exists($name, $this->transformators))
119
		{
120 71
			if (!$this->meta->$name)
121
			{
122
				throw new TransformatorException(sprintf('There is not metadata for field `%s` of model `%s`, have you declared this field?', $name, get_class($this->getModel())));
123
			}
124 71
			$this->transformators[$name] = $this->_getTransformer($this->transformatorClass, $this->meta->type(), $this->meta->$name);
125
		}
126
127
		// Support for setting name in sanitizers etc.
128 71
		if ($this->transformators[$name] instanceof NameAwareInterface)
129
		{
130
			$this->transformators[$name]->setName($name);
131
		}
132 71
		self::$c[$key] = $this->transformators[$name];
133 71
		return $this->transformators[$name];
134
	}
135
136
	public function __get($name)
137
	{
138
		return $this->getList($name);
139
	}
140
141
	public function __set($name, $value)
142
	{
143
		throw new TransformatorException(sprintf('Cannot set field `%s` of `%s` (tried to set with value of type `%s`)', $name, __CLASS__, gettype($value)));
144
	}
145
146
	/**
147
	 * Get transformer
148
	 * @param string $transformatorClass
149
	 * @param DocumentTypeMeta $modelMeta
150
	 * @param DocumentPropertyMeta $fieldMeta
151
	 * @return object
152
	 */
153
	abstract protected function _getTransformer($transformatorClass, DocumentTypeMeta $modelMeta, DocumentPropertyMeta $fieldMeta);
154
}
155