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\Decorator; |
15
|
|
|
|
16
|
|
|
use Maslosoft\Mangan\Interfaces\Decorators\Property\DecoratorInterface; |
17
|
|
|
use Maslosoft\Mangan\Helpers\Transformator; |
18
|
|
|
use Maslosoft\Mangan\Meta\DocumentPropertyMeta; |
19
|
|
|
use Maslosoft\Mangan\Meta\DocumentTypeMeta; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Decorator |
23
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
24
|
|
|
*/ |
25
|
|
|
class Decorator extends Transformator |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Read value from database |
30
|
|
|
* @param string $name |
31
|
|
|
* @param mixed $dbValue |
32
|
|
|
*/ |
33
|
116 |
|
public function read($name, &$dbValue) |
34
|
|
|
{ |
35
|
116 |
|
$decorator = $this->getFor($name); |
36
|
116 |
|
$model = $this->getModel(); |
37
|
116 |
|
if(empty($decorator)) |
38
|
|
|
{ |
39
|
116 |
|
$model->$name = $dbValue; |
40
|
116 |
|
return; |
41
|
|
|
} |
42
|
50 |
|
$decorator->read($model, $name, $dbValue, $this->getTransformatorClass()); |
43
|
50 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Write value into database |
47
|
|
|
* @param string $name |
48
|
|
|
* @param mixed $dbValue |
49
|
|
|
*/ |
50
|
165 |
|
public function write($name, &$dbValue) |
51
|
|
|
{ |
52
|
165 |
|
$decorator = $this->getFor($name); |
53
|
165 |
|
$model = $this->getModel(); |
54
|
165 |
|
if(empty($decorator)) |
55
|
|
|
{ |
56
|
156 |
|
$dbValue[$name] = $model->$name; |
57
|
156 |
|
return; |
58
|
|
|
} |
59
|
69 |
|
$decorator->write($model, $name, $dbValue, $this->getTransformatorClass()); |
60
|
69 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get transformer |
64
|
|
|
* @param string $transformatorClass |
65
|
|
|
* @param DocumentTypeMeta $modelMeta |
66
|
|
|
* @param DocumentPropertyMeta $meta |
67
|
|
|
* @return DecoratorInterface |
68
|
|
|
*/ |
69
|
97 |
|
protected function _getTransformer($transformatorClass, DocumentTypeMeta $modelMeta, DocumentPropertyMeta $meta) |
70
|
|
|
{ |
71
|
97 |
|
return Factory::createForField($transformatorClass, $modelMeta, $meta); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|