|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace ApiClients\Client\GitHubEnterpriseCloud\Internal\Hydrator\Operation\Orgs\Org; |
| 6 | + |
| 7 | +use ApiClients\Client\GitHubEnterpriseCloud\Schema\BasicError; |
| 8 | +use ApiClients\Client\GitHubEnterpriseCloud\Schema\ValidationError; |
| 9 | +use EventSauce\ObjectHydrator\IterableList; |
| 10 | +use EventSauce\ObjectHydrator\ObjectMapper; |
| 11 | +use EventSauce\ObjectHydrator\PropertySerializers\SerializeArrayItems; |
| 12 | +use EventSauce\ObjectHydrator\PropertySerializers\SerializeDateTime; |
| 13 | +use EventSauce\ObjectHydrator\PropertySerializers\SerializeUuidToString; |
| 14 | +use EventSauce\ObjectHydrator\UnableToHydrateObject; |
| 15 | +use EventSauce\ObjectHydrator\UnableToSerializeObject; |
| 16 | +use Generator; |
| 17 | +use LogicException; |
| 18 | +use Throwable; |
| 19 | + |
| 20 | +use function assert; |
| 21 | +use function count; |
| 22 | +use function is_a; |
| 23 | + |
| 24 | +class OrganizationFineGrainedPermissions implements ObjectMapper |
| 25 | +{ |
| 26 | + private array $hydrationStack = []; |
| 27 | + |
| 28 | + public function __construct() |
| 29 | + { |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @param class-string<T> $className |
| 34 | + * |
| 35 | + * @return T |
| 36 | + * |
| 37 | + * @template T of object |
| 38 | + */ |
| 39 | + public function hydrateObject(string $className, array $payload): object |
| 40 | + { |
| 41 | + return match ($className) { |
| 42 | + 'ApiClients\Client\GitHubEnterpriseCloud\Schema\BasicError' => $this->hydrateApiClients⚡️Client⚡️GitHubEnterpriseCloud⚡️Schema⚡️BasicError($payload), |
| 43 | + 'ApiClients\Client\GitHubEnterpriseCloud\Schema\ValidationError' => $this->hydrateApiClients⚡️Client⚡️GitHubEnterpriseCloud⚡️Schema⚡️ValidationError($payload), |
| 44 | + default => throw UnableToHydrateObject::noHydrationDefined($className, $this->hydrationStack), |
| 45 | + }; |
| 46 | + } |
| 47 | + |
| 48 | + private function hydrateApiClients⚡️Client⚡️GitHubEnterpriseCloud⚡️Schema⚡️BasicError(array $payload): BasicError |
| 49 | + { |
| 50 | + $properties = []; |
| 51 | + $missingFields = []; |
| 52 | + try { |
| 53 | + $value = $payload['message'] ?? null; |
| 54 | + |
| 55 | + if ($value === null) { |
| 56 | + $properties['message'] = null; |
| 57 | + goto after_message; |
| 58 | + } |
| 59 | + |
| 60 | + $properties['message'] = $value; |
| 61 | + |
| 62 | + after_message: |
| 63 | + |
| 64 | + $value = $payload['documentation_url'] ?? null; |
| 65 | + |
| 66 | + if ($value === null) { |
| 67 | + $properties['documentationUrl'] = null; |
| 68 | + goto after_documentationUrl; |
| 69 | + } |
| 70 | + |
| 71 | + $properties['documentationUrl'] = $value; |
| 72 | + |
| 73 | + after_documentationUrl: |
| 74 | + |
| 75 | + $value = $payload['url'] ?? null; |
| 76 | + |
| 77 | + if ($value === null) { |
| 78 | + $properties['url'] = null; |
| 79 | + goto after_url; |
| 80 | + } |
| 81 | + |
| 82 | + $properties['url'] = $value; |
| 83 | + |
| 84 | + after_url: |
| 85 | + |
| 86 | + $value = $payload['status'] ?? null; |
| 87 | + |
| 88 | + if ($value === null) { |
| 89 | + $properties['status'] = null; |
| 90 | + goto after_status; |
| 91 | + } |
| 92 | + |
| 93 | + $properties['status'] = $value; |
| 94 | + |
| 95 | + after_status: |
| 96 | + } catch (Throwable $exception) { |
| 97 | + throw UnableToHydrateObject::dueToError('ApiClients\Client\GitHubEnterpriseCloud\Schema\BasicError', $exception, stack: $this->hydrationStack); |
| 98 | + } |
| 99 | + |
| 100 | + if (count($missingFields) > 0) { |
| 101 | + throw UnableToHydrateObject::dueToMissingFields(BasicError::class, $missingFields, stack: $this->hydrationStack); |
| 102 | + } |
| 103 | + |
| 104 | + try { |
| 105 | + return new BasicError(...$properties); |
| 106 | + } catch (Throwable $exception) { |
| 107 | + throw UnableToHydrateObject::dueToError('ApiClients\Client\GitHubEnterpriseCloud\Schema\BasicError', $exception, stack: $this->hydrationStack); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private function hydrateApiClients⚡️Client⚡️GitHubEnterpriseCloud⚡️Schema⚡️ValidationError(array $payload): ValidationError |
| 112 | + { |
| 113 | + $properties = []; |
| 114 | + $missingFields = []; |
| 115 | + try { |
| 116 | + $value = $payload['message'] ?? null; |
| 117 | + |
| 118 | + if ($value === null) { |
| 119 | + $missingFields[] = 'message'; |
| 120 | + goto after_message; |
| 121 | + } |
| 122 | + |
| 123 | + $properties['message'] = $value; |
| 124 | + |
| 125 | + after_message: |
| 126 | + |
| 127 | + $value = $payload['documentation_url'] ?? null; |
| 128 | + |
| 129 | + if ($value === null) { |
| 130 | + $missingFields[] = 'documentation_url'; |
| 131 | + goto after_documentationUrl; |
| 132 | + } |
| 133 | + |
| 134 | + $properties['documentationUrl'] = $value; |
| 135 | + |
| 136 | + after_documentationUrl: |
| 137 | + |
| 138 | + $value = $payload['errors'] ?? null; |
| 139 | + |
| 140 | + if ($value === null) { |
| 141 | + $properties['errors'] = null; |
| 142 | + goto after_errors; |
| 143 | + } |
| 144 | + |
| 145 | + $properties['errors'] = $value; |
| 146 | + |
| 147 | + after_errors: |
| 148 | + } catch (Throwable $exception) { |
| 149 | + throw UnableToHydrateObject::dueToError('ApiClients\Client\GitHubEnterpriseCloud\Schema\ValidationError', $exception, stack: $this->hydrationStack); |
| 150 | + } |
| 151 | + |
| 152 | + if (count($missingFields) > 0) { |
| 153 | + throw UnableToHydrateObject::dueToMissingFields(ValidationError::class, $missingFields, stack: $this->hydrationStack); |
| 154 | + } |
| 155 | + |
| 156 | + try { |
| 157 | + return new ValidationError(...$properties); |
| 158 | + } catch (Throwable $exception) { |
| 159 | + throw UnableToHydrateObject::dueToError('ApiClients\Client\GitHubEnterpriseCloud\Schema\ValidationError', $exception, stack: $this->hydrationStack); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + private function serializeViaTypeMap(string $accessor, object $object, array $payloadToTypeMap): array |
| 164 | + { |
| 165 | + foreach ($payloadToTypeMap as $payloadType => [$valueType, $method]) { |
| 166 | + if (is_a($object, $valueType)) { |
| 167 | + return [$accessor => $payloadType] + $this->{$method}($object); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + throw new LogicException('No type mapped for object of class: ' . $object::class); |
| 172 | + } |
| 173 | + |
| 174 | + public function serializeObject(object $object): mixed |
| 175 | + { |
| 176 | + return $this->serializeObjectOfType($object, $object::class); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * @param T $object |
| 181 | + * @param class-string<T> $className |
| 182 | + * |
| 183 | + * @template T |
| 184 | + */ |
| 185 | + public function serializeObjectOfType(object $object, string $className): mixed |
| 186 | + { |
| 187 | + try { |
| 188 | + return match ($className) { |
| 189 | + 'array' => $this->serializeValuearray($object), |
| 190 | + 'Ramsey\Uuid\UuidInterface' => $this->serializeValueRamsey⚡️Uuid⚡️UuidInterface($object), |
| 191 | + 'DateTime' => $this->serializeValueDateTime($object), |
| 192 | + 'DateTimeImmutable' => $this->serializeValueDateTimeImmutable($object), |
| 193 | + 'DateTimeInterface' => $this->serializeValueDateTimeInterface($object), |
| 194 | + 'ApiClients\Client\GitHubEnterpriseCloud\Schema\BasicError' => $this->serializeObjectApiClients⚡️Client⚡️GitHubEnterpriseCloud⚡️Schema⚡️BasicError($object), |
| 195 | + 'ApiClients\Client\GitHubEnterpriseCloud\Schema\ValidationError' => $this->serializeObjectApiClients⚡️Client⚡️GitHubEnterpriseCloud⚡️Schema⚡️ValidationError($object), |
| 196 | + default => throw new LogicException('No serialization defined for $className'), |
| 197 | + }; |
| 198 | + } catch (Throwable $exception) { |
| 199 | + throw UnableToSerializeObject::dueToError($className, $exception); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + private function serializeValuearray(mixed $value): mixed |
| 204 | + { |
| 205 | + static $serializer; |
| 206 | + |
| 207 | + if ($serializer === null) { |
| 208 | + $serializer = new SerializeArrayItems(...[]); |
| 209 | + } |
| 210 | + |
| 211 | + return $serializer->serialize($value, $this); |
| 212 | + } |
| 213 | + |
| 214 | + private function serializeValueRamsey⚡️Uuid⚡️UuidInterface(mixed $value): mixed |
| 215 | + { |
| 216 | + static $serializer; |
| 217 | + |
| 218 | + if ($serializer === null) { |
| 219 | + $serializer = new SerializeUuidToString(...[]); |
| 220 | + } |
| 221 | + |
| 222 | + return $serializer->serialize($value, $this); |
| 223 | + } |
| 224 | + |
| 225 | + private function serializeValueDateTime(mixed $value): mixed |
| 226 | + { |
| 227 | + static $serializer; |
| 228 | + |
| 229 | + if ($serializer === null) { |
| 230 | + $serializer = new SerializeDateTime(...[]); |
| 231 | + } |
| 232 | + |
| 233 | + return $serializer->serialize($value, $this); |
| 234 | + } |
| 235 | + |
| 236 | + private function serializeValueDateTimeImmutable(mixed $value): mixed |
| 237 | + { |
| 238 | + static $serializer; |
| 239 | + |
| 240 | + if ($serializer === null) { |
| 241 | + $serializer = new SerializeDateTime(...[]); |
| 242 | + } |
| 243 | + |
| 244 | + return $serializer->serialize($value, $this); |
| 245 | + } |
| 246 | + |
| 247 | + private function serializeValueDateTimeInterface(mixed $value): mixed |
| 248 | + { |
| 249 | + static $serializer; |
| 250 | + |
| 251 | + if ($serializer === null) { |
| 252 | + $serializer = new SerializeDateTime(...[]); |
| 253 | + } |
| 254 | + |
| 255 | + return $serializer->serialize($value, $this); |
| 256 | + } |
| 257 | + |
| 258 | + private function serializeObjectApiClients⚡️Client⚡️GitHubEnterpriseCloud⚡️Schema⚡️BasicError(mixed $object): mixed |
| 259 | + { |
| 260 | + assert($object instanceof BasicError); |
| 261 | + $result = []; |
| 262 | + |
| 263 | + $message = $object->message; |
| 264 | + |
| 265 | + if ($message === null) { |
| 266 | + goto after_message; |
| 267 | + } |
| 268 | + |
| 269 | + after_message: $result['message'] = $message; |
| 270 | + |
| 271 | + $documentationUrl = $object->documentationUrl; |
| 272 | + |
| 273 | + if ($documentationUrl === null) { |
| 274 | + goto after_documentationUrl; |
| 275 | + } |
| 276 | + |
| 277 | + after_documentationUrl: $result['documentation_url'] = $documentationUrl; |
| 278 | + |
| 279 | + $url = $object->url; |
| 280 | + |
| 281 | + if ($url === null) { |
| 282 | + goto after_url; |
| 283 | + } |
| 284 | + |
| 285 | + after_url: $result['url'] = $url; |
| 286 | + |
| 287 | + $status = $object->status; |
| 288 | + |
| 289 | + if ($status === null) { |
| 290 | + goto after_status; |
| 291 | + } |
| 292 | + |
| 293 | + after_status: $result['status'] = $status; |
| 294 | + |
| 295 | + return $result; |
| 296 | + } |
| 297 | + |
| 298 | + private function serializeObjectApiClients⚡️Client⚡️GitHubEnterpriseCloud⚡️Schema⚡️ValidationError(mixed $object): mixed |
| 299 | + { |
| 300 | + assert($object instanceof ValidationError); |
| 301 | + $result = []; |
| 302 | + |
| 303 | + $message = $object->message; |
| 304 | + after_message: $result['message'] = $message; |
| 305 | + |
| 306 | + $documentationUrl = $object->documentationUrl; |
| 307 | + after_documentationUrl: $result['documentation_url'] = $documentationUrl; |
| 308 | + |
| 309 | + $errors = $object->errors; |
| 310 | + |
| 311 | + if ($errors === null) { |
| 312 | + goto after_errors; |
| 313 | + } |
| 314 | + |
| 315 | + static $errorsSerializer0; |
| 316 | + |
| 317 | + if ($errorsSerializer0 === null) { |
| 318 | + $errorsSerializer0 = new SerializeArrayItems(...[]); |
| 319 | + } |
| 320 | + |
| 321 | + $errors = $errorsSerializer0->serialize($errors, $this); |
| 322 | + after_errors: $result['errors'] = $errors; |
| 323 | + |
| 324 | + return $result; |
| 325 | + } |
| 326 | + |
| 327 | + /** |
| 328 | + * @param class-string<T> $className |
| 329 | + * @param iterable<array> $payloads; |
| 330 | + * |
| 331 | + * @return IterableList<T> |
| 332 | + * |
| 333 | + * @throws UnableToHydrateObject |
| 334 | + * |
| 335 | + * @template T |
| 336 | + */ |
| 337 | + public function hydrateObjects(string $className, iterable $payloads): IterableList |
| 338 | + { |
| 339 | + return new IterableList($this->doHydrateObjects($className, $payloads)); |
| 340 | + } |
| 341 | + |
| 342 | + private function doHydrateObjects(string $className, iterable $payloads): Generator |
| 343 | + { |
| 344 | + foreach ($payloads as $index => $payload) { |
| 345 | + yield $index => $this->hydrateObject($className, $payload); |
| 346 | + } |
| 347 | + } |
| 348 | + |
| 349 | + /** |
| 350 | + * @param class-string<T> $className |
| 351 | + * @param iterable<array> $payloads; |
| 352 | + * |
| 353 | + * @return IterableList<T> |
| 354 | + * |
| 355 | + * @throws UnableToSerializeObject |
| 356 | + * |
| 357 | + * @template T |
| 358 | + */ |
| 359 | + public function serializeObjects(iterable $payloads): IterableList |
| 360 | + { |
| 361 | + return new IterableList($this->doSerializeObjects($payloads)); |
| 362 | + } |
| 363 | + |
| 364 | + private function doSerializeObjects(iterable $objects): Generator |
| 365 | + { |
| 366 | + foreach ($objects as $index => $object) { |
| 367 | + yield $index => $this->serializeObject($object); |
| 368 | + } |
| 369 | + } |
| 370 | +} |
0 commit comments