55use Doctrine \DBAL \Platforms \AbstractPlatform ;
66use Doctrine \DBAL \Types \ConversionException ;
77use Doctrine \DBAL \Types \FloatType ;
8+ use SoureCode \Bundle \Unit \Model \Distance ;
89use SoureCode \Bundle \Unit \Model \Length \LengthUnitInterface ;
910use SoureCode \Bundle \Unit \Model \Length \Picometer ;
1011use SoureCode \Bundle \Unit \Model \UnitInterface ;
1112
12- class LengthType extends FloatType
13+ class DistanceType extends FloatType
1314{
14- public const string NAME = 'length ' ;
15+ public const string NAME = 'distance ' ;
1516
1617 /**
1718 * @var class-string<LengthUnitInterface>
1819 */
1920 public static string $ databaseUnitClass = Picometer::class;
2021
21- public function convertToPHPValue ($ value , AbstractPlatform $ platform ): ?UnitInterface
22+ public function convertToPHPValue ($ value , AbstractPlatform $ platform ): ?Distance
2223 {
2324 $ value = parent ::convertToPHPValue ($ value , $ platform );
2425
@@ -27,11 +28,11 @@ public function convertToPHPValue($value, AbstractPlatform $platform): ?UnitInte
2728 }
2829
2930 /**
30- * @var UnitInterface $databaseUnit
31+ * @var LengthUnitInterface $databaseUnit
3132 */
32- $ databaseUnit = new self ::$ databaseUnitClass ($ value );
33+ $ databaseUnit = new static ::$ databaseUnitClass ($ value );
3334
34- return $ databaseUnit ->normalize ();
35+ return new Distance ( $ databaseUnit ->normalize () );
3536 }
3637
3738 public function convertToDatabaseValue ($ value , AbstractPlatform $ platform ): ?string
@@ -40,22 +41,25 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str
4041 return null ;
4142 }
4243
43- if (!$ value instanceof UnitInterface ) {
44- throw ConversionException::conversionFailedInvalidType ($ value , $ this -> getName () , [UnitInterface::class]);
44+ if (!$ value instanceof Distance ) {
45+ throw ConversionException::conversionFailedInvalidType ($ value , static :: NAME , [UnitInterface::class]);
4546 }
4647
47- $ databaseUnit = $ value ->convert (self ::$ databaseUnitClass );
48+ /**
49+ * @var LengthUnitInterface $databaseUnit
50+ */
51+ $ databaseUnit = $ value ->getValue ()->convert (static ::$ databaseUnitClass );
4852
4953 return parent ::convertToDatabaseValue ((string ) $ databaseUnit ->getValue (), $ platform );
5054 }
5155
52- public function getName ( ): string
56+ public function requiresSQLCommentHint ( AbstractPlatform $ platform ): bool
5357 {
54- return self :: NAME ;
58+ return true ;
5559 }
5660
57- public function requiresSQLCommentHint ( AbstractPlatform $ platform ): bool
61+ public function getName ( ): string
5862 {
59- return true ;
63+ return static :: NAME ;
6064 }
6165}
0 commit comments