JsonUnescapedType::convertToDatabaseValue()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 5
cts 7
cp 0.7143
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3.2098
1
<?php
2
declare(strict_types=1);
3
4
namespace Tanigami\DoctrineJsonUnescapedType;
5
6
use Doctrine\DBAL\Platforms\AbstractPlatform;
7
use Doctrine\DBAL\Types\ConversionException;
8
use Doctrine\DBAL\Types\JsonType;
9
10
class JsonUnescapedType extends JsonType
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15 1
    public function convertToDatabaseValue($value, AbstractPlatform $platform = null)
16
    {
17 1
        if (null === $value) {
18
            return null;
19
        }
20
21 1
        $encoded = json_encode($value, JSON_UNESCAPED_UNICODE);
22
23 1
        if (JSON_ERROR_NONE !== json_last_error()) {
24
            throw ConversionException::conversionFailedSerialization($value, 'json', json_last_error_msg());
25
        }
26
27 1
        return $encoded;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function getName()
34
    {
35
        return 'json_unescaped';
36
    }
37
}
38