@@ -52,41 +52,55 @@ public static function detectType(string $type): string
5252 public function __invoke (array $ row , ResultSet $ resultSet ): array
5353 {
5454 foreach ($ resultSet ->getColumnTypes () as $ key => $ type ) {
55- $ value = $ row [$ key ];
56- if ( $ value === null || $ value === false || $ type === IStructure:: FIELD_TEXT ) {
57- // do nothing
58- } elseif ( $ type === IStructure:: FIELD_FLOAT || $ type === IStructure:: FIELD_DECIMAL ) {
59- $ row [ $ key ] = is_float ( $ tmp = $ value * 1 ) ? $ value : $ tmp ;
55+ $ row [ $ key ] = $ this -> normalizeField ( $ row [$ key ], $ type ) ;
56+ }
57+
58+ return $ row ;
59+ }
6060
61- } elseif ($ type === IStructure::FIELD_FLOAT ) {
61+
62+ private function normalizeField (mixed $ value , string $ type ): mixed
63+ {
64+ if ($ value === null || $ value === false ) {
65+ return $ value ;
66+ }
67+
68+ switch ($ type ) {
69+ case Type::Integer:
70+ return is_float ($ tmp = $ value * 1 ) ? $ value : $ tmp ;
71+
72+ case Type::Float:
73+ case Type::Decimal:
6274 if (is_string ($ value ) && ($ pos = strpos ($ value , '. ' )) !== false ) {
6375 $ value = rtrim (rtrim ($ pos === 0 ? "0 $ value " : $ value , '0 ' ), '. ' );
6476 }
6577
66- $ row [ $ key ] = (float ) $ value ;
78+ return (float ) $ value ;
6779
68- } elseif ( $ type === IStructure:: FIELD_BOOL ) {
69- $ row [ $ key ] = $ value && $ value !== 'f ' && $ value !== 'F ' ;
80+ case Type::Bool:
81+ return $ value && $ value !== 'f ' && $ value !== 'F ' ;
7082
71- } elseif ($ type === IStructure::FIELD_DATETIME || $ type === IStructure::FIELD_DATE ) {
72- $ row [$ key ] = str_starts_with ($ value , '0000-00 ' )
83+ case Type::DateTime:
84+ case Type::Date:
85+ return str_starts_with ($ value , '0000-00 ' )
7386 ? null
7487 : new DateTime ($ value );
7588
76- } elseif ( $ type === IStructure:: FIELD_TIME ) {
77- $ row [ $ key ] = (new DateTime ($ value ))->setDate (1 , 1 , 1 );
89+ case Type::Time:
90+ return (new DateTime ($ value ))->setDate (1 , 1 , 1 );
7891
79- } elseif ( $ type === IStructure:: FIELD_TIME_INTERVAL ) {
92+ case Type::TimeInterval:
8093 preg_match ('#^(-?)(\d+)\D(\d+)\D(\d+)(\.\d+)?$#D ' , $ value , $ m );
81- $ row [$ key ] = new \DateInterval ("PT $ m [2 ]H $ m [3 ]M $ m [4 ]S " );
82- $ row [$ key ]->f = isset ($ m [5 ]) ? (float ) $ m [5 ] : 0.0 ;
83- $ row [$ key ]->invert = (int ) (bool ) $ m [1 ];
94+ $ di = new \DateInterval ("PT $ m [2 ]H $ m [3 ]M $ m [4 ]S " );
95+ $ di ->f = isset ($ m [5 ]) ? (float ) $ m [5 ] : 0.0 ;
96+ $ di ->invert = (int ) (bool ) $ m [1 ];
97+ return $ di ;
8498
85- } elseif ($ type === IStructure::FIELD_UNIX_TIMESTAMP ) {
86- $ row [$ key ] = (new DateTime )->setTimestamp ($ value );
87- }
88- }
99+ case Type::UnixTimestamp:
100+ return (new DateTime )->setTimestamp ($ value );
89101
90- return $ row ;
102+ default :
103+ return $ value ;
104+ }
91105 }
92106}
0 commit comments