Test Failed
Pull Request — master (#13)
by Alexander
28:59
created

AbstractODMType::setEntityClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/*
3
 * This file is part of Goodwix Doctrine JSON ODM.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Goodwix\DoctrineJsonOdm\Type;
10
11
use Doctrine\DBAL\Platforms\AbstractPlatform;
12
use Doctrine\DBAL\Types\JsonType;
13
use Goodwix\DoctrineJsonOdm\Exception\JsonOdmException;
14
use Symfony\Component\Serializer\Exception\ExceptionInterface;
15
use Symfony\Component\Serializer\SerializerInterface;
16
17
abstract class AbstractODMType extends JsonType
18
{
19
    /** @var array */
20
    public $deserializationContext = [];
21
    /** @var SerializerInterface */
22
    private $serializer;
23
24
    /** @var string */
25
    private $format = 'json';
26
27
    /** @var string */
28
    private $entityClass;
29
30
    /** @var array */
31
    private $serializationContext = [];
32
33
    public function getSerializer(): SerializerInterface
34
    {
35
        if (null === $this->serializer) {
36
            throw new \RuntimeException(
37
                sprintf(
38
                    'An instance of "%s" must be available. Call the "setSerializer" method.',
39
                    SerializerInterface::class
40
                ))
41
            ;
42
        }
43
44
        return $this->serializer;
45
    }
46
47
    public function setSerializer(SerializerInterface $serializer): void
48
    {
49
        $this->serializer = $serializer;
50
    }
51
52
    public function getFormat(): string
53
    {
54
        return $this->format;
55
    }
56
57
    public function setFormat(string $format): void
58
    {
59
        $this->format = $format;
60
    }
61
62
    public function getEntityClass(): string
63
    {
64
        if (null === $this->entityClass) {
65
            throw new \RuntimeException(
66
                'ODM entity class name must be available. Call the "setEntityClass" method.'
67
            );
68
        }
69
70
        return $this->entityClass;
71
    }
72
73
    public function setEntityClass(string $entityClass): void
74
    {
75
        $this->entityClass = $entityClass;
76
    }
77
78
    public function getSerializationContext(): array
79
    {
80
        return $this->serializationContext;
81
    }
82
83
    public function setSerializationContext(array $serializationContext): void
84
    {
85
        $this->serializationContext = $serializationContext;
86
    }
87
88
    public function getDeserializationContext(): array
89
    {
90
        return $this->deserializationContext;
91
    }
92
93
    public function setDeserializationContext(array $deserializationContext): void
94
    {
95
        $this->deserializationContext = $deserializationContext;
96
    }
97
98
    public function getName(): string
99
    {
100
        return $this->getEntityClass();
101
    }
102
103
    /**
104
     * @throws JsonOdmException
105
     */
106
    public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
107
    {
108
        if (null === $value) {
109
            return null;
110
        }
111
112
        try {
113
            $context = $this->getSerializationContext();
114
            $value   = $this->getSerializer()->serialize($value, $this->format, $context);
115
        } catch (ExceptionInterface $exception) {
116
            $message = sprintf('Serialization exception occurred for class "%s".', $this->getEntityClass());
117
118
            throw new JsonOdmException($message, 0, $exception);
119
        }
120
121
        return $value;
122
    }
123
124
    /**
125
     * @throws JsonOdmException
126
     */
127
    public function convertToPHPValue($value, AbstractPlatform $platform)
128
    {
129
        if (null === $value || '' === $value) {
130
            return null;
131
        }
132
133
        try {
134
            $context = $this->getDeserializationContext();
135
            $value   = $this->getSerializer()->deserialize($value, $this->getEntityClass(), $this->format, $context);
136
        } catch (ExceptionInterface $exception) {
137
            $message = sprintf('Deserialization exception occurred for class "%s".', $this->getEntityClass());
138
139
            throw new JsonOdmException($message, 0, $exception);
140
        }
141
142
        return $value;
143
    }
144
145
    abstract public static function registerODMType(string $entityClass, SerializerInterface $serializer): void;
146
    
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function requiresSQLCommentHint(AbstractPlatform $platform)
151
    {
152
        return true;
153
    }
154
}
155