File tree Expand file tree Collapse file tree 5 files changed +82
-2
lines changed Expand file tree Collapse file tree 5 files changed +82
-2
lines changed Original file line number Diff line number Diff line change @@ -33,6 +33,7 @@ class Mutator implements HydratorInterface
33
33
public function hydrate ($ object , array $ data )
34
34
{
35
35
foreach ($ data as $ property => $ value ) {
36
+
36
37
$ mutator = $ this ->determineMutator ($ property );
37
38
38
39
if (method_exists ($ object , $ mutator )) {
Original file line number Diff line number Diff line change @@ -136,7 +136,11 @@ public function mapRawData(array $data)
136
136
$ value = sprintf ('%1$sm ' , $ value );
137
137
break ;
138
138
case self ::CREATEDATE :
139
- $ value = DateTime::createFromFormat ('Y:m:d H:i:s ' , $ value );
139
+ try {
140
+ $ value = new DateTime ($ value );
141
+ } catch (\Exception $ exception ) {
142
+ continue 2 ;
143
+ }
140
144
break ;
141
145
case self ::EXPOSURETIME :
142
146
$ value = '1/ ' . round (1 / $ value );
Original file line number Diff line number Diff line change 13
13
14
14
use PHPExif \Exif ;
15
15
use DateTime ;
16
+ use Exception ;
16
17
17
18
/**
18
19
* PHP Exif Native Mapper
@@ -142,7 +143,11 @@ public function mapRawData(array $data)
142
143
// manipulate the value if necessary
143
144
switch ($ field ) {
144
145
case self ::DATETIMEORIGINAL :
145
- $ value = DateTime::createFromFormat ('Y:m:d H:i:s ' , $ value );
146
+ try {
147
+ $ value = new DateTime ($ value );
148
+ } catch (Exception $ exception ) {
149
+ continue 2 ;
150
+ }
146
151
break ;
147
152
case self ::EXPOSURETIME :
148
153
// normalize ExposureTime
Original file line number Diff line number Diff line change @@ -236,4 +236,39 @@ public function testSetNumericInProperty()
236
236
237
237
$ this ->assertEquals ($ expected , $ reflProperty ->getValue ($ this ->mapper ));
238
238
}
239
+
240
+ public function testMapRawDataCorrectlyFormatsDifferentDateTimeString ()
241
+ {
242
+ $ rawData = array (
243
+ \PHPExif \Mapper \Exiftool::CREATEDATE => '2014-12-15 00:12:00 '
244
+ );
245
+
246
+ $ mapped = $ this ->mapper ->mapRawData (
247
+ $ rawData
248
+ );
249
+
250
+ $ result = reset ($ mapped );
251
+ $ this ->assertInstanceOf ('\DateTime ' , $ result );
252
+ $ this ->assertEquals (
253
+ reset ($ rawData ),
254
+ $ result ->format ("Y-m-d H:i:s " )
255
+ );
256
+ }
257
+
258
+ public function testMapRawDataCorrectlyIgnoresInvalidCreateDate ()
259
+ {
260
+ $ rawData = array (
261
+ \PHPExif \Mapper \Exiftool::CREATEDATE => 'Invalid Date String '
262
+ );
263
+
264
+ $ result = $ this ->mapper ->mapRawData (
265
+ $ rawData
266
+ );
267
+
268
+ $ this ->assertCount (0 , $ result );
269
+ $ this ->assertNotEquals (
270
+ reset ($ rawData ),
271
+ $ result
272
+ );
273
+ }
239
274
}
Original file line number Diff line number Diff line change @@ -187,4 +187,39 @@ public function testMapRawDataCorrectlyFormatsGPSData()
187
187
$ expected = '40.333452380952,-20.167314814815 ' ;
188
188
$ this ->assertEquals ($ expected , reset ($ result ));
189
189
}
190
+
191
+ public function testMapRawDataCorrectlyFormatsDifferentDateTimeString ()
192
+ {
193
+ $ rawData = array (
194
+ \PHPExif \Mapper \Native::DATETIMEORIGINAL => '2014-12-15 00:12:00 '
195
+ );
196
+
197
+ $ mapped = $ this ->mapper ->mapRawData (
198
+ $ rawData
199
+ );
200
+
201
+ $ result = reset ($ mapped );
202
+ $ this ->assertInstanceOf ('\DateTime ' , $ result );
203
+ $ this ->assertEquals (
204
+ reset ($ rawData ),
205
+ $ result ->format ("Y-m-d H:i:s " )
206
+ );
207
+ }
208
+
209
+ public function testMapRawDataCorrectlyIgnoresInvalidCreateDate ()
210
+ {
211
+ $ rawData = array (
212
+ \PHPExif \Mapper \Native::DATETIMEORIGINAL => 'Invalid Date String '
213
+ );
214
+
215
+ $ result = $ this ->mapper ->mapRawData (
216
+ $ rawData
217
+ );
218
+
219
+ $ this ->assertCount (0 , $ result );
220
+ $ this ->assertNotEquals (
221
+ reset ($ rawData ),
222
+ $ result
223
+ );
224
+ }
190
225
}
You can’t perform that action at this time.
0 commit comments