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