Skip to content

Commit 6c06960

Browse files
committed
JsonSerializer class introducced
1 parent eac3f8c commit 6c06960

File tree

4 files changed

+142
-23
lines changed

4 files changed

+142
-23
lines changed

README.md

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,10 @@ $mapper = new Mapper($mappings);
148148
Calling the transformer will output a **valid JSON response** using the correct formatting:
149149

150150
```php
151-
use NilPortugues\Api\Json\JsonTransformer;
151+
use NilPortugues\Api\Json\JsonSerializer;
152152
use NilPortugues\Api\Json\Http\Message\Response;
153-
use NilPortugues\Serializer\DeepCopySerializer;
154-
155-
$transformer = new JsonTransformer($mapper);
156-
157-
//Output transformation
158-
$serializer = new DeepCopySerializer($transformer);
159-
$serializer->setSelfUrl('http://example.com/posts/9');
160-
$serializer->setNextUrl('http://example.com/posts/10');
161-
$serializer->addMeta('author',[['name' => 'Nil Portugués Calderó', 'email' => 'contact@nilportugues.com']]);
162153

154+
$serializer = new JsonSerializer($mapper);
163155
$output = $serializer->serialize($post);
164156

165157
//PSR7 Response with headers and content.
@@ -213,15 +205,12 @@ Content-type: application/json; charset=utf-8
213205
}
214206
],
215207
"links": {
216-
"self": {
217-
"href": "http://localhost:8000/post/9"
218-
},
219-
"next": {
220-
"href": "http://localhost:8000/post/10"
221-
},
222208
"comments": {
223209
"href": "http://localhost:8000/post/9/comments"
224-
}
210+
},
211+
"self": {
212+
"href": "http://localhost:8000/post/9"
213+
}
225214
}
226215
}
227216
```

src/JsonSerializer.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace NilPortugues\Api\Json;
4+
5+
use NilPortugues\Api\Mapping\Mapper;
6+
use NilPortugues\Serializer\DeepCopySerializer;
7+
8+
class JsonSerializer extends DeepCopySerializer
9+
{
10+
/**
11+
* JsonSerializer constructor.
12+
*
13+
* @param Mapper $mapper
14+
*/
15+
public function __construct(Mapper $mapper)
16+
{
17+
parent::__construct(new JsonTransformer($mapper));
18+
}
19+
20+
/**
21+
* @return JsonTransformer
22+
*/
23+
public function getTransformer()
24+
{
25+
return $this->serializationStrategy;
26+
}
27+
/**
28+
* @param mixed $value
29+
*
30+
* @return string
31+
*/
32+
public function serialize($value)
33+
{
34+
return parent::serialize($value);
35+
}
36+
}

src/JsonTransformer.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@ public function serialize($value)
4141
* @return mixed
4242
*/
4343
protected function serialization($value)
44+
{
45+
if (is_array($value) && !empty($value[Serializer::MAP_TYPE])) {
46+
$data = [];
47+
foreach ($value[Serializer::SCALAR_VALUE] as $v) {
48+
$data[] = $this->serialization($v);
49+
}
50+
} else {
51+
$data = $this->serializeObject($value);
52+
}
53+
54+
return $data;
55+
}
56+
57+
/**
58+
* @param array $value
59+
*
60+
* @return mixed
61+
*/
62+
protected function serializeObject(array $value)
4463
{
4564
if (null !== $this->mappings) {
4665
/** @var \NilPortugues\Api\Mapping\Mapping $mapping */

tests/JsonTransformerTest.php renamed to tests/JsonSerializerTest.php

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212
namespace NilPortugues\Tests\Json;
1313

1414
use DateTime;
15+
use NilPortugues\Api\Json\JsonSerializer;
1516
use NilPortugues\Api\Json\JsonTransformer;
1617
use NilPortugues\Api\Mapping\Mapper;
17-
use NilPortugues\Serializer\Serializer;
1818
use NilPortugues\Tests\Api\Json\Dummy\ComplexObject\Comment;
1919
use NilPortugues\Tests\Api\Json\Dummy\ComplexObject\Post;
2020
use NilPortugues\Tests\Api\Json\Dummy\ComplexObject\User;
2121
use NilPortugues\Tests\Api\Json\Dummy\ComplexObject\ValueObject\CommentId;
2222
use NilPortugues\Tests\Api\Json\Dummy\ComplexObject\ValueObject\PostId;
2323
use NilPortugues\Tests\Api\Json\Dummy\ComplexObject\ValueObject\UserId;
2424

25-
class JsonTransformerTest extends \PHPUnit_Framework_TestCase
25+
class JsonSerializerTest extends \PHPUnit_Framework_TestCase
2626
{
2727
/**
2828
* @return Post
@@ -105,10 +105,85 @@ public function testItWillRenamePropertiesAndHideFromClass()
105105

106106
$this->assertEquals(
107107
json_decode($expected, true),
108-
json_decode(
109-
(new Serializer(new JsonTransformer(new Mapper($mappings))))->serialize($this->getPostObject()),
110-
true
111-
)
108+
json_decode((new JsonSerializer(new Mapper($mappings)))->serialize($this->getPostObject()), true)
112109
);
113110
}
111+
112+
public function testItCanSerializeArrays()
113+
{
114+
$mappings = [
115+
[
116+
'class' => Post::class,
117+
'alias' => 'Message',
118+
'aliased_properties' => [
119+
'title' => 'headline',
120+
'content' => 'body',
121+
],
122+
'hide_properties' => [
123+
'comments',
124+
],
125+
'id_properties' => [
126+
'postId',
127+
],
128+
'urls' => [
129+
// Mandatory
130+
'self' => 'http://example.com/posts/{postId}',
131+
// Optional
132+
'comments' => 'http://example.com/posts/{postId}/comments',
133+
],
134+
],
135+
];
136+
137+
$expected = <<<JSON
138+
[
139+
{
140+
"post_id": 9,
141+
"headline": "Hello World",
142+
"body": "Your first post",
143+
"author": {
144+
"user_id": 1,
145+
"name": "Post Author"
146+
},
147+
"links": {
148+
"self": {
149+
"href": "http://example.com/posts/9"
150+
},
151+
"comments": {
152+
"href": "http://example.com/posts/9/comments"
153+
}
154+
}
155+
},
156+
{
157+
"post_id": 9,
158+
"headline": "Hello World",
159+
"body": "Your first post",
160+
"author": {
161+
"user_id": 1,
162+
"name": "Post Author"
163+
},
164+
"links": {
165+
"self": {
166+
"href": "http://example.com/posts/9"
167+
},
168+
"comments": {
169+
"href": "http://example.com/posts/9/comments"
170+
}
171+
}
172+
}
173+
]
174+
JSON;
175+
$serializer = (new JsonSerializer(new Mapper($mappings)));
176+
177+
$this->assertEquals(
178+
json_decode($expected, true),
179+
json_decode($serializer->serialize([$this->getPostObject(), $this->getPostObject()]), true)
180+
);
181+
}
182+
183+
public function testGetTransformer()
184+
{
185+
$serializer = (new JsonSerializer(new Mapper([])));
186+
187+
$this->assertInstanceOf(JsonTransformer::class, $serializer->getTransformer());
188+
}
114189
}

0 commit comments

Comments
 (0)