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
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
5 changes: 5 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Types/BinDataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class BinDataType extends Type
*/
protected $binDataType = Binary::TYPE_GENERIC;

public function getBSONType(): BsonType
{
return BsonType::BinaryData;
}

public function convertToDatabaseValue($value)
{
if ($value === null) {
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Types/BooleanType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
*/
class BooleanType extends Type
{
public function getBSONType(): BsonType
{
return BsonType::Boolean;
}

public function convertToDatabaseValue($value)
{
return $value !== null ? (bool) $value : null;
Expand Down
26 changes: 26 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Types/BsonType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Doctrine\ODM\MongoDB\Types;

enum BsonType: string
{
case Double = 'double';
case String = 'string';
case Object = 'object';
case Array = 'array';
case BinaryData = 'binData';
case ObjectId = 'objectId';
case Boolean = 'bool';
case Date = 'date';
case Null = 'null';
case RegularExpression = 'regex';
case JavaScript = 'javascript';
case Int32 = 'int';
case Timestamp = 'timestamp';
case Int64 = 'long';
case Decimal128 = 'decimal';
case MinKey = 'minKey';
case MaxKey = 'maxKey';
}
5 changes: 5 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Types/CollectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
*/
class CollectionType extends Type
{
public function getBSONType(): BsonType
{
return BsonType::Array;
}

public function convertToDatabaseValue($value)
{
if ($value !== null && ! is_array($value)) {
Expand Down
9 changes: 9 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Types/CustomIdType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@

namespace Doctrine\ODM\MongoDB\Types;

use LogicException;

use function sprintf;

/**
* 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.

}

public function convertToDatabaseValue($value)
{
return $value;
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Types/DateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
*/
class DateType extends Type implements Versionable
{
public function getBSONType(): BsonType
{
return BsonType::Date;
}

/**
* Converts a value to a DateTime.
* Supports microseconds
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Types/Decimal128Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class Decimal128Type extends Type implements Incrementable, Versionable
{
use ClosureToPHP;

public function getBSONType(): BsonType
{
return BsonType::Decimal128;
}

public function convertToDatabaseValue($value)
{
if ($value === null) {
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Types/FloatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
*/
class FloatType extends Type implements Incrementable
{
public function getBSONType(): BsonType
{
return BsonType::Double;
}

public function convertToDatabaseValue($value)
{
return $value !== null ? (float) $value : null;
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Types/HashType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
*/
class HashType extends Type
{
public function getBSONType(): BsonType
{
return BsonType::Object;
}

public function convertToDatabaseValue($value)
{
if ($value !== null && ! is_array($value)) {
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Types/IdType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
*/
class IdType extends Type
{
public function getBSONType(): BsonType
{
return BsonType::ObjectId;
}

public function convertToDatabaseValue($value)
{
if ($value === null) {
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Types/IntType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.

}

public function convertToDatabaseValue($value)
{
return $value !== null ? (int) $value : null;
Expand Down
8 changes: 8 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Types/KeyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@

namespace Doctrine\ODM\MongoDB\Types;

use LogicException;
use MongoDB\BSON\MaxKey;
use MongoDB\BSON\MinKey;

use function sprintf;

/**
* 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.

}

public function convertToDatabaseValue($value)
{
if ($value === null) {
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Types/ObjectIdType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
*/
class ObjectIdType extends Type
{
public function getBSONType(): BsonType
{
return BsonType::ObjectId;
}

public function convertToDatabaseValue($value)
{
if ($value === null) {
Expand Down
9 changes: 9 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Types/RawType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@

namespace Doctrine\ODM\MongoDB\Types;

use LogicException;

use function sprintf;

/**
* 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.

}

public function convertToDatabaseValue($value)
{
return $value;
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Types/StringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
*/
class StringType extends Type
{
public function getBSONType(): BsonType
{
return BsonType::String;
}

public function convertToDatabaseValue($value)
{
return $value === null || $value instanceof Regex ? $value : (string) $value;
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Types/TimestampType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
*/
class TimestampType extends Type
{
public function getBSONType(): BsonType
{
return BsonType::Timestamp;
}

public function convertToDatabaseValue($value)
{
if ($value instanceof Timestamp) {
Expand Down
12 changes: 12 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Types/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\ODM\MongoDB\Types;

use BadMethodCallException;
use DateTimeInterface;
use Doctrine\ODM\MongoDB\Mapping\MappingException;
use Doctrine\ODM\MongoDB\Types;
Expand Down Expand Up @@ -91,6 +92,17 @@ final private function __construct()
{
}

/**
* 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));
}

Comment on lines +95 to +105
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.

/**
* Converts a value from its PHP representation to its database representation
* of this type.
Expand Down