|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2025 Google Inc. All Rights Reserved. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +namespace Google\Cloud\Core; |
| 19 | + |
| 20 | +use Google\ApiCore\ArrayTrait; |
| 21 | +use Google\ApiCore\Options\CallOptions; |
| 22 | +use Google\ApiCore\Serializer; |
| 23 | +use Google\Protobuf\Internal\Message; |
| 24 | +use LogicException; |
| 25 | + |
| 26 | +/** |
| 27 | + * Helper used to validate options |
| 28 | + * |
| 29 | + * @internal |
| 30 | + */ |
| 31 | +class OptionsValidator |
| 32 | +{ |
| 33 | + use ArrayTrait; |
| 34 | + |
| 35 | + /** |
| 36 | + * @param ?Serializer $serializer use a serializer to decode protobuf messages |
| 37 | + * instead of calling {@see Message::mergeFromJsonString()}. |
| 38 | + */ |
| 39 | + public function __construct(private ?Serializer $serializer = null) |
| 40 | + { |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Validate an array of options based on the supplied `$optionTypes`. |
| 45 | + * $optionTypes can be an array of string keys, a protobuf Message classname, or a |
| 46 | + * the CallOptions classname. Parameters are split and returned in the order |
| 47 | + * that the options types are provided. |
| 48 | + * |
| 49 | + * - If the option type is an array, any keys in $options matching the string values |
| 50 | + * of the array are returned. |
| 51 | + * - If the option type is {@see Message}, any keys matching getters will be set on the message. |
| 52 | + * - If the option type is string, and that string is a valid {@see CallOptions} option, those |
| 53 | + * options will be returned in an array |
| 54 | + * |
| 55 | + * ``` |
| 56 | + * [$customOps, $commitRequest, $callOptions] = $optionsValidator->vaidateOptions( |
| 57 | + * $options, |
| 58 | + * ['customOp1', 'customOp2'], |
| 59 | + * new CommitRequest(), |
| 60 | + * CallOptions::class, |
| 61 | + * ); |
| 62 | + * ``` |
| 63 | + * |
| 64 | + * @param array $options |
| 65 | + * @param array|Message|string ...$optionTypes |
| 66 | + * @return array |
| 67 | + * @throws LogicException when a value exists which is not supported by any of the `$optionTypes`. |
| 68 | + */ |
| 69 | + public function validateOptions(array $options, array|Message|string ...$optionTypes): array |
| 70 | + { |
| 71 | + $splitOptions = []; |
| 72 | + foreach ($optionTypes as $optionType) { |
| 73 | + if (is_array($optionType)) { |
| 74 | + $splitOptions[] = $this->pluckArray($optionType, $options); |
| 75 | + } elseif ($optionType === CallOptions::class) { |
| 76 | + $callOptionKeys = array_keys((new CallOptions([]))->toArray()); |
| 77 | + $splitOptions[] = $this->pluckArray($callOptionKeys, $options); |
| 78 | + } elseif ($optionType instanceof Message) { |
| 79 | + $messageKeys = array_map( |
| 80 | + fn ($method) => lcfirst(substr($method, 3)), |
| 81 | + array_filter( |
| 82 | + get_class_methods($optionType), |
| 83 | + fn ($m) => 0 === strpos($m, 'get') |
| 84 | + ) |
| 85 | + ); |
| 86 | + $messageOptions = $this->pluckArray($messageKeys, $options); |
| 87 | + if ($this->serializer) { |
| 88 | + $optionType = $this->serializer->decodeMessage($optionType, $messageOptions); |
| 89 | + } else { |
| 90 | + $optionType->mergeFromJsonString(json_encode($messageOptions, JSON_FORCE_OBJECT)); |
| 91 | + } |
| 92 | + $splitOptions[] = $optionType; |
| 93 | + } else { |
| 94 | + throw new LogicException(sprintf('Invalid option type: %s', $optionType)); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (!empty($options)) { |
| 99 | + throw new LogicException( |
| 100 | + 'Unexpected option(s) provided: ' . implode(', ', array_keys($options)) |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + return $splitOptions; |
| 105 | + } |
| 106 | +} |
0 commit comments