Completed
Push — master ( ace74a...38a91c )
by Daan van
04:54
created

DocumentNumberType::convertToDatabaseValue()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
rs 8.8571
cc 5
eloc 10
nc 3
nop 2
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
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
namespace Surfnet\StepupMiddleware\ApiBundle\Doctrine\Type;
20
21
use Doctrine\DBAL\Platforms\AbstractPlatform;
22
use Doctrine\DBAL\Types\ConversionException;
23
use Doctrine\DBAL\Types\Type;
24
use Surfnet\Stepup\Identity\Value\DocumentNumber;
25
26
/**
27
 * Custom Type for the Surfnet\Stepup\Identity\Value\DocumentNumber Object
28
 */
29
class DocumentNumberType extends Type
30
{
31
    const NAME = 'stepup_document_number';
32
33
    /**
34
     * @param array            $fieldDeclaration
35
     * @param AbstractPlatform $platform
36
     * @return string
37
     * @throws \Doctrine\DBAL\DBALException
38
     */
39
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
40
    {
41
        return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration);
42
    }
43
44
    /**
45
     * @param mixed $value
46
     * @param AbstractPlatform $platform
47
     * @return null|string
48
     * @throws ConversionException
49
     */
50
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
51
    {
52
        if (is_null($value)) {
53
            return null;
54
        }
55
56
        if (!$value instanceof DocumentNumber) {
57
            throw new ConversionException(
58
                sprintf(
59
                    "Encountered illegal document number of type %s '%s', expected a DocumentNumber instance",
60
                    is_object($value) ? get_class($value) : gettype($value),
61
                    is_scalar($value) ? (string) $value : ''
62
                )
63
            );
64
        }
65
66
        return $value->getDocumentNumber();
67
    }
68
69
    /**
70
     * @param mixed            $value
71
     * @param AbstractPlatform $platform
72
     * @return null|DocumentNumber
73
     * @throws ConversionException
74
     */
75
    public function convertToPHPValue($value, AbstractPlatform $platform)
76
    {
77
        if (is_null($value)) {
78
            return null;
79
        }
80
81
        return new DocumentNumber($value);
82
    }
83
84
    public function getName()
85
    {
86
        return self::NAME;
87
    }
88
}
89