ArrayType.php 1.57 KB
Newer Older
1 2
<?php

3 4
namespace Doctrine\DBAL\Types;

5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6

7
use function is_resource;
8
use function restore_error_handler;
9
use function serialize;
10
use function set_error_handler;
11 12
use function stream_get_contents;
use function unserialize;
13

14
/**
15
 * Type that maps a PHP array to a clob SQL type.
16
 */
17
class ArrayType extends Type
18
{
Benjamin Morel's avatar
Benjamin Morel committed
19 20 21
    /**
     * {@inheritdoc}
     */
22
    public function getSQLDeclaration(array $column, AbstractPlatform $platform)
23
    {
24
        return $platform->getClobTypeDeclarationSQL($column);
25 26
    }

Benjamin Morel's avatar
Benjamin Morel committed
27 28 29 30
    /**
     * {@inheritdoc}
     */
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
31
    {
32
        // @todo 3.0 - $value === null check to save real NULL in database
33 34 35
        return serialize($value);
    }

Benjamin Morel's avatar
Benjamin Morel committed
36 37 38 39
    /**
     * {@inheritdoc}
     */
    public function convertToPHPValue($value, AbstractPlatform $platform)
40
    {
41 42
        if ($value === null) {
            return null;
43 44
        }

45
        $value = is_resource($value) ? stream_get_contents($value) : $value;
Benjamin Morel's avatar
Benjamin Morel committed
46

47
        set_error_handler(function (int $code, string $message): bool {
48 49 50 51 52 53 54 55
            throw ConversionException::conversionFailedUnserialization($this->getName(), $message);
        });

        try {
            return unserialize($value);
        } finally {
            restore_error_handler();
        }
56 57
    }

Benjamin Morel's avatar
Benjamin Morel committed
58 59 60
    /**
     * {@inheritdoc}
     */
61 62
    public function getName()
    {
63
        return Types::ARRAY;
64
    }
65

Benjamin Morel's avatar
Benjamin Morel committed
66 67 68
    /**
     * {@inheritdoc}
     */
69 70 71 72
    public function requiresSQLCommentHint(AbstractPlatform $platform)
    {
        return true;
    }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
73
}