Skip to content

Commit db49a70

Browse files
committed
Add Type::getBSONType()
1 parent b0909ca commit db49a70

17 files changed

+124
-0
lines changed

lib/Doctrine/ODM/MongoDB/Types/BinDataType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ class BinDataType extends Type
2222
*/
2323
protected $binDataType = Binary::TYPE_GENERIC;
2424

25+
public function getBSONType(): BsonType
26+
{
27+
return BsonType::BinaryData;
28+
}
29+
2530
public function convertToDatabaseValue($value)
2631
{
2732
if ($value === null) {

lib/Doctrine/ODM/MongoDB/Types/BooleanType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
*/
1010
class BooleanType extends Type
1111
{
12+
public function getBSONType(): BsonType
13+
{
14+
return BsonType::Boolean;
15+
}
16+
1217
public function convertToDatabaseValue($value)
1318
{
1419
return $value !== null ? (bool) $value : null;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\ODM\MongoDB\Types;
6+
7+
enum BsonType: string
8+
{
9+
case Double = 'double';
10+
case String = 'string';
11+
case Object = 'object';
12+
case Array = 'array';
13+
case BinaryData = 'binData';
14+
case ObjectId = 'objectId';
15+
case Boolean = 'bool';
16+
case Date = 'date';
17+
case Null = 'null';
18+
case RegularExpression = 'regex';
19+
case JavaScript = 'javascript';
20+
case Int32 = 'int';
21+
case Timestamp = 'timestamp';
22+
case Int64 = 'long';
23+
case Decimal128 = 'decimal';
24+
case MinKey = 'minKey';
25+
case MaxKey = 'maxKey';
26+
}

lib/Doctrine/ODM/MongoDB/Types/CollectionType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
*/
1515
class CollectionType extends Type
1616
{
17+
public function getBSONType(): BsonType
18+
{
19+
return BsonType::Array;
20+
}
21+
1722
public function convertToDatabaseValue($value)
1823
{
1924
if ($value !== null && ! is_array($value)) {

lib/Doctrine/ODM/MongoDB/Types/CustomIdType.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@
44

55
namespace Doctrine\ODM\MongoDB\Types;
66

7+
use LogicException;
8+
9+
use function sprintf;
10+
711
/**
812
* The Id type.
913
*/
1014
class CustomIdType extends Type
1115
{
16+
public function getBSONType(): BsonType
17+
{
18+
throw new LogicException(sprintf('Cannot determine BSON type for "%s".', self::class));
19+
}
20+
1221
public function convertToDatabaseValue($value)
1322
{
1423
return $value;

lib/Doctrine/ODM/MongoDB/Types/DateType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
*/
2929
class DateType extends Type implements Versionable
3030
{
31+
public function getBSONType(): BsonType
32+
{
33+
return BsonType::Date;
34+
}
35+
3136
/**
3237
* Converts a value to a DateTime.
3338
* Supports microseconds

lib/Doctrine/ODM/MongoDB/Types/Decimal128Type.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ class Decimal128Type extends Type implements Incrementable, Versionable
1313
{
1414
use ClosureToPHP;
1515

16+
public function getBSONType(): BsonType
17+
{
18+
return BsonType::Decimal128;
19+
}
20+
1621
public function convertToDatabaseValue($value)
1722
{
1823
if ($value === null) {

lib/Doctrine/ODM/MongoDB/Types/FloatType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
*/
1010
class FloatType extends Type implements Incrementable
1111
{
12+
public function getBSONType(): BsonType
13+
{
14+
return BsonType::Double;
15+
}
16+
1217
public function convertToDatabaseValue($value)
1318
{
1419
return $value !== null ? (float) $value : null;

lib/Doctrine/ODM/MongoDB/Types/HashType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
*/
1414
class HashType extends Type
1515
{
16+
public function getBSONType(): BsonType
17+
{
18+
return BsonType::Object;
19+
}
20+
1621
public function convertToDatabaseValue($value)
1722
{
1823
if ($value !== null && ! is_array($value)) {

lib/Doctrine/ODM/MongoDB/Types/IdType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
*/
1313
class IdType extends Type
1414
{
15+
public function getBSONType(): BsonType
16+
{
17+
return BsonType::ObjectId;
18+
}
19+
1520
public function convertToDatabaseValue($value)
1621
{
1722
if ($value === null) {

lib/Doctrine/ODM/MongoDB/Types/IntType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
*/
1212
class IntType extends Type implements Incrementable, Versionable
1313
{
14+
public function getBSONType(): BsonType
15+
{
16+
return BsonType::Int32;
17+
}
18+
1419
public function convertToDatabaseValue($value)
1520
{
1621
return $value !== null ? (int) $value : null;

lib/Doctrine/ODM/MongoDB/Types/KeyType.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,22 @@
44

55
namespace Doctrine\ODM\MongoDB\Types;
66

7+
use LogicException;
78
use MongoDB\BSON\MaxKey;
89
use MongoDB\BSON\MinKey;
910

11+
use function sprintf;
12+
1013
/**
1114
* The Key type.
1215
*/
1316
class KeyType extends Type
1417
{
18+
public function getBSONType(): BsonType
19+
{
20+
throw new LogicException(sprintf('Cannot determine BSON type for "%s".', self::class));
21+
}
22+
1523
public function convertToDatabaseValue($value)
1624
{
1725
if ($value === null) {

lib/Doctrine/ODM/MongoDB/Types/ObjectIdType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
*/
1212
class ObjectIdType extends Type
1313
{
14+
public function getBSONType(): BsonType
15+
{
16+
return BsonType::ObjectId;
17+
}
18+
1419
public function convertToDatabaseValue($value)
1520
{
1621
if ($value === null) {

lib/Doctrine/ODM/MongoDB/Types/RawType.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@
44

55
namespace Doctrine\ODM\MongoDB\Types;
66

7+
use LogicException;
8+
9+
use function sprintf;
10+
711
/**
812
* Raw data type.
913
*/
1014
class RawType extends Type
1115
{
16+
public function getBSONType(): BsonType
17+
{
18+
throw new LogicException(sprintf('Cannot determine BSON type for "%s".', self::class));
19+
}
20+
1221
public function convertToDatabaseValue($value)
1322
{
1423
return $value;

lib/Doctrine/ODM/MongoDB/Types/StringType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
*/
1212
class StringType extends Type
1313
{
14+
public function getBSONType(): BsonType
15+
{
16+
return BsonType::String;
17+
}
18+
1419
public function convertToDatabaseValue($value)
1520
{
1621
return $value === null || $value instanceof Regex ? $value : (string) $value;

lib/Doctrine/ODM/MongoDB/Types/TimestampType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
*/
1515
class TimestampType extends Type
1616
{
17+
public function getBSONType(): BsonType
18+
{
19+
return BsonType::Timestamp;
20+
}
21+
1722
public function convertToDatabaseValue($value)
1823
{
1924
if ($value instanceof Timestamp) {

lib/Doctrine/ODM/MongoDB/Types/Type.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Doctrine\ODM\MongoDB\Types;
66

7+
use BadMethodCallException;
78
use DateTimeInterface;
89
use Doctrine\ODM\MongoDB\Mapping\MappingException;
910
use Doctrine\ODM\MongoDB\Types;
@@ -91,6 +92,17 @@ final private function __construct()
9192
{
9293
}
9394

95+
/**
96+
* Returns the alias name of the BSON type.
97+
*
98+
* @link https://www.mongodb.com/docs/manual/reference/bson-types/
99+
*/
100+
public function getBSONType(): BsonType
101+
{
102+
// This method will be abstract in the next major version.
103+
throw new BadMethodCallException(sprintf('The method "%s::getBSONType" is not implemented. You must implement this method in the concrete type class.', static::class));
104+
}
105+
94106
/**
95107
* Converts a value from its PHP representation to its database representation
96108
* of this type.

0 commit comments

Comments
 (0)