Completed
Push — master ( 7137c5...e7caf5 )
by Neomerx
01:51
created

EnumType::getSQLDeclaration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.7333
cc 1
nc 1
nop 2
crap 1
1
<?php namespace Limoncello\Data\Migrations;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Doctrine\DBAL\Platforms\AbstractPlatform;
20
use Doctrine\DBAL\Types\Type;
21
22
/**
23
 * @package Limoncello\Data
24
 */
25
class EnumType extends Type
26
{
27
    /** Type name */
28
    const TYPE_NAME = 'EnumValues';
29
30
    /**
31
     * @inheritdoc
32
     */
33 2
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
34
    {
35 2
        assert(
36 2
            array_key_exists(static::TYPE_NAME, $fieldDeclaration),
37 2
            'Enum values are not set. Use `Column::setCustomSchemaOption` to set them.'
38
        );
39 2
        $values = $fieldDeclaration[static::TYPE_NAME];
40
41
        $quotedValues = array_map(function (string $value) use ($platform, $values) : string {
42 2
            return $platform->quoteStringLiteral($value);
43 2
        }, $values);
44
45 2
        $valueList = implode(',', $quotedValues);
46
47 2
        return "ENUM($valueList)";
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53 1
    public function getName()
54
    {
55 1
        return self::TYPE_NAME;
56
    }
57
}
58