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