SimpleArrayType.php 1.39 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Types;

5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6

7
use function count;
8 9
use function explode;
use function implode;
10
use function is_array;
11 12
use function is_resource;
use function stream_get_contents;
13

14 15 16 17 18 19 20
/**
 * Array Type which can be used for simple values.
 *
 * Only use this type if you are sure that your values cannot contain a ",".
 */
class SimpleArrayType extends Type
{
Benjamin Morel's avatar
Benjamin Morel committed
21 22 23
    /**
     * {@inheritdoc}
     */
24
    public function getSQLDeclaration(array $column, AbstractPlatform $platform)
25
    {
26
        return $platform->getClobTypeDeclarationSQL($column);
27 28
    }

Benjamin Morel's avatar
Benjamin Morel committed
29 30 31
    /**
     * {@inheritdoc}
     */
Benjamin Eberlei's avatar
Benjamin Eberlei committed
32
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
33
    {
34
        if (! is_array($value) || count($value) === 0) {
35 36 37
            return null;
        }

38 39 40
        return implode(',', $value);
    }

Benjamin Morel's avatar
Benjamin Morel committed
41 42 43
    /**
     * {@inheritdoc}
     */
Benjamin Eberlei's avatar
Benjamin Eberlei committed
44
    public function convertToPHPValue($value, AbstractPlatform $platform)
45 46
    {
        if ($value === null) {
47
            return [];
48 49
        }

50
        $value = is_resource($value) ? stream_get_contents($value) : $value;
51 52

        return explode(',', $value);
53 54
    }

Benjamin Morel's avatar
Benjamin Morel committed
55 56 57
    /**
     * {@inheritdoc}
     */
58 59
    public function getName()
    {
60
        return Types::SIMPLE_ARRAY;
61 62
    }

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