1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
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
|
|
|
return $platform->getVarcharTypeDeclarationSQL($column); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
|
|
|
|
42
|
|
|
* @throws ConversionException |
43
|
|
|
*/ |
|
|
|
|
44
|
|
|
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string |
45
|
|
|
{ |
46
|
|
|
if (is_null($value)) { |
47
|
|
|
return null; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (!$value instanceof DocumentNumber) { |
51
|
|
|
throw new ConversionException( |
52
|
|
|
sprintf( |
53
|
|
|
"Encountered illegal document number of type %s '%s', expected a DocumentNumber instance", |
54
|
|
|
get_debug_type($value), |
55
|
|
|
is_scalar($value) ? (string)$value : '', |
56
|
|
|
), |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $value->getDocumentNumber(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
|
|
|
|
64
|
|
|
* @throws InvalidArgumentException |
65
|
|
|
*/ |
|
|
|
|
66
|
|
|
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?DocumentNumber |
67
|
|
|
{ |
68
|
|
|
if (is_null($value)) { |
69
|
|
|
return null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return new DocumentNumber($value); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getName(): string |
76
|
|
|
{ |
77
|
|
|
return self::NAME; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|