|
6 | 6 |
|
7 | 7 | use DOMElement; |
8 | 8 | use SimpleSAML\Assert\Assert; |
| 9 | +use SimpleSAML\SAML2\Exception\ArrayValidationException; |
9 | 10 | use SimpleSAML\XML\Exception\InvalidDOMElementException; |
10 | 11 | use SimpleSAML\XML\Exception\SchemaViolationException; |
11 | 12 |
|
| 13 | +use function array_change_key_case; |
| 14 | +use function array_filter; |
| 15 | +use function array_key_exists; |
| 16 | +use function array_keys; |
| 17 | + |
12 | 18 | /** |
13 | 19 | * Class for handling SAML2 IDPEntry. |
14 | 20 | * |
@@ -106,4 +112,80 @@ public function toXML(DOMElement $parent = null): DOMElement |
106 | 112 |
|
107 | 113 | return $e; |
108 | 114 | } |
| 115 | + |
| 116 | + |
| 117 | + /** |
| 118 | + * Create a class from an array |
| 119 | + * |
| 120 | + * @param array $data |
| 121 | + * @return static |
| 122 | + */ |
| 123 | + public static function fromArray(array $data): static |
| 124 | + { |
| 125 | + $data = self::processArrayContents($data); |
| 126 | + |
| 127 | + return new static( |
| 128 | + $data['ProviderID'], |
| 129 | + $data['Name'] ?? null, |
| 130 | + $data['Loc'] ?? null, |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | + /** |
| 136 | + * Validates an array representation of this object and returns the same array with |
| 137 | + * rationalized keys (casing) and parsed sub-elements. |
| 138 | + * |
| 139 | + * @param array $data |
| 140 | + * @return array $data |
| 141 | + */ |
| 142 | + private static function processArrayContents(array $data): array |
| 143 | + { |
| 144 | + $data = array_change_key_case($data, CASE_LOWER); |
| 145 | + |
| 146 | + // Make sure the array keys are known for this kind of object |
| 147 | + Assert::allOneOf( |
| 148 | + array_keys($data), |
| 149 | + [ |
| 150 | + 'providerid', |
| 151 | + 'name', |
| 152 | + 'loc', |
| 153 | + ], |
| 154 | + ArrayValidationException::class, |
| 155 | + ); |
| 156 | + |
| 157 | + Assert::keyExists($data, 'providerid', ArrayValidationException::class); |
| 158 | + Assert::string($data['providerid'], ArrayValidationException::class); |
| 159 | + |
| 160 | + $retval = ['ProviderID' => $data['providerid']]; |
| 161 | + |
| 162 | + if (array_key_exists('name', $data)) { |
| 163 | + Assert::string($data['name'], ArrayValidationException::class); |
| 164 | + $retval['Name'] = $data['name']; |
| 165 | + } |
| 166 | + |
| 167 | + if (array_key_exists('loc', $data)) { |
| 168 | + Assert::string($data['loc'], ArrayValidationException::class); |
| 169 | + $retval['Loc'] = $data['loc']; |
| 170 | + } |
| 171 | + |
| 172 | + return $retval; |
| 173 | + } |
| 174 | + |
| 175 | + |
| 176 | + /** |
| 177 | + * Create an array from this class |
| 178 | + * |
| 179 | + * @return array |
| 180 | + */ |
| 181 | + public function toArray(): array |
| 182 | + { |
| 183 | + $data = [ |
| 184 | + 'ProviderID' => $this->getProviderID(), |
| 185 | + 'Name' => $this->getName(), |
| 186 | + 'Loc' => $this->getLoc(), |
| 187 | + ]; |
| 188 | + |
| 189 | + return array_filter($data); |
| 190 | + } |
109 | 191 | } |
0 commit comments