|
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
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* YamlString |
|
24
|
|
|
* |
|
25
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
26
|
|
|
*/ |
|
27
|
|
|
class YamlString implements TransformatorInterface |
|
28
|
|
|
{ |
|
29
|
|
|
const AspectYamlStringFromModel = 'AspectYamlStringFromModel'; |
|
30
|
|
|
const AspectYamlStringToModel = 'AspectYamlStringToModel'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Returns the given object as an associative array |
|
34
|
|
|
* @param AnnotatedInterface|object $model |
|
35
|
|
|
* @param string[] $fields Fields to transform |
|
36
|
|
|
* |
|
37
|
|
|
* @param int $inline The level where you switch to inline YAML |
|
38
|
|
|
* @param int $indent The amount of spaces to use for indentation of nested nodes. |
|
39
|
|
|
* @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise |
|
40
|
|
|
* @return string YAML string with the contents of this object |
|
41
|
|
|
*/ |
|
42
|
2 |
|
public static function fromModel(AnnotatedInterface $model, $fields = [], $inline = 2, $indent = 4, $exceptionOnInvalidType = false) |
|
43
|
|
|
{ |
|
44
|
2 |
|
AspectManager::addAspect($model, self::AspectYamlStringFromModel); |
|
45
|
2 |
|
$data = Yaml::dump(YamlArray::fromModel($model, $fields), $inline, $indent, $exceptionOnInvalidType); |
|
|
|
|
|
|
46
|
2 |
|
AspectManager::removeAspect($model, self::AspectYamlStringFromModel); |
|
47
|
2 |
|
return $data; |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Create document from array |
|
52
|
|
|
* |
|
53
|
|
|
* @param mixed[] $data |
|
54
|
|
|
* @param string|object $className |
|
55
|
|
|
* @param AnnotatedInterface $instance |
|
56
|
|
|
* |
|
57
|
|
|
* @param bool $exceptionOnInvalidType True if an exception must be thrown on invalid types false otherwise |
|
58
|
|
|
* |
|
59
|
|
|
* @return AnnotatedInterface |
|
60
|
|
|
* @throws TransformatorException |
|
61
|
|
|
*/ |
|
62
|
2 |
|
public static function toModel($data, $className = null, AnnotatedInterface $instance = null, $exceptionOnInvalidType = false) |
|
63
|
|
|
{ |
|
64
|
2 |
|
AspectManager::addAspect($instance, self::AspectYamlStringToModel); |
|
65
|
2 |
|
$model = YamlArray::toModel(Yaml::parse($data, $exceptionOnInvalidType), $className, $instance); |
|
|
|
|
|
|
66
|
2 |
|
AspectManager::removeAspect($instance, self::AspectYamlStringToModel); |
|
67
|
2 |
|
AspectManager::removeAspect($model, self::AspectYamlStringToModel); |
|
68
|
2 |
|
return $model; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: