1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory Serializer package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\Serializer\Visitor\Json; |
13
|
|
|
|
14
|
|
|
use Ivory\Serializer\Accessor\AccessorInterface; |
15
|
|
|
use Ivory\Serializer\Context\ContextInterface; |
16
|
|
|
use Ivory\Serializer\Mapping\ClassMetadataInterface; |
17
|
|
|
use Ivory\Serializer\Mapping\TypeMetadataInterface; |
18
|
|
|
use Ivory\Serializer\Visitor\AbstractSerializationVisitor; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author GeLo <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class JsonSerializationVisitor extends AbstractSerializationVisitor |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
private $options; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param AccessorInterface $accessor |
32
|
|
|
* @param int $options |
33
|
|
|
*/ |
34
|
1492 |
|
public function __construct(AccessorInterface $accessor, $options = 0) |
35
|
|
|
{ |
36
|
1492 |
|
parent::__construct($accessor); |
37
|
|
|
|
38
|
1492 |
|
if (defined('JSON_PRESERVE_ZERO_FRACTION')) { |
39
|
1492 |
|
$options |= JSON_PRESERVE_ZERO_FRACTION; |
40
|
|
|
} |
41
|
|
|
|
42
|
1492 |
|
$this->options = $options; |
43
|
1492 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
228 |
View Code Duplication |
public function visitData($data, TypeMetadataInterface $type, ContextInterface $context) |
|
|
|
|
49
|
|
|
{ |
50
|
228 |
|
if ($data === [] && class_exists($type->getName())) { |
51
|
4 |
|
$data = (object) $data; |
52
|
|
|
} |
53
|
|
|
|
54
|
228 |
|
return parent::visitData($data, $type, $context); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
168 |
|
public function finishVisitingObject($data, ClassMetadataInterface $class, ContextInterface $context) |
61
|
|
|
{ |
62
|
168 |
|
if ($this->result === []) { |
63
|
4 |
|
$this->result = (object) $this->result; |
64
|
|
|
} |
65
|
|
|
|
66
|
168 |
|
return parent::finishVisitingObject($data, $class, $context); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
228 |
View Code Duplication |
protected function encode($data) |
|
|
|
|
73
|
|
|
{ |
74
|
228 |
|
$result = @json_encode($data, $this->options); |
75
|
|
|
|
76
|
228 |
|
if (json_last_error() !== JSON_ERROR_NONE) { |
77
|
|
|
throw new \InvalidArgumentException(json_last_error_msg()); |
78
|
|
|
} |
79
|
|
|
|
80
|
228 |
|
return $result; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.