Skip to content

Commit 4604eb0

Browse files
committed
Merge pull request #40 from Luciam91/master
Minor update to the Mutator so that it doesn't try to update false values
2 parents 98a582d + c61c855 commit 4604eb0

File tree

5 files changed

+82
-2
lines changed

5 files changed

+82
-2
lines changed

lib/PHPExif/Hydrator/Mutator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Mutator implements HydratorInterface
3333
public function hydrate($object, array $data)
3434
{
3535
foreach ($data as $property => $value) {
36+
3637
$mutator = $this->determineMutator($property);
3738

3839
if (method_exists($object, $mutator)) {

lib/PHPExif/Mapper/Exiftool.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,11 @@ public function mapRawData(array $data)
136136
$value = sprintf('%1$sm', $value);
137137
break;
138138
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+
}
140144
break;
141145
case self::EXPOSURETIME:
142146
$value = '1/' . round(1 / $value);

lib/PHPExif/Mapper/Native.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPExif\Exif;
1515
use DateTime;
16+
use Exception;
1617

1718
/**
1819
* PHP Exif Native Mapper
@@ -142,7 +143,11 @@ public function mapRawData(array $data)
142143
// manipulate the value if necessary
143144
switch ($field) {
144145
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+
}
146151
break;
147152
case self::EXPOSURETIME:
148153
// normalize ExposureTime

tests/PHPExif/Mapper/ExiftoolMapperTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,39 @@ public function testSetNumericInProperty()
236236

237237
$this->assertEquals($expected, $reflProperty->getValue($this->mapper));
238238
}
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+
}
239274
}

tests/PHPExif/Mapper/NativeMapperTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,39 @@ public function testMapRawDataCorrectlyFormatsGPSData()
187187
$expected = '40.333452380952,-20.167314814815';
188188
$this->assertEquals($expected, reset($result));
189189
}
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+
}
190225
}

0 commit comments

Comments
 (0)