Skip to content

Commit fb48263

Browse files
committed
Change default implementation of Type::closureToPHP and remove closureToMongo
1 parent 20c9caf commit fb48263

23 files changed

+10
-185
lines changed

src/Types/AbstractVectorType.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,6 @@ public function convertToPHPValue(mixed $value): ?array
7272
return $value->toArray();
7373
}
7474

75-
public function closureToMongo(): string
76-
{
77-
return str_replace('%%vectorType%%', $this->getVectorType()->name, <<<'PHP'
78-
if ($value === null) {
79-
$return = null;
80-
} elseif (\is_array($value)) {
81-
$return = \MongoDB\BSON\Binary::fromVector($value, \MongoDB\BSON\VectorType::%%vectorType%%);
82-
} elseif (! $value instanceof \MongoDB\BSON\Binary) {
83-
throw new InvalidArgumentException(sprintf('Invalid data type %s received for vector field, expected null, array or MongoDB\BSON\Binary', get_debug_type($value)));
84-
} elseif ($value->getType() !== \MongoDB\BSON\Binary::TYPE_VECTOR) {
85-
throw new InvalidArgumentException(sprintf('Invalid binary data of type %d received for vector field, expected binary type %d', $value->getType(), \MongoDB\BSON\Binary::TYPE_VECTOR));
86-
} elseif ($value->getVectorType() !== \MongoDB\BSON\VectorType::%%vectorType%%) {
87-
throw new \InvalidArgumentException(sprintf('Invalid binary vector data of vector type %s received for vector field, expected vector type %%vectorType%%', $value->getVectorType()->name));
88-
} else {
89-
$return = $value;
90-
}
91-
PHP);
92-
}
93-
9475
public function closureToPHP(): string
9576
{
9677
return str_replace('%%vectorType%%', $this->getVectorType()->name, <<<'PHP'

src/Types/BinDataType.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
use MongoDB\BSON\Binary;
88

9-
use function sprintf;
10-
119
/**
1210
* The BinData type for generic data.
1311
*/
@@ -42,11 +40,6 @@ public function convertToPHPValue(mixed $value): mixed
4240
return $value !== null ? ($value instanceof Binary ? $value->getData() : $value) : null;
4341
}
4442

45-
public function closureToMongo(): string
46-
{
47-
return sprintf('$return = $value !== null ? new \MongoDB\BSON\Binary($value, %d) : null;', $this->binDataType);
48-
}
49-
5043
public function closureToPHP(): string
5144
{
5245
return '$return = $value !== null ? ($value instanceof \MongoDB\BSON\Binary ? $value->getData() : $value) : null;';

src/Types/BinaryUuidType.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,6 @@ public function convertToPHPValue(mixed $value): Uuid
4242
return Uuid::fromBinary($value->getData());
4343
}
4444

45-
public function closureToMongo(): string
46-
{
47-
return <<<'PHP'
48-
$return = match (true) {
49-
$value === null => null,
50-
$value instanceof \MongoDB\BSON\Binary => $value,
51-
$value instanceof \Symfony\Component\Uid\Uuid => new \MongoDB\BSON\Binary($value->toBinary(), \MongoDB\BSON\Binary::TYPE_UUID),
52-
is_string($value) => new \MongoDB\BSON\Binary(\Symfony\Component\Uid\Uuid::fromString($value)->toBinary(), \MongoDB\BSON\Binary::TYPE_UUID),
53-
default => throw new \InvalidArgumentException(sprintf('Invalid data type %s received for UUID', get_debug_type($value))),
54-
};
55-
PHP;
56-
}
57-
5845
public function closureToPHP(): string
5946
{
6047
return <<<'PHP'

src/Types/BooleanType.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ public function convertToPHPValue(mixed $value): ?bool
1919
return $value !== null ? (bool) $value : null;
2020
}
2121

22-
public function closureToMongo(): string
23-
{
24-
return '$return = (bool) $value;';
25-
}
26-
2722
public function closureToPHP(): string
2823
{
2924
return '$return = (bool) $value;';

src/Types/ClosureToPHP.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
use function sprintf;
88

9-
/** This trait will be deprecated in 3.0 as this behavior will be used by default */
9+
/**
10+
* @deprecated Since 3.0, the default {@Type::closureToPHP()} implementation is identical and this trait is no longer needed.
11+
*
12+
* @phpstan-ignore trait.unused
13+
*/
1014
trait ClosureToPHP
1115
{
1216
/** @return string Redirects to the method convertToPHPValue from child class */

src/Types/CustomIdType.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ public function convertToPHPValue(mixed $value): mixed
1919
return $value;
2020
}
2121

22-
public function closureToMongo(): string
23-
{
24-
return '$return = $value;';
25-
}
26-
2722
public function closureToPHP(): string
2823
{
2924
return '$return = $value;';

src/Types/DateType.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,6 @@ public function convertToPHPValue(mixed $value): ?DateTimeInterface
101101
return static::getDateTime($value);
102102
}
103103

104-
public function closureToMongo(): string
105-
{
106-
return 'if ($value === null || $value instanceof \MongoDB\BSON\UTCDateTime) { $return = $value; } else { $datetime = \\' . static::class . '::getDateTime($value); $return = new \MongoDB\BSON\UTCDateTime($datetime); }';
107-
}
108-
109104
public function closureToPHP(): string
110105
{
111106
return 'if ($value === null) { $return = null; } else { $return = \\' . static::class . '::getDateTime($value); }';

src/Types/Decimal128Type.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
class Decimal128Type extends Type implements Incrementable, Versionable
1313
{
14-
use ClosureToPHP;
15-
1614
public function convertToDatabaseValue(mixed $value): ?Decimal128
1715
{
1816
if ($value === null) {

src/Types/FloatType.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ public function convertToPHPValue(mixed $value): ?float
1919
return $value !== null ? (float) $value : null;
2020
}
2121

22-
public function closureToMongo(): string
23-
{
24-
return '$return = (float) $value;';
25-
}
26-
2722
public function closureToPHP(): string
2823
{
2924
return '$return = (float) $value;';

src/Types/IdType.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ public function convertToPHPValue(mixed $value): mixed
3434
return $value instanceof ObjectId ? (string) $value : $value;
3535
}
3636

37-
public function closureToMongo(): string
38-
{
39-
return '$return = new MongoDB\BSON\ObjectId($value);';
40-
}
41-
4237
public function closureToPHP(): string
4338
{
4439
return '$return = $value instanceof \MongoDB\BSON\ObjectId ? (string) $value : $value;';

0 commit comments

Comments
 (0)