Skip to content

Commit 7293cbb

Browse files
committed
Add toArray/fromArray helpers for a couple of classes
1 parent c88c97b commit 7293cbb

8 files changed

Lines changed: 336 additions & 4 deletions

File tree

src/XML/mdui/DomainHint.php

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,94 @@
44

55
namespace SimpleSAML\SAML2\XML\mdui;
66

7+
use SimpleSAML\SAML2\Assert\Assert;
8+
use SimpleSAML\SAML2\Exception\ArrayValidationException;
79
use SimpleSAML\SAML2\Type\DomainValue;
10+
use SimpleSAML\XML\ArrayizableElementInterface;
811
use SimpleSAML\XML\SchemaValidatableElementInterface;
912
use SimpleSAML\XML\SchemaValidatableElementTrait;
1013
use SimpleSAML\XML\TypedTextContentTrait;
1114

15+
use function array_change_key_case;
16+
use function array_keys;
17+
1218
/**
1319
* Class implementing DomainHint.
1420
*
1521
* @package simplesamlphp/saml2
1622
*/
17-
final class DomainHint extends AbstractMduiElement implements SchemaValidatableElementInterface
23+
final class DomainHint extends AbstractMduiElement implements
24+
ArrayizableElementInterface,
25+
SchemaValidatableElementInterface
1826
{
1927
use SchemaValidatableElementTrait;
2028
use TypedTextContentTrait;
2129

2230

2331
public const string TEXTCONTENT_TYPE = DomainValue::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+
DomainValue::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+
}
2497
}

src/XML/mdui/GeolocationHint.php

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,94 @@
44

55
namespace SimpleSAML\SAML2\XML\mdui;
66

7+
use SimpleSAML\SAML2\Assert\Assert;
8+
use SimpleSAML\SAML2\Exception\ArrayValidationException;
79
use SimpleSAML\SAML2\Type\GeolocationValue;
10+
use SimpleSAML\XML\ArrayizableElementInterface;
811
use SimpleSAML\XML\SchemaValidatableElementInterface;
912
use SimpleSAML\XML\SchemaValidatableElementTrait;
1013
use SimpleSAML\XML\TypedTextContentTrait;
1114

15+
use function array_change_key_case;
16+
use function array_keys;
17+
1218
/**
1319
* Class implementing GeolocationHint.
1420
*
1521
* @package simplesamlphp/saml2
1622
*/
17-
final class GeolocationHint extends AbstractMduiElement implements SchemaValidatableElementInterface
23+
final class GeolocationHint extends AbstractMduiElement implements
24+
ArrayizableElementInterface,
25+
SchemaValidatableElementInterface
1826
{
1927
use SchemaValidatableElementTrait;
2028
use TypedTextContentTrait;
2129

2230

2331
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+
}
2497
}

src/XML/mdui/IPHint.php

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,94 @@
44

55
namespace SimpleSAML\SAML2\XML\mdui;
66

7+
use SimpleSAML\SAML2\Assert\Assert;
8+
use SimpleSAML\SAML2\Exception\ArrayValidationException;
79
use SimpleSAML\SAML2\Type\CIDRValue;
10+
use SimpleSAML\XML\ArrayizableElementInterface;
811
use SimpleSAML\XML\SchemaValidatableElementInterface;
912
use SimpleSAML\XML\SchemaValidatableElementTrait;
1013
use SimpleSAML\XML\TypedTextContentTrait;
1114

15+
use function array_change_key_case;
16+
use function array_keys;
17+
1218
/**
1319
* Class implementing IPHint.
1420
*
1521
* @package simplesamlphp/saml2
1622
*/
17-
final class IPHint extends AbstractMduiElement implements SchemaValidatableElementInterface
23+
final class IPHint extends AbstractMduiElement implements
24+
ArrayizableElementInterface,
25+
SchemaValidatableElementInterface
1826
{
1927
use SchemaValidatableElementTrait;
2028
use TypedTextContentTrait;
2129

2230

2331
public const string TEXTCONTENT_TYPE = CIDRValue::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+
CIDRValue::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+
}
2497
}

0 commit comments

Comments
 (0)