Skip to content

Minor update to the Mutator so that it doesn't try to update false values #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/PHPExif/Hydrator/Mutator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Mutator implements HydratorInterface
public function hydrate($object, array $data)
{
foreach ($data as $property => $value) {

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

if (method_exists($object, $mutator)) {
Expand Down
6 changes: 5 additions & 1 deletion lib/PHPExif/Mapper/Exiftool.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ public function mapRawData(array $data)
$value = sprintf('%1$sm', $value);
break;
case self::CREATEDATE:
$value = DateTime::createFromFormat('Y:m:d H:i:s', $value);
try {
$value = new DateTime($value);
} catch (\Exception $exception) {
continue 2;
}
break;
case self::EXPOSURETIME:
$value = '1/' . round(1 / $value);
Expand Down
7 changes: 6 additions & 1 deletion lib/PHPExif/Mapper/Native.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPExif\Exif;
use DateTime;
use Exception;

/**
* PHP Exif Native Mapper
Expand Down Expand Up @@ -142,7 +143,11 @@ public function mapRawData(array $data)
// manipulate the value if necessary
switch ($field) {
case self::DATETIMEORIGINAL:
$value = DateTime::createFromFormat('Y:m:d H:i:s', $value);
try {
$value = new DateTime($value);
} catch (Exception $exception) {
continue 2;
}
break;
case self::EXPOSURETIME:
// normalize ExposureTime
Expand Down
35 changes: 35 additions & 0 deletions tests/PHPExif/Mapper/ExiftoolMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,39 @@ public function testSetNumericInProperty()

$this->assertEquals($expected, $reflProperty->getValue($this->mapper));
}

public function testMapRawDataCorrectlyFormatsDifferentDateTimeString()
{
$rawData = array(
\PHPExif\Mapper\Exiftool::CREATEDATE => '2014-12-15 00:12:00'
);

$mapped = $this->mapper->mapRawData(
$rawData
);

$result = reset($mapped);
$this->assertInstanceOf('\DateTime', $result);
$this->assertEquals(
reset($rawData),
$result->format("Y-m-d H:i:s")
);
}

public function testMapRawDataCorrectlyIgnoresInvalidCreateDate()
{
$rawData = array(
\PHPExif\Mapper\Exiftool::CREATEDATE => 'Invalid Date String'
);

$result = $this->mapper->mapRawData(
$rawData
);

$this->assertCount(0, $result);
$this->assertNotEquals(
reset($rawData),
$result
);
}
}
35 changes: 35 additions & 0 deletions tests/PHPExif/Mapper/NativeMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,39 @@ public function testMapRawDataCorrectlyFormatsGPSData()
$expected = '40.333452380952,-20.167314814815';
$this->assertEquals($expected, reset($result));
}

public function testMapRawDataCorrectlyFormatsDifferentDateTimeString()
{
$rawData = array(
\PHPExif\Mapper\Native::DATETIMEORIGINAL => '2014-12-15 00:12:00'
);

$mapped = $this->mapper->mapRawData(
$rawData
);

$result = reset($mapped);
$this->assertInstanceOf('\DateTime', $result);
$this->assertEquals(
reset($rawData),
$result->format("Y-m-d H:i:s")
);
}

public function testMapRawDataCorrectlyIgnoresInvalidCreateDate()
{
$rawData = array(
\PHPExif\Mapper\Native::DATETIMEORIGINAL => 'Invalid Date String'
);

$result = $this->mapper->mapRawData(
$rawData
);

$this->assertCount(0, $result);
$this->assertNotEquals(
reset($rawData),
$result
);
}
}