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

src/Type/JsonDocumentType.php (4 issues)

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 Doctrine\DBAL\Types\JsonType;
15
use Symfony\Component\Serializer\SerializerInterface;
16
17
if (class_exists(JsonType::class)) {
18
    /**
19
     * @internal
20
     */
21
    class InternalParentClass extends JsonType
22
    {
23
    }
24
} else {
25
    /**
26
     * @internal
27
     */
28
    class InternalParentClass extends JsonArrayType
0 ignored issues
show
Comprehensibility Best Practice introduced by
The type Dunglas\DoctrineJsonOdm\Type\InternalParentClass has been defined more than once; this definition is ignored, only the first definition in this file (L21-23) is considered.

This check looks for classes that have been defined more than once in the same file.

If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.

This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.

Loading history...
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...
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
29
    {
30
    }
31
}
32
33
/**
34
 * The JSON document type.
35
 *
36
 * @author Kévin Dunglas <[email protected]>
37
 */
38
final class JsonDocumentType extends InternalParentClass
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

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