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