|
20 | 20 | use Google\ApiCore\ArrayTrait; |
21 | 21 | use Google\ApiCore\Options\CallOptions; |
22 | 22 | use Google\Protobuf\NullValue; |
| 23 | +use Google\Protobuf\Internal\Message; |
23 | 24 |
|
24 | 25 | /** |
25 | 26 | * @internal |
@@ -264,4 +265,42 @@ private function splitOptionalArgs(array $input, array $extraAllowedKeys = []): |
264 | 265 |
|
265 | 266 | return [$input, $callOptions]; |
266 | 267 | } |
| 268 | + |
| 269 | + /** |
| 270 | + * Helper method used to validate optons based on the supplied $optionTypes |
| 271 | + * $optionTypes can be an array of string keys, a protobuf Message classname, or a |
| 272 | + * the CallOptions classname. Parameters are split and returned in the order |
| 273 | + * that the options types are provided. |
| 274 | + */ |
| 275 | + private function validateOptions(array $options, array|string ...$optionTypes): array |
| 276 | + { |
| 277 | + $splitOptions = []; |
| 278 | + foreach ($optionTypes as $optionType) { |
| 279 | + if (is_array($optionType)) { |
| 280 | + $splitOptions[] = $this->pluckArray($optionType, $options); |
| 281 | + } elseif (is_string($optionType)) { |
| 282 | + if (is_subclass_of($optionType, Message::class)) { |
| 283 | + $messageKeys = array_map( |
| 284 | + fn ($method) => lcfirst(substr($method, 3)), |
| 285 | + array_filter( |
| 286 | + get_class_methods($optionType), |
| 287 | + fn ($m) => 0 === strpos($m, 'get') |
| 288 | + ) |
| 289 | + ); |
| 290 | + $splitOptions[] = $this->pluckArray($messageKeys, $options); |
| 291 | + } elseif ($optionType === CallOptions::class) { |
| 292 | + $callOptionKeys = array_keys((new CallOptions([]))->toArray()); |
| 293 | + $splitOptions[] = $this->pluckArray($callOptionKeys, $options); |
| 294 | + } |
| 295 | + } |
| 296 | + } |
| 297 | + |
| 298 | + if (!empty($options)) { |
| 299 | + throw new \Exception( |
| 300 | + 'Unexpected option(s) provided: ' . implode(', ', array_keys($options)) |
| 301 | + ); |
| 302 | + } |
| 303 | + |
| 304 | + return $splitOptions; |
| 305 | + } |
267 | 306 | } |
0 commit comments