DocumentNumberType::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
0 ignored issues
show
Coding Style introduced by
The file-level docblock must follow the opening PHP tag in the file header
Loading history...
6
 * Copyright 2014 SURFnet bv
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
namespace Surfnet\StepupMiddleware\ApiBundle\Doctrine\Type;
22
23
use Doctrine\DBAL\Platforms\AbstractPlatform;
24
use Doctrine\DBAL\Types\ConversionException;
25
use Doctrine\DBAL\Types\Type;
26
use Surfnet\Stepup\Exception\InvalidArgumentException;
27
use Surfnet\Stepup\Identity\Value\DocumentNumber;
28
29
/**
30
 * Custom Type for the Surfnet\Stepup\Identity\Value\DocumentNumber Object
31
 */
32
class DocumentNumberType extends Type
33
{
34
    public const NAME = 'stepup_document_number';
35
36
    public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
37
    {
38
        if (!isset($column['length'])) {
39
            $column['length'] = 255;
40
        }
41
42
        return $platform->getStringTypeDeclarationSQL($column);
43
    }
44
45
    /**
46
     * @throws ConversionException
47
     */
48
    public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
49
    {
50
        if (is_null($value)) {
51
            return null;
52
        }
53
54
        if (!$value instanceof DocumentNumber) {
55
            throw new ConversionException(
56
                sprintf(
57
                    "Encountered illegal document number of type %s '%s', expected a DocumentNumber instance",
58
                    get_debug_type($value),
59
                    is_scalar($value) ? (string)$value : '',
60
                ),
61
            );
62
        }
63
64
        return $value->getDocumentNumber();
65
    }
66
67
    /**
68
     * @throws InvalidArgumentException
69
     */
70
    public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?DocumentNumber
71
    {
72
        if (is_null($value)) {
73
            return null;
74
        }
75
76
        return new DocumentNumber($value);
77
    }
78
79
    public function getName(): string
80
    {
81
        return self::NAME;
82
    }
83
84
    public function requiresSQLCommentHint(AbstractPlatform $platform): bool
85
    {
86
        return false;
87
    }
88
}
89