Completed
Push — master ( 268ab9...0f3d43 )
by Kévin
13s
created

src/Type/JsonDocumentType.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * (c) Kévin Dunglas <[email protected]>
5
 *
6
 * This source file is subject to the MIT license that is bundled
7
 * with this source code in the file LICENSE.
8
 */
9
10
namespace Dunglas\DoctrineJsonOdm\Type;
11
12
use Doctrine\DBAL\Platforms\AbstractPlatform;
13
use Doctrine\DBAL\Types\JsonArrayType;
14
use Symfony\Component\Serializer\SerializerInterface;
15
16
/**
17
 * The JSON document type.
18
 *
19
 * @author Kévin Dunglas <[email protected]>
20
 */
21
final class JsonDocumentType extends JsonArrayType
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\DBAL\Types\JsonArrayType has been deprecated with message: Use JsonType instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
22
{
23
    /**
24
     * @var SerializerInterface
25
     */
26
    private $serializer;
27
28
    /**
29
     * @var string
30
     */
31
    private $format = 'json';
32
33
    /**
34
     * @var array
35
     */
36
    private $serializationContext = [];
37
38
    /**
39
     * @var array
40
     */
41
    private $deserializationContext = [];
42
43
    /**
44
     * Sets the serializer to use.
45
     *
46
     * @param SerializerInterface $serializer
47
     */
48
    public function setSerializer(SerializerInterface $serializer)
49
    {
50
        $this->serializer = $serializer;
51
    }
52
53
    /**
54
     * Gets the serializer or throw an exception if it isn't available.
55
     *
56
     * @throws \RuntimeException
57
     *
58
     * @return SerializerInterface
59
     */
60
    private function getSerializer()
61
    {
62
        if (null === $this->serializer) {
63
            throw new \RuntimeException(sprintf('An instance of "%s" must be available. Call the "setSerializer" method.', SerializerInterface::class));
64
        }
65
66
        return $this->serializer;
67
    }
68
69
    /**
70
     * Sets the serialization format (default to "json").
71
     *
72
     * @param string $format
73
     */
74
    public function setFormat($format)
75
    {
76
        $this->format = $format;
77
    }
78
79
    /**
80
     * Sets the serialization context (default to an empty array).
81
     *
82
     * @param array $serializationContext
83
     */
84
    public function setSerializationContext(array $serializationContext)
85
    {
86
        $this->serializationContext = $serializationContext;
87
    }
88
89
    /**
90
     * Sets the deserialization context (default to an empty array).
91
     *
92
     * @param array $deserializationContext
93
     */
94
    public function setDeserializationContext(array $deserializationContext)
95
    {
96
        $this->deserializationContext = $deserializationContext;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
103
    {
104
        return $this->getSerializer()->serialize($value, $this->format, $this->serializationContext);
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function convertToPHPValue($value, AbstractPlatform $platform)
111
    {
112
        if (null === $value || $value === '') {
113
            return;
114
        }
115
116
        return $this->getSerializer()->deserialize($value, '', $this->format, $this->deserializationContext);
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function getName()
123
    {
124
        return 'json_document';
125
    }
126
}
127