1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2022 SURFnet B.V. |
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\IntegerType; |
24
|
|
|
use Doctrine\DBAL\Types\Type; |
25
|
|
|
use phpseclib3\Math\PrimeField\Integer; |
26
|
|
|
use Surfnet\Stepup\Configuration\Value\SelfAssertedTokensOption; |
|
|
|
|
27
|
|
|
use TypeError; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Custom Type for the SelfAssertedTokens options Value Object |
31
|
|
|
*/ |
|
|
|
|
32
|
|
|
class SelfAssertedTokensOptionType extends IntegerType |
33
|
|
|
{ |
34
|
|
|
public const NAME = 'stepup_self_asserted_tokens_option'; |
35
|
|
|
|
36
|
|
|
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string |
37
|
|
|
{ |
38
|
|
|
return $platform->getIntegerTypeDeclarationSQL($column); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?int |
42
|
|
|
{ |
43
|
|
|
if (is_null($value)) { |
44
|
|
|
return null; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (!$value instanceof SelfAssertedTokensOption) { |
48
|
|
|
throw new ConversionException( |
49
|
|
|
sprintf( |
50
|
|
|
"Encountered illegal self vet option %s '%s', expected a |
51
|
|
|
SelfAssertedTokensOption instance", |
52
|
|
|
get_debug_type($value), |
53
|
|
|
is_scalar($value) ? (string)$value : '', |
54
|
|
|
), |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return (int)$value->isEnabled(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function convertToPHPValue($value, AbstractPlatform $platform): ?SelfAssertedTokensOption |
62
|
|
|
{ |
63
|
|
|
if (is_null($value)) { |
64
|
|
|
return null; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
try { |
68
|
|
|
$selfAssertedTokensOption = new SelfAssertedTokensOption((bool)$value); |
69
|
|
|
} catch (TypeError $e) { |
70
|
|
|
// get nice standard message, so we can throw it keeping the exception chain |
71
|
|
|
$doctrineExceptionMessage = ConversionException::conversionFailed( |
72
|
|
|
$value, |
73
|
|
|
$this->getName(), |
74
|
|
|
)->getMessage(); |
75
|
|
|
|
76
|
|
|
throw new ConversionException($doctrineExceptionMessage, 0, $e); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $selfAssertedTokensOption; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getName(): string |
83
|
|
|
{ |
84
|
|
|
return self::NAME; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|