-
-
Notifications
You must be signed in to change notification settings - Fork 512
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
base: 2.12.x
Are you sure you want to change the base?
Add Type::getBSONType()
#2786
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,11 @@ | |
*/ | ||
class IntType extends Type implements Incrementable, Versionable | ||
{ | ||
public function getBSONType(): BsonType | ||
{ | ||
return BsonType::Int32; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The field type can be |
||
} | ||
|
||
public function convertToDatabaseValue($value) | ||
{ | ||
if ($value === null) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The field type can be anything. |
||
} | ||
|
||
public function convertToDatabaseValue($value) | ||
{ | ||
return $value; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
namespace Doctrine\ODM\MongoDB\Types; | ||
|
||
use BadMethodCallException; | ||
use DateTimeInterface; | ||
use Doctrine\ODM\MongoDB\Mapping\MappingException; | ||
use Doctrine\ODM\MongoDB\Types; | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
There was a problem hiding this comment.
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.