Completed
Push — master ( c7a6bb...41fefb )
by Peter
20:43
created

Transformer   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 98.28%

Importance

Changes 6
Bugs 1 Features 0
Metric Value
wmc 13
c 6
b 1
f 0
lcom 0
cbo 8
dl 0
loc 100
ccs 57
cts 58
cp 0.9828
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B fromModel() 0 25 5
B toModel() 0 54 8
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\Transformers;
15
16
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
17
use Maslosoft\Mangan\Exceptions\TransformatorException;
18
use Maslosoft\Mangan\Helpers\Decorator\Decorator;
19
use Maslosoft\Mangan\Helpers\Decorator\ModelDecorator;
20
use Maslosoft\Mangan\Helpers\Finalizer\FinalizingManager;
21
use Maslosoft\Mangan\Helpers\PropertyFilter\Filter;
22
use Maslosoft\Mangan\Helpers\Sanitizer\Sanitizer;
23
use Maslosoft\Mangan\Meta\DocumentPropertyMeta;
24
use Maslosoft\Mangan\Meta\ManganMeta;
25
26
/**
27
 * Transformer
28
 *
29
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
30
 */
31
abstract class Transformer
32
{
33
34
	/**
35
	 * Returns the given object as an associative array
36
	 * @param AnnotatedInterface|object $model
37
	 * @param string[] $fields Fields to transform
38
	 * @return array an associative array of the contents of this object
39
	 */
40 101
	public static function fromModel(AnnotatedInterface $model, $fields = [])
41
	{
42 101
		$meta = ManganMeta::create($model);
43 101
		$calledClass = get_called_class();
44 101
		$decorator = new Decorator($model, $calledClass);
45 101
		$md = new ModelDecorator($model, $calledClass);
46 101
		$sanitizer = new Sanitizer($model, $calledClass);
47 101
		$filter = new Filter($model, $calledClass);
48 101
		$arr = [];
49 101
		foreach ($meta->fields() as $name => $fieldMeta)
50
		{
51 101
			if (!empty($fields) && !in_array($name, $fields))
52 101
			{
53 23
				continue;
54
			}
55 101
			if (!$filter->fromModel($model, $fieldMeta))
56 101
			{
57 32
				continue;
58
			}
59 101
			$model->$name = $sanitizer->write($name, $model->$name);
60 101
			$decorator->write($name, $arr);
61 101
		}
62 101
		$md->write($arr);
63 101
		return FinalizingManager::fromModel($arr, static::class, $model);
64
	}
65
66
	/**
67
	 * Create document from array
68
	 *
69
	 * @param mixed[] $data
70
	 * @param string|object $className
71
	 * @param AnnotatedInterface $instance
72
	 * @return AnnotatedInterface
73
	 * @throws TransformatorException
74
	 */
75 75
	public static function toModel($data, $className = null, AnnotatedInterface $instance = null)
76
	{
77 75
		$data = (array) $data;
78 75
		if (is_object($className))
79 75
		{
80 59
			$className = get_class($className);
81 59
		}
82 75
		if (!$className)
83 75
		{
84 35
			if (array_key_exists('_class', $data))
85 35
			{
86 35
				$className = $data['_class'];
87 35
			}
88
			else
89
			{
90
				throw new TransformatorException('Could not determine document type');
91
			}
92 35
		}
93
		if ($instance)
94 75
		{
95 3
			$model = $instance;
96 3
		}
97
		else
98
		{
99 72
			$model = new $className;
100
		}
101 75
		$meta = ManganMeta::create($model);
102 75
		$calledClass = get_called_class();
103 75
		$decorator = new Decorator($model, $calledClass);
104 75
		$md = new ModelDecorator($model, $calledClass);
105 75
		$sanitizer = new Sanitizer($model, $calledClass);
106 75
		$filter = new Filter($model, $calledClass);
107 75
		foreach ($meta->fields() as $name => $fieldMeta)
108
		{
109
			/* @var $fieldMeta DocumentPropertyMeta */
110 75
			if (isset($data[$name]))
111 75
			{
112 75
				$value = $data[$name];
113 75
			}
114
			else
115
			{
116 35
				$value = $fieldMeta->default;
117
			}
118 75
			if (!$filter->toModel($model, $fieldMeta))
119 75
			{
120 17
				continue;
121
			}
122 75
			$decorator->read($name, $value);
123 75
			$model->$name = $sanitizer->read($name, $model->$name);
124 75
		}
125 75
		$md->read($data);
126
127 75
		return FinalizingManager::toModel(static::class, $model);
128
	}
129
130
}
131