Completed
Push — master ( c1ad5e...1443d8 )
by Peter
08:22
created

SafeArray::toModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 5
crap 1
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