Skip to content

Commit e3edd22

Browse files
author
Scott Pringle
committed
Updated Unit Tests to check for more date strings/invalid date strings when mapping the data, also updated the mapRawData method to ignore false values
1 parent 3648311 commit e3edd22

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

lib/PHPExif/Hydrator/Mutator.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ class Mutator implements HydratorInterface
3333
public function hydrate($object, array $data)
3434
{
3535
foreach ($data as $property => $value) {
36-
if ($value === false) {
37-
continue;
38-
}
3936

4037
$mutator = $this->determineMutator($property);
4138

lib/PHPExif/Mapper/Exiftool.php

Lines changed: 9 additions & 2 deletions
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+
$value = false;
143+
}
140144
break;
141145
case self::EXPOSURETIME:
142146
$value = '1/' . round(1 / $value);
@@ -154,7 +158,10 @@ public function mapRawData(array $data)
154158
}
155159

156160
// set end result
157-
$mappedData[$key] = $value;
161+
if ($value !== false) {
162+
// Only map data when it does not equal a false value
163+
$mappedData[$key] = $value;
164+
}
158165
}
159166

160167
// add GPS coordinates, if available

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 = [
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 = [
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
}

0 commit comments

Comments
 (0)