JsonString   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 2
dl 0
loc 39
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fromModel() 0 7 1
A toModel() 0 8 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\Exceptions\TransformatorException;
19
use Maslosoft\Mangan\Interfaces\Transformators\TransformatorInterface;
20
21
/**
22
 * JsonString
23
 *
24
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
25
 */
26
class JsonString implements TransformatorInterface
27
{
28
	const AspectJsonStringFromModel = 'AspectJsonStringFromModel';
29
	const AspectJsonStringToModel = 'AspectJsonStringToModel';
30
31
	/**
32
	 * Returns the given object as an associative array
33
	 * @param AnnotatedInterface|object $model
34
	 * @param string[] $fields Fields to transform
35
	 * @param int $options json_encode options
36
	 * @return array an associative array of the contents of this object
37
	 */
38 2
	public static function fromModel(AnnotatedInterface $model, $fields = [], $options = null)
39
	{
40 2
		AspectManager::addAspect($model, self::AspectJsonStringFromModel);
41 2
		$data = json_encode(JsonArray::fromModel($model, $fields), $options);
42 2
		AspectManager::removeAspect($model, self::AspectJsonStringFromModel);
43 2
		return $data;
44
	}
45
46
	/**
47
	 * Create document from array
48
	 *
49
	 * @param mixed[] $data
50
	 * @param string|object $className
51
	 * @param AnnotatedInterface $instance
52
	 * @return AnnotatedInterface
53
	 * @throws TransformatorException
54
	 */
55 2
	public static function toModel($data, $className = null, AnnotatedInterface $instance = null)
56
	{
57 2
		AspectManager::addAspect($instance, self::AspectJsonStringFromModel);
58 2
		$model = JsonArray::toModel(json_decode($data, true), $className, $instance);
0 ignored issues
show
Bug introduced by
It seems like $className defined by parameter $className on line 55 can also be of type object; however, Maslosoft\Mangan\Transformers\JsonArray::toModel() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
59 2
		AspectManager::removeAspect($instance, self::AspectJsonStringToModel);
60 2
		AspectManager::removeAspect($model, self::AspectJsonStringToModel);
61 2
		return $model;
62
	}
63
64
}
65