Completed
Pull Request — develop (#3453)
by Evgeniy
63:46
created

ObjectType::getBindingType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Doctrine\DBAL\Types;
4
5
use Doctrine\DBAL\ParameterType;
6
use Doctrine\DBAL\Platforms\AbstractPlatform;
7
use function is_resource;
8
use function restore_error_handler;
9
use function serialize;
10
use function set_error_handler;
11
use function stream_get_contents;
12
use function unserialize;
13
14
/**
15
 * Type that maps a PHP object to a clob SQL type.
16
 */
17
class ObjectType extends Type
18
{
19
    /**
20
     * {@inheritdoc}
21 462
     */
22
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
23 462
    {
24
        return $platform->getBlobTypeDeclarationSQL($fieldDeclaration);
25
    }
26
27
    /**
28
     * {@inheritdoc}
29 46
     */
30
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
31 46
    {
32
        return serialize($value);
33
    }
34
35
    /**
36
     * {@inheritdoc}
37 115
     */
38
    public function convertToPHPValue($value, AbstractPlatform $platform)
39 115
    {
40 23
        if ($value === null) {
41
            return null;
42
        }
43 92
44
        $value = is_resource($value) ? stream_get_contents($value) : $value;
45
46 23
        set_error_handler(function (int $code, string $message) : void {
47 92
            throw ConversionException::conversionFailedUnserialization($this->getName(), $message);
48
        });
49
50 92
        try {
51
            return unserialize($value);
52 92
        } finally {
53
            restore_error_handler();
54
        }
55
    }
56
57
    /**
58
     * {@inheritdoc}
59 761
     */
60
    public function getBindingType() : int
61 761
    {
62
        return ParameterType::BINARY;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67 14094
     */
68
    public function getName()
69 14094
    {
70
        return Type::OBJECT;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function requiresSQLCommentHint(AbstractPlatform $platform)
77
    {
78
        return true;
79
    }
80
}
81