Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions Core/src/OptionsValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,56 @@ public function validateOptions(array $options, array|Message|string ...$optionT

return $splitOptions;
}

/**
* @var array
*/
private static array $allowedKeysCache = [];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be at the top of the class between use ArrayTrait; and __construct


/**
* Filter an array of options to only include those matching the supplied `$optionTypes`.
*
* @param array $options
* @param array|Message|string ...$optionTypes
* @return array
*/
public function stripUnknownOptions(array $options, array|Message|string ...$optionTypes): array
{
$cacheKey = serialize(array_map(function ($type) {
return is_object($type) ? get_class($type) : $type;
}, $optionTypes));

if (isset(self::$allowedKeysCache[$cacheKey])) {
return array_intersect_key($options, array_flip(self::$allowedKeysCache[$cacheKey]));
}

$allowedKeys = [];
foreach ($optionTypes as $optionType) {
if (is_array($optionType)) {
$allowedKeys = array_merge($allowedKeys, $optionType);
} elseif ($optionType === CallOptions::class) {
$callOptionKeys = array_keys((new CallOptions([]))->toArray());
$allowedKeys = array_merge($allowedKeys, $callOptionKeys);
} elseif ($optionType instanceof Message

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should only support message class name in this case. There's no reason to provide a message instance (as there is for validateOptions).

|| (is_string($optionType) && is_subclass_of($optionType, Message::class))
) {
$messageKeys = array_map(
fn ($method) => lcfirst(substr($method, 3)),
array_filter(
get_class_methods($optionType),
fn ($m) => 0 === strpos($m, 'get')
)
);
$allowedKeys = array_merge($allowedKeys, $messageKeys);
} elseif (is_string($optionType)) {
$allowedKeys[] = $optionType;
Comment on lines +150 to +151

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit but we probably don't need this... we can just say scalars should always be string[]

} else {
throw new LogicException(sprintf('Invalid option type: %s', $optionType));
}
}

self::$allowedKeysCache[$cacheKey] = $allowedKeys;

return array_intersect_key($options, array_flip($allowedKeys));
}
}
40 changes: 40 additions & 0 deletions Core/tests/Unit/DummyMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Copyright 2025 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Core\Tests\Unit;

use Google\Protobuf\Internal\Message;

/**
* A dummy message for testing OptionsValidator message extraction.
*/
class DummyMessage extends Message
{
/**
* @return void
*/
public function getQueryOptions()
{
}

/**
* @return void
*/
public function getRequestOptions()
{
}
}
59 changes: 59 additions & 0 deletions Core/tests/Unit/OptionsValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Copyright 2025 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Core\Tests\Unit;

use Google\ApiCore\Options\CallOptions;
use Google\Cloud\Core\OptionsValidator;
use PHPUnit\Framework\TestCase;

/**
* @group core
*/
class OptionsValidatorTest extends TestCase
{
private $validator;

public function setUp(): void
{
$this->validator = new OptionsValidator();
}

public function testStripUnknownOptions()
{
$options = [
'parameters' => ['a' => 1],
'queryOptions' => ['optimizerVersion' => '1'],
'requestOptions' => ['priority' => 1],
'timeoutMillis' => 100,
'unknown' => 'strip me'
];

$stripped = $this->validator->stripUnknownOptions(
$options,
['parameters'],
CallOptions::class,
DummyMessage::class
);

$this->assertArrayHasKey('parameters', $stripped);
$this->assertArrayHasKey('queryOptions', $stripped); // From DummyMessage
$this->assertArrayHasKey('requestOptions', $stripped); // From DummyMessage
$this->assertArrayHasKey('timeoutMillis', $stripped); // From CallOptions
$this->assertArrayNotHasKey('unknown', $stripped);
}
}
5 changes: 3 additions & 2 deletions Spanner/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"require": {
"php": "^8.1",
"ext-grpc": "*",
"google/cloud-core": "^1.68",
"google/cloud-core": "^1.73.0",
"google/gax": "^1.41.0",
"google/cloud-monitoring": "^2.2",
"open-telemetry/sdk": "^1.13"
Expand All @@ -21,7 +21,8 @@
"google/cloud-pubsub": "^2.0",
"dg/bypass-finals": "^1.7",
"dms/phpunit-arraysubset-asserts": "^0.5.0",
"symfony/process": "^6.4"
"symfony/process": "^6.4",
"nikic/php-parser": "^5.0"
},
"suggest": {
"ext-protobuf": "Provides a significant increase in throughput over the pure PHP protobuf implementation. See https://cloud.google.com/php/grpc for installation instructions.",
Expand Down
Loading
Loading