1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
5
|
|
|
* Date: 7/18/15 |
6
|
|
|
* Time: 2:26 PM. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace NilPortugues\Api\Json; |
12
|
|
|
|
13
|
|
|
use NilPortugues\Api\Mapping\Mapper; |
14
|
|
|
use NilPortugues\Api\Transformer\Helpers\RecursiveDeleteHelper; |
15
|
|
|
use NilPortugues\Api\Transformer\Helpers\RecursiveRenamerHelper; |
16
|
|
|
use NilPortugues\Api\Transformer\Transformer; |
17
|
|
|
use NilPortugues\Serializer\Serializer; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class JsonTransformer. |
21
|
|
|
*/ |
22
|
|
|
class JsonTransformer extends Transformer |
23
|
|
|
{ |
24
|
|
|
const META_KEY = 'meta'; |
25
|
|
|
const LINKS = 'links'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param Mapper $mapper |
29
|
|
|
*/ |
30
|
|
|
public function __construct(Mapper $mapper = null) |
31
|
|
|
{ |
32
|
|
|
if (null !== $mapper) { |
33
|
|
|
$this->mappings = $mapper->getClassMap(); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param mixed $value |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function serialize($value) |
43
|
|
|
{ |
44
|
|
|
return json_encode($this->serialization($value), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $value |
49
|
|
|
* |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
protected function serialization($value) |
53
|
|
|
{ |
54
|
|
|
if (null !== $this->mappings) { |
55
|
|
|
/** @var \NilPortugues\Api\Mapping\Mapping $mapping */ |
56
|
|
|
foreach ($this->mappings as $class => $mapping) { |
57
|
|
|
RecursiveDeleteHelper::deleteProperties($this->mappings, $value, $class); |
58
|
|
|
RecursiveRenamerHelper::renameKeyValue($this->mappings, $value, $class); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->setResponseMeta($value); |
63
|
|
|
$this->setResponseLinks($value); |
64
|
|
|
self::formatScalarValues($value); |
65
|
|
|
RecursiveDeleteHelper::deleteKeys($value, [Serializer::CLASS_IDENTIFIER_KEY]); |
66
|
|
|
self::flattenObjectsWithSingleKeyScalars($value); |
67
|
|
|
$this->recursiveSetKeysToUnderScore($value); |
68
|
|
|
|
69
|
|
|
return $value; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array $response |
74
|
|
|
*/ |
75
|
|
|
private function setResponseMeta(array &$response) |
76
|
|
|
{ |
77
|
|
|
if (!empty($this->meta)) { |
78
|
|
|
$response[self::META_KEY] = $this->meta; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param array $response |
84
|
|
|
*/ |
85
|
|
|
private function setResponseLinks(array &$response) |
86
|
|
|
{ |
87
|
|
|
$links = array_filter( |
88
|
|
|
array_merge( |
89
|
|
|
$this->buildLinks(), |
90
|
|
|
$this->getResponseAdditionalLinks($response, $response[Serializer::CLASS_IDENTIFIER_KEY]) |
91
|
|
|
) |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
if (!empty($links)) { |
95
|
|
|
$response[self::LINKS] = $this->addHrefToLinks($links); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|