ArrayType::convertToDatabaseValue()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 3
c 1
b 0
f 1
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 4
nc 2
nop 2
crap 4
1
<?php
2
3
/**
4
 * Licensed to CRATE Technology GmbH("Crate") under one or more contributor
5
 * license agreements.  See the NOTICE file distributed with this work for
6
 * additional information regarding copyright ownership.  Crate licenses
7
 * this file to you under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.  You may
9
 * obtain a copy of the License at
10
 *
11
 * http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
16
 * License for the specific language governing permissions and limitations
17
 * under the License.
18
 *
19
 * However, if you have executed another commercial license agreement
20
 * with Crate these terms will supersede the license and you may use the
21
 * software solely pursuant to the terms of the relevant commercial agreement.
22
 */
23
24
namespace Crate\DBAL\Types;
25
26
use Crate\PDO\PDOCrateDB;
27
use Doctrine\DBAL\Platforms\AbstractPlatform;
28
use Doctrine\DBAL\Types\Type;
29
use Doctrine\DBAL\Types\Types;
30
31
/**
32
 * Type that maps a PHP sequential array to an array SQL type.
33
 *
34
 */
35
class ArrayType extends Type
36
{
37
    public const NAME = 'array';
38
39
    /**
40
     * Gets the name of this type.
41
     *
42
     * @return string
43
     */
44 66
    public function getName()
45
    {
46 66
        return self::NAME;
47
    }
48
49
    /**
50
     * Gets the (preferred) binding type for values of this type that
51
     * can be used when binding parameters to prepared statements.
52
     *
53
     * @return integer
54
     */
55 44
    public function getBindingType()
56
    {
57 44
        return PDOCrateDB::PARAM_ARRAY;
58
    }
59
60 50
    public function convertToDatabaseValue($value, AbstractPlatform $platform): mixed
61
    {
62 50
        if (!is_array($value) || (count($value) > 0 && !(array_keys($value) === range(0, count($value) - 1)))) {
63 4
            return null;
64
        }
65 46
        return $value;
66
    }
67
68 2
    public function convertToPHPValue($value, AbstractPlatform $platform): mixed
69
    {
70 2
        return $value;
71
    }
72
73
    /**
74
     * Gets the SQL declaration snippet for a field of this type.
75
     *
76
     * @param array $fieldDeclaration The field declaration.
77
     * @param AbstractPlatform $platform The currently used database platform.
78
     * @return string
79
     * @throws \Doctrine\DBAL\Exception
80
     */
81 8
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
82
    {
83 8
        $options = !array_key_exists('platformOptions', $fieldDeclaration) ?
84 8
            array() : $fieldDeclaration['platformOptions'];
85 8
        return $this->getArrayTypeDeclarationSQL($platform, $fieldDeclaration, $options);
86
    }
87
88
    /**
89
     * Gets the SQL snippet used to declare an ARRAY column type.
90
     *
91
     * @param AbstractPlatform $platform
92
     * @param array $field
93
     *
94
     * @param array $options
95
     * @return string
96
     * @throws \Doctrine\DBAL\Exception
97
     */
98 8
    public function getArrayTypeDeclarationSQL(AbstractPlatform $platform, array $field, array $options): string
99
    {
100 8
        $type = array_key_exists('type', $options) ? $options['type'] : Types::STRING;
101 8
        return 'ARRAY ( ' . Type::getType($type)->getSQLDeclaration($field, $platform) . ' )';
102
    }
103
}
104