Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPLIB-1505 Add driver option builderEncoder #1382

Merged
merged 1 commit into from
Sep 10, 2024

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Sep 9, 2024

Fix PHPLIB-1505

The new option builderEncoder contains a MongoDB\Codec\Encoder object that will be used to encode queries and pipelines. When not set, it defaults to a new instance of MongoDB\Builder\BuilderEncoder.

$client = new Client(static::getUri(), $uriOptions);
$driverOptions = [
'builderEncoder' => $builderEncoder = $this->createMock(Encoder::class),
'typeMap' => ['root' => 'array'],
Copy link
Member Author

Choose a reason for hiding this comment

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

typeMap option was not tested here.

@@ -209,23 +208,28 @@ protected function getCollectionName(): string
*/
protected function getInvalidArrayValues(bool $includeNull = false): array
{
return array_merge([123, 3.14, 'foo', true, new stdClass()], $includeNull ? [null] : []);
return [123, 3.14, 'foo', true, new stdClass(), ...($includeNull ? [null] : [])];
Copy link
Member Author

@GromNaN GromNaN Sep 10, 2024

Choose a reason for hiding this comment

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

Reformatting data provider using array unpacking.

Copy link
Member

Choose a reason for hiding this comment

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

I feel bad for forgetting PHP has this feature now 😅

src/Client.php Show resolved Hide resolved
src/Client.php Outdated Show resolved Hide resolved
Copy link
Member

@alcaeus alcaeus left a comment

Choose a reason for hiding this comment

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

I'd suggest making the encoder property readonly, but LGTM other than that.

src/Client.php Outdated
@@ -78,6 +83,8 @@ class Client
*
* Supported driver-specific options:
*
* * builderEncoder (MongoDB\Builder\Encoder): Encoder for query builder
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion to call out there's a default value being used:

Suggested change
* * builderEncoder (MongoDB\Builder\Encoder): Encoder for query builder
* * builderEncoder (MongoDB\Builder\Encoder): Encoder for query and aggregation builders. If not given, the default encoder will be used.

If you accept this, it would make sense to apply this to the Database and Collection classes as well.

src/Client.php Outdated
@@ -67,6 +70,8 @@ class Client

private array $typeMap;

private Encoder $builderEncoder;
Copy link
Member

Choose a reason for hiding this comment

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

PHP 8.1 allows us to make this readonly:

Suggested change
private Encoder $builderEncoder;
private readonly Encoder $builderEncoder;

Copy link
Member

Choose a reason for hiding this comment

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

Also, just thought of this after seeing the Psalm errors in the follow-up PR, but specifying a template for the Encoder might be helpful:

Suggested change
private Encoder $builderEncoder;
/** @psalm-var Encoder<array|stdClass|Document|PackedArray, mixed> */
private readonly Encoder $builderEncoder;

Copy link
Member Author

Choose a reason for hiding this comment

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

I had to check the Encoder interface template types. Seems correct.

src/Collection.php Outdated Show resolved Hide resolved
src/Database.php Outdated Show resolved Hide resolved
@@ -209,23 +208,28 @@ protected function getCollectionName(): string
*/
protected function getInvalidArrayValues(bool $includeNull = false): array
{
return array_merge([123, 3.14, 'foo', true, new stdClass()], $includeNull ? [null] : []);
return [123, 3.14, 'foo', true, new stdClass(), ...($includeNull ? [null] : [])];
Copy link
Member

Choose a reason for hiding this comment

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

I feel bad for forgetting PHP has this feature now 😅

@GromNaN GromNaN enabled auto-merge (squash) September 10, 2024 15:08
src/Client.php Dismissed Show dismissed Hide dismissed
src/Collection.php Dismissed Show dismissed Hide dismissed
src/Database.php Dismissed Show dismissed Hide dismissed
@GromNaN GromNaN merged commit be2df01 into mongodb:master Sep 10, 2024
31 checks passed
@GromNaN GromNaN deleted the PHPLIB-1505 branch September 11, 2024 06:39
alcaeus added a commit that referenced this pull request Sep 19, 2024
* v1.x: (87 commits)
  Performance: Keep collections and indexes between GridFS tests (#1421)
  Add final annotations to non-internal Operation classes (#1410)
  Fix types accepted by $round (#1401)
  Replace arrayHasKey with assertArrayHasKey in tests (#1403)
  PHPLIB-1514 Make data providers static (#1404)
  PHPLIB-1515 Replace assertObjectHasAttribute with assertObjectHasProperty (#1405)
  Restore Prose22_RangeExplicitEncryptionTest (#1400)
  Remove Prose22_RangeExplicitEncryptionTest that requires ext-mongodb < 1.20 (#1394)
  Use `match` instead of `switch` when a simple value is returned (#1393)
  Remove PHPUnit functions polyfill (#1395)
  Update branch names for GHA workflows (#1390)
  PHPLIB-1419 Encode Agg builder objects in Collection methods (#1383)
  PHPLIB-1420 Integrate query builder for non-aggregation APIs (#1385)
  Fix optional parameter declared before required parameter (#1384)
  PHPLIB-1505 Add driver option "builderEncoder" (#1382)
  Exclude rector.php from the artifact
  Check generated files are up-to-date
  Replace composer package mongodb/builder
  Skip Pedentry method sort for generated files
  Remove composer constraints already imposed by the main package
  ...
alcaeus added a commit that referenced this pull request Sep 25, 2024
* v1.x: (90 commits)
  Merge v1.20 into v1.x (#1447)
  PHPLIB-1525 Removes dependency to Symfony PHPUnit bridge (#1413)
  Change deprecated assertObjectHasAttribute to assertObjectHasProperty (#1432)
  Performance: Keep collections and indexes between GridFS tests (#1421)
  Add final annotations to non-internal Operation classes (#1410)
  Fix types accepted by $round (#1401)
  Replace arrayHasKey with assertArrayHasKey in tests (#1403)
  PHPLIB-1514 Make data providers static (#1404)
  PHPLIB-1515 Replace assertObjectHasAttribute with assertObjectHasProperty (#1405)
  Restore Prose22_RangeExplicitEncryptionTest (#1400)
  Remove Prose22_RangeExplicitEncryptionTest that requires ext-mongodb < 1.20 (#1394)
  Use `match` instead of `switch` when a simple value is returned (#1393)
  Remove PHPUnit functions polyfill (#1395)
  Update branch names for GHA workflows (#1390)
  PHPLIB-1419 Encode Agg builder objects in Collection methods (#1383)
  PHPLIB-1420 Integrate query builder for non-aggregation APIs (#1385)
  Fix optional parameter declared before required parameter (#1384)
  PHPLIB-1505 Add driver option "builderEncoder" (#1382)
  Exclude rector.php from the artifact
  Check generated files are up-to-date
  ...
alcaeus added a commit that referenced this pull request Sep 25, 2024
* v1.x: (90 commits)
  Merge v1.20 into v1.x (#1447)
  PHPLIB-1525 Removes dependency to Symfony PHPUnit bridge (#1413)
  Change deprecated assertObjectHasAttribute to assertObjectHasProperty (#1432)
  Performance: Keep collections and indexes between GridFS tests (#1421)
  Add final annotations to non-internal Operation classes (#1410)
  Fix types accepted by $round (#1401)
  Replace arrayHasKey with assertArrayHasKey in tests (#1403)
  PHPLIB-1514 Make data providers static (#1404)
  PHPLIB-1515 Replace assertObjectHasAttribute with assertObjectHasProperty (#1405)
  Restore Prose22_RangeExplicitEncryptionTest (#1400)
  Remove Prose22_RangeExplicitEncryptionTest that requires ext-mongodb < 1.20 (#1394)
  Use `match` instead of `switch` when a simple value is returned (#1393)
  Remove PHPUnit functions polyfill (#1395)
  Update branch names for GHA workflows (#1390)
  PHPLIB-1419 Encode Agg builder objects in Collection methods (#1383)
  PHPLIB-1420 Integrate query builder for non-aggregation APIs (#1385)
  Fix optional parameter declared before required parameter (#1384)
  PHPLIB-1505 Add driver option "builderEncoder" (#1382)
  Exclude rector.php from the artifact
  Check generated files are up-to-date
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants