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
|
|
|
private static $c = []; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Class constructor |
59
|
|
|
* @param AnnotatedInterface $model |
60
|
|
|
* @param string $transformatorClass |
61
|
|
|
*/ |
62
|
137 |
|
public function __construct(AnnotatedInterface $model, $transformatorClass = TransformatorInterface::class, $meta = null) |
63
|
|
|
{ |
64
|
137 |
|
if (null === $meta) |
65
|
|
|
{ |
66
|
79 |
|
$this->meta = ManganMeta::create($model); |
67
|
|
|
} |
68
|
|
|
else |
69
|
|
|
{ |
70
|
135 |
|
$this->meta = $meta; |
71
|
|
|
} |
72
|
137 |
|
$this->transformatorClass = $transformatorClass; |
73
|
137 |
|
$this->model = $model; |
74
|
137 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get transformator class |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
135 |
|
public function getTransformatorClass() |
81
|
|
|
{ |
82
|
135 |
|
return $this->transformatorClass; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get model instance |
87
|
|
|
* @return AnnotatedInterface |
88
|
|
|
*/ |
89
|
137 |
|
public function getModel() |
90
|
|
|
{ |
91
|
137 |
|
return $this->model; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get Mangan meta data |
96
|
|
|
* @return ManganMeta |
97
|
|
|
*/ |
98
|
135 |
|
public function getMeta() |
99
|
|
|
{ |
100
|
135 |
|
return $this->meta; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* |
105
|
|
|
* @param string $name |
106
|
|
|
* @return mixed |
107
|
|
|
*/ |
108
|
137 |
|
public function getFor($name) |
109
|
|
|
{ |
110
|
137 |
|
$key = static::class . get_class($this->model) . $name . $this->transformatorClass; |
111
|
|
|
|
112
|
137 |
|
if (isset(self::$c[$key])) |
113
|
|
|
{ |
114
|
128 |
|
return self::$c[$key]; |
115
|
|
|
} |
116
|
|
|
|
117
|
75 |
|
if (!array_key_exists($name, $this->transformators)) |
118
|
|
|
{ |
119
|
75 |
|
if (!$this->meta->$name) |
120
|
|
|
{ |
121
|
|
|
throw new TransformatorException(sprintf('There is not metadata for field `%s` of model `%s`, have you declared this field?', $name, get_class($this->getModel()))); |
122
|
|
|
} |
123
|
75 |
|
$this->transformators[$name] = $this->_getTransformer($this->transformatorClass, $this->meta->type(), $this->meta->$name); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// Support for setting name in sanitizers etc. |
127
|
75 |
|
if ($this->transformators[$name] instanceof NameAwareInterface) |
128
|
|
|
{ |
129
|
|
|
$this->transformators[$name]->setName($name); |
130
|
|
|
} |
131
|
75 |
|
self::$c[$key] = $this->transformators[$name]; |
132
|
75 |
|
return $this->transformators[$name]; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get transformer |
137
|
|
|
* @param string $transformatorClass |
138
|
|
|
* @param DocumentTypeMeta $modelMeta |
139
|
|
|
* @param DocumentPropertyMeta $fieldMeta |
140
|
|
|
* @return object |
141
|
|
|
*/ |
142
|
|
|
abstract protected function _getTransformer($transformatorClass, DocumentTypeMeta $modelMeta, DocumentPropertyMeta $fieldMeta); |
143
|
|
|
} |
144
|
|
|
|