Skip to content

Commit de2d92d

Browse files
committed
Deprecate not overriding Type::closureToPHP() so that we can change the implementation in 3.0
1 parent 65e4f53 commit de2d92d

File tree

9 files changed

+62
-0
lines changed

9 files changed

+62
-0
lines changed

UPGRADE-2.16.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,16 @@ be supported in MongoDB ODM 3.0.
1414
Calling `Doctrine\ODM\MongoDB\Configuration::setProxyDir()` or
1515
`Doctrine\ODM\MongoDB\Configuration::getProxyDir()` is deprecated and triggers
1616
a deprecation notice when using native lazy objects.
17+
18+
## Override `Type::closureToPHP()` for custom type classes
19+
20+
The default implementation of `Doctrine\ODM\MongoDB\Types\Type::closureToPHP()`
21+
will change in MongoDB ODM 3.0 to call `convertToPHPValue()`. If you have custom
22+
type classes, use the `Doctrine\ODM\MongoDB\Types\ClosureToPHP` trait or
23+
implement `closureToPHP()`.
24+
25+
## Deprecate `Type::closureToMongo()`
26+
27+
The method `Doctrine\ODM\MongoDB\Types\Type::closureToMongo()` is not used,
28+
and will be removed in MongoDB ODM 3.0. Don't call this method, but use
29+
`convertToDatabaseValue()` instead.

src/Types/ClosureToPHP.php

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

77
use function sprintf;
88

9+
/** This trait will be deprecated in 3.0 as this behavior will be used by default */
910
trait ClosureToPHP
1011
{
1112
/** @return string Redirects to the method convertToPHPValue from child class */

src/Types/CollectionType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@ public function convertToPHPValue($value)
2929
{
3030
return $value !== null ? array_values($value) : null;
3131
}
32+
33+
public function closureToPHP(): string
34+
{
35+
return '$return = array_values($value);';
36+
}
3237
}

src/Types/HashType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ public function convertToPHPValue($value)
2828
{
2929
return $value !== null ? (array) $value : null;
3030
}
31+
32+
public function closureToPHP(): string
33+
{
34+
return '$return = (array) $value;';
35+
}
3136
}

src/Types/KeyType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
class KeyType extends Type
1414
{
15+
use ClosureToPHP;
16+
1517
/** @return MinKey|MaxKey|null */
1618
public function convertToDatabaseValue($value)
1719
{

src/Types/TimestampType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515
class TimestampType extends Type
1616
{
17+
use ClosureToPHP;
18+
1719
/** @return Timestamp|null */
1820
public function convertToDatabaseValue($value)
1921
{

src/Types/Type.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use function gettype;
1616
use function is_object;
1717
use function str_replace;
18+
use function trigger_deprecation;
1819

1920
/**
2021
* The Type interface.
@@ -129,18 +130,27 @@ public function convertToPHPValue($value)
129130
/**
130131
* Get the PHP code equivalent to {@see convertToDatabaseValue()}, used in code generator.
131132
* Use variables $value for input and $return for output.
133+
*
134+
* @deprecated Since 2.16, will be removed in 3.0.
132135
*/
133136
public function closureToMongo(): string
134137
{
138+
trigger_deprecation('doctrine/mongodb-odm', '2.16', 'Type::closureToMongo() is deprecated and will be removed in 3.0.');
139+
135140
return '$return = $value;';
136141
}
137142

138143
/**
139144
* Get the PHP code equivalent to {@see convertToPHPValue()}, used in code generator.
140145
* Use variables $value for input and $return for output.
146+
*
147+
* @abstract The default implementation will change in 3.0.
141148
*/
142149
public function closureToPHP(): string
143150
{
151+
trigger_deprecation('doctrine/mongodb-odm', '2.16', 'The method Type::closureToPHP() will change its default implementation in 3.0 to use convertToPHPValue(). Override this method if you need custom behavior before upgrading to 3.0 or use the trait ClosureToPHP to get the upcoming behavior now.');
152+
// return sprintf('$return = \%s::getType($typeIdentifier)->convertToPHPValue($value);', Type::class);
153+
144154
return '$return = $value;';
145155
}
146156

tests/Tests/Functional/CustomTypeTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Doctrine\ODM\MongoDB\Types\Type;
1212
use Exception;
1313
use PHPUnit\Framework\Attributes\After;
14+
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
1415
use ReflectionProperty;
1516

1617
use function array_map;
@@ -26,6 +27,7 @@ public function setUp(): void
2627

2728
Type::addType('date_collection', DateCollectionType::class);
2829
Type::addType(Language::class, LanguageType::class);
30+
Type::addType('custom_type_without_closure_to_php', CustomTypeWithoutClosureToPHP::class);
2931
}
3032

3133
#[After]
@@ -89,6 +91,14 @@ public function testTypeFromPHPVariable(): void
8991
$databaseValue = Type::convertPHPToDatabaseValue($lang);
9092
self::assertSame(['name' => 'French', 'code' => 'fr'], $databaseValue);
9193
}
94+
95+
#[IgnoreDeprecations]
96+
public function testNotOverridingClosureToPHPIsDeprecated(): void
97+
{
98+
$type = Type::getType('custom_type_without_closure_to_php');
99+
100+
self::assertSame('$return = $value;', $type->closureToPHP());
101+
}
92102
}
93103

94104
class DateCollectionType extends Type
@@ -198,3 +208,7 @@ public function convertToPHPValue($value): ?Language
198208
return new Language($value['name'], $value['code']);
199209
}
200210
}
211+
212+
class CustomTypeWithoutClosureToPHP extends Type
213+
{
214+
}

tests/Tests/Types/TypeTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ public function testConversion(string $typeName, mixed $phpValue, mixed $bsonVal
4242
self::assertSameTypeAndValue($bsonValue, $type->convertToDatabaseValue($phpValue));
4343
}
4444

45+
#[DataProvider('provideTypes')]
46+
public function testConversionWithClosureToPHP(string $typeIdentifier, mixed $expectedValue, mixed $value = null): void
47+
{
48+
$value ??= $expectedValue;
49+
$return = $this;
50+
eval(Type::getType($typeIdentifier)->closureToPHP());
51+
52+
self::assertSameTypeAndValue($expectedValue, $return);
53+
}
54+
4555
public static function provideTypes(): array
4656
{
4757
return [

0 commit comments

Comments
 (0)