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\Transformers; |
15
|
|
|
|
16
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
17
|
|
|
use Maslosoft\Mangan\AspectManager; |
18
|
|
|
use Maslosoft\Mangan\EntityManager; |
19
|
|
|
use Maslosoft\Mangan\Interfaces\Transformators\TransformatorInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* This transformer is configured to set only safe attributes - from any external source. |
23
|
|
|
* @see EntityManager::setAttributes() |
24
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
25
|
|
|
*/ |
26
|
|
|
class SafeArray extends Transformer implements TransformatorInterface |
27
|
|
|
{ |
28
|
|
|
const AspectSafeArrayFromModel = 'AspectSafeArrayFromModel'; |
29
|
|
|
const AspectSafeArrayToModel = 'AspectSafeArrayToModel'; |
30
|
|
|
|
31
|
|
|
public static function fromModel(AnnotatedInterface $model, $fields = []) |
32
|
|
|
{ |
33
|
|
|
AspectManager::addAspect($model, self::AspectSafeArrayFromModel); |
34
|
|
|
$data = parent::fromModel($model, $fields); |
35
|
|
|
AspectManager::removeAspect($model, self::AspectSafeArrayFromModel); |
36
|
|
|
return $data; |
37
|
|
|
} |
38
|
|
|
|
39
|
6 |
|
public static function toModel($data, $className = null, AnnotatedInterface $instance = null, AnnotatedInterface $parent = null, $parentField = '') |
40
|
|
|
{ |
41
|
6 |
|
AspectManager::addAspect($instance, self::AspectSafeArrayToModel); |
42
|
6 |
|
$model = parent::toModel($data, $className, $instance, $parent, $parentField); |
43
|
6 |
|
AspectManager::removeAspect($instance, self::AspectSafeArrayToModel); |
44
|
6 |
|
AspectManager::removeAspect($model, self::AspectSafeArrayToModel); |
45
|
6 |
|
return $model; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|