|
4 | 4 |
|
5 | 5 | namespace SimpleSAML\SAML2\XML\mdui; |
6 | 6 |
|
| 7 | +use SimpleSAML\SAML2\Assert\Assert; |
| 8 | +use SimpleSAML\SAML2\Exception\ArrayValidationException; |
7 | 9 | use SimpleSAML\SAML2\Type\GeolocationValue; |
| 10 | +use SimpleSAML\XML\ArrayizableElementInterface; |
8 | 11 | use SimpleSAML\XML\SchemaValidatableElementInterface; |
9 | 12 | use SimpleSAML\XML\SchemaValidatableElementTrait; |
10 | 13 | use SimpleSAML\XML\TypedTextContentTrait; |
11 | 14 |
|
| 15 | +use function array_change_key_case; |
| 16 | +use function array_keys; |
| 17 | + |
12 | 18 | /** |
13 | 19 | * Class implementing GeolocationHint. |
14 | 20 | * |
15 | 21 | * @package simplesamlphp/saml2 |
16 | 22 | */ |
17 | | -final class GeolocationHint extends AbstractMduiElement implements SchemaValidatableElementInterface |
| 23 | +final class GeolocationHint extends AbstractMduiElement implements |
| 24 | + ArrayizableElementInterface, |
| 25 | + SchemaValidatableElementInterface |
18 | 26 | { |
19 | 27 | use SchemaValidatableElementTrait; |
20 | 28 | use TypedTextContentTrait; |
21 | 29 |
|
22 | 30 |
|
23 | 31 | public const string TEXTCONTENT_TYPE = GeolocationValue::class; |
| 32 | + |
| 33 | + |
| 34 | + /** |
| 35 | + * Create a class from an array |
| 36 | + * |
| 37 | + * @param array{ |
| 38 | + * 'url': string, |
| 39 | + * } $data |
| 40 | + */ |
| 41 | + public static function fromArray(array $data): static |
| 42 | + { |
| 43 | + $data = self::processArrayContents($data); |
| 44 | + |
| 45 | + return new static( |
| 46 | + GeolocationValue::fromString($data['hint']), |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + /** |
| 52 | + * Validates an array representation of this object and returns the same array with |
| 53 | + * rationalized keys (casing) and parsed sub-elements. |
| 54 | + * |
| 55 | + * @param array{ |
| 56 | + * 'hint': string, |
| 57 | + * } $data |
| 58 | + * @return array{ |
| 59 | + * 'hint': string, |
| 60 | + * } |
| 61 | + */ |
| 62 | + private static function processArrayContents(array $data): array |
| 63 | + { |
| 64 | + $data = array_change_key_case($data, CASE_LOWER); |
| 65 | + |
| 66 | + // Make sure the array keys are known for this kind of object |
| 67 | + Assert::allOneOf( |
| 68 | + array_keys($data), |
| 69 | + [ |
| 70 | + 'hint', |
| 71 | + ], |
| 72 | + ArrayValidationException::class, |
| 73 | + ); |
| 74 | + |
| 75 | + Assert::keyExists($data, 'hint', ArrayValidationException::class); |
| 76 | + Assert::string($data['hint'], ArrayValidationException::class); |
| 77 | + |
| 78 | + return [ |
| 79 | + 'hint' => $data['hint'], |
| 80 | + ]; |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + /** |
| 85 | + * Create an array from this class |
| 86 | + * |
| 87 | + * @return array{ |
| 88 | + * 'hint': string, |
| 89 | + * } |
| 90 | + */ |
| 91 | + public function toArray(): array |
| 92 | + { |
| 93 | + return [ |
| 94 | + 'hint' => $this->getContent()->getValue(), |
| 95 | + ]; |
| 96 | + } |
24 | 97 | } |
0 commit comments