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); |
|
|
|
|
59
|
2 |
|
AspectManager::removeAspect($instance, self::AspectJsonStringToModel); |
60
|
2 |
|
AspectManager::removeAspect($model, self::AspectJsonStringToModel); |
61
|
2 |
|
return $model; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
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.