|
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\Interfaces\Transformators\TransformatorInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* JsonString |
|
22
|
|
|
* |
|
23
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
24
|
|
|
*/ |
|
25
|
|
|
class JsonString implements TransformatorInterface |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Returns the given object as an associative array |
|
30
|
|
|
* @param AnnotatedInterface|object $model |
|
31
|
|
|
* @param string[] $fields Fields to transform |
|
32
|
|
|
* @param int $options json_encode options |
|
33
|
|
|
* @return array an associative array of the contents of this object |
|
34
|
|
|
*/ |
|
35
|
2 |
|
public static function fromModel(AnnotatedInterface $model, $fields = [], $options = null) |
|
36
|
|
|
{ |
|
37
|
2 |
|
return json_encode(JsonArray::fromModel($model, $fields), $options); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Create document from array |
|
42
|
|
|
* |
|
43
|
|
|
* @param mixed[] $data |
|
44
|
|
|
* @param string|object $className |
|
45
|
|
|
* @param AnnotatedInterface $instance |
|
46
|
|
|
* @return AnnotatedInterface |
|
47
|
|
|
* @throws TransformatorException |
|
48
|
|
|
*/ |
|
49
|
2 |
|
public static function toModel($data, $className = null, AnnotatedInterface $instance = null) |
|
50
|
|
|
{ |
|
51
|
2 |
|
return JsonArray::toModel(json_decode($data, true), $className, $instance); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.