Completed
Push — master ( 17956e...f71939 )
by Peter
22:54
created

Datamatrix::fromModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4286
nc 1
cc 1
eloc 3
nop 2
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Maslosoft\Mangan\Transformers;
10
11
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
12
use Maslosoft\Mangan\Exceptions\TransformatorException;
13
use Maslosoft\Mangan\Interfaces\Transformators\TransformatorInterface;
14
15
/**
16
 * Datamatrix
17
 *
18
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
19
 */
20
class Datamatrix implements TransformatorInterface
21
{
22
23
	/**
24
	 * Returns the given object as an associative array
25
	 * @param AnnotatedInterface|object $model
26
	 * @param string[] $fields Fields to transform
27
	 * @return array an associative array of the contents of this object
28
	 */
29
	public static function fromModel(AnnotatedInterface $model, $fields = [])
30
	{
31
		$data = YamlString::fromModel($model, $fields, 1, 1);
32
		return (new \Dmtx\Writer())->encode($data)->dump();
33
	}
34
35
	/**
36
	 * Create document from array
37
	 *
38
	 * @param mixed[] $data
39
	 * @param string|object $className
40
	 * @param AnnotatedInterface $instance
41
	 * @return AnnotatedInterface
42
	 * @throws TransformatorException
43
	 */
44
	public static function toModel($data, $className = null, AnnotatedInterface $instance = null)
45
	{
46
		$data = (new \Dmtx\Reader())->decode($data);
47
		return YamlString::toModel($data, $className, $instance);
48
	}
49
50
}
51