MapType   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 96%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 15
eloc 24
c 1
b 0
f 1
dl 0
loc 82
ccs 24
cts 25
cp 0.96
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A convertToDatabaseValue() 0 7 4
A getBindingType() 0 3 1
A getSQLDeclaration() 0 6 2
A getMapTypeDeclarationSQL() 0 13 5
A convertToPHPValue() 0 6 2
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\DBAL\Platforms\CratePlatform;
27
use Crate\PDO\PDOCrateDB;
28
use Doctrine\DBAL\Platforms\AbstractPlatform;
29
use Doctrine\DBAL\Types\Type;
30
31
/**
32
 * Type that maps a PHP associative array (map) to an object SQL type.
33
 *
34
 * TODO: Add support for strict|dynamic|ignored object types
35
 *
36
 */
37
class MapType extends Type
38
{
39
    public const NAME = 'map';
40
    public const STRICT = 'strict';
41
    public const DYNAMIC = 'dynamic';
42
    public const IGNORED = 'ignored';
43
44
    /**
45
     * Gets the name of this type.
46
     *
47
     * @return string
48
     */
49 66
    public function getName()
50
    {
51 66
        return self::NAME;
52
    }
53
54
    /**
55
     * Gets the (preferred) binding type for values of this type that
56
     * can be used when binding parameters to prepared statements.
57
     *
58
     * @return integer
59
     */
60 48
    public function getBindingType()
61
    {
62 48
        return PDOCrateDB::PARAM_OBJECT;
63
    }
64
65 54
    public function convertToDatabaseValue($value, AbstractPlatform $platform): mixed
66
    {
67 54
        if (!is_array($value) || (count($value) > 0 && array_keys($value) === range(0, count($value) - 1))) {
68 4
            return null;
69
        }
70
71 50
        return $value;
72
    }
73
74 2
    public function convertToPHPValue($value, AbstractPlatform $platform): mixed
75
    {
76 2
        if ($value === null) {
77
            return null;
78
        }
79 2
        return (array) $value;
80
    }
81
82
    /**
83
     * Gets the SQL declaration snippet for a field of this type.
84
     *
85
     * @param array $fieldDeclaration The field declaration.
86
     * @param AbstractPlatform $platform The currently used database platform.
87
     * @return string
88
     * @throws \Doctrine\DBAL\Exception
89
     */
90 66
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
91
    {
92 66
        $options = !array_key_exists('platformOptions', $fieldDeclaration) ?
93 66
            array() : $fieldDeclaration['platformOptions'];
94
95 66
        return $this->getMapTypeDeclarationSQL($platform, $fieldDeclaration, $options);
96
    }
97
98
    /**
99
     * Gets the SQL snippet used to declare an OBJECT column type.
100
     *
101
     * @param array $field
102
     *
103
     * @return string
104
     * @throws \Doctrine\DBAL\Exception
105
     */
106 66
    public function getMapTypeDeclarationSQL(AbstractPlatform $platform, array $field, array $options): string
0 ignored issues
show
Unused Code introduced by
The parameter $field is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

106
    public function getMapTypeDeclarationSQL(AbstractPlatform $platform, /** @scrutinizer ignore-unused */ array $field, array $options): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
107
    {
108 66
        $type = array_key_exists('type', $options) ? $options['type'] : MapType::DYNAMIC;
109
110 66
        $fields = array_key_exists('fields', $options) ? $options['fields'] : array();
111 66
        $columns = array();
112 66
        foreach ($fields as $field) {
113 64
            $columns[$field->getQuotedName($platform)] = CratePlatform::prepareColumnData($platform, $field);
114
        }
115 66
        $objectFields = $platform->getColumnDeclarationListSQL($columns);
116
117 66
        $declaration = count($columns) > 0 ? ' AS ( ' . $objectFields . ' )' : '';
118 66
        return 'OBJECT ( ' . $type . ' )' . $declaration ;
119
    }
120
}
121