Skip to content

Add Type::getBSONType() #2786

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

Open
wants to merge 1 commit into
base: 2.12.x
Choose a base branch
from
Open

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Jun 26, 2025

Q A
Type feature
BC Break no
Fixed issues PHPLIB-1687

Summary

While working on encryptedFieldsMap (#2785 (comment)), we found that it would be useful if every ODM Type could indicate its BSON type.

It's possible to assign 1 BSON type for most of the ODM types, but there are some exception with some ODM types that can return multiple BSON types (see comments).

Comment on lines +95 to +105
/**
* Returns the alias name of the BSON type.
*
* @link https://www.mongodb.com/docs/manual/reference/bson-types/
*/
public function getBSONType(): BsonType
{
// This method will be abstract in the next major version.
throw new BadMethodCallException(sprintf('The method "%s::getBSONType" is not implemented. You must implement this method in the concrete type class.', static::class));
}

Copy link
Member Author

Choose a reason for hiding this comment

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

We cannot make this method abstract in a minor version, that would be a breaking change for custom type classes that extend this class.

/**
* Raw data type.
*/
class RawType extends Type
{
public function getBSONType(): BsonType
{
throw new LogicException(sprintf('Cannot determine BSON type for "%s".', self::class));
Copy link
Member Author

Choose a reason for hiding this comment

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

The field type can be anything.

/**
* The Key type.
*/
class KeyType extends Type
{
public function getBSONType(): BsonType
{
throw new LogicException(sprintf('Cannot determine BSON type for "%s".', self::class));
Copy link
Member Author

Choose a reason for hiding this comment

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

The field type can be maxKey or minKey.

@@ -11,6 +11,11 @@
*/
class IntType extends Type implements Incrementable, Versionable
{
public function getBSONType(): BsonType
{
return BsonType::Int32;
Copy link
Member Author

Choose a reason for hiding this comment

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

Depending on the system and the value, I think the type can be an int 32 bits or 64 bits.

/**
* The Id type.
*/
class CustomIdType extends Type
{
public function getBSONType(): BsonType
{
throw new LogicException(sprintf('Cannot determine BSON type for "%s".', self::class));
Copy link
Member Author

Choose a reason for hiding this comment

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

The field type can be anything.

@GromNaN GromNaN marked this pull request as ready for review June 26, 2025 12:45
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.

1 participant