Skip to content

Commit a5ab4b3

Browse files
author
Tom Van Herreweghe
committedAug 19, 2013
Updated Native adapter for CS tests against PSR-2
1 parent 807bd70 commit a5ab4b3

File tree

2 files changed

+121
-20
lines changed

2 files changed

+121
-20
lines changed
 

‎lib/PHPExif/Reader/Adapter/Native.php

+70-20
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public function getIptcData($file)
206206
{
207207
$size = getimagesize($file, $info);
208208
$arrData = array();
209-
if(isset($info['APP13'])) {
209+
if (isset($info['APP13'])) {
210210
$iptc = iptcparse($info['APP13']);
211211

212212
foreach ($this->iptcMapping as $name => $field) {
@@ -251,28 +251,78 @@ public function mapData(array $source)
251251
$vertResolution = (int)reset($resolutionParts);
252252
}
253253

254-
return array(
255-
Exif::APERTURE => (!isset($source[self::SECTION_COMPUTED]['ApertureFNumber'])) ? false : $source[self::SECTION_COMPUTED]['ApertureFNumber'],
256-
Exif::AUTHOR => (!isset($source['Artist'])) ? false : $source['Artist'],
257-
Exif::CAMERA => (!isset($source['Model'])) ? false : $source['Model'],
258-
Exif::CAPTION => (!isset($source[self::SECTION_IPTC]['caption'])) ? false : $source[self::SECTION_IPTC]['caption'],
259-
Exif::COPYRIGHT => (!isset($source[self::SECTION_IPTC]['copyright'])) ? false : $source[self::SECTION_IPTC]['copyright'],
260-
Exif::CREATION_DATE => (!isset($source['DateTimeOriginal'])) ? false : DateTime::createFromFormat('Y:m:d H:i:s', $source['DateTimeOriginal']),
261-
Exif::CREDIT => (!isset($source[self::SECTION_IPTC]['credit'])) ? false : $source[self::SECTION_IPTC]['credit'],
262-
Exif::EXPOSURE => (!isset($source['ExposureTime'])) ? false : $source['ExposureTime'],
254+
$creationDate = false;
255+
if (isset($source['DateTimeOriginal'])) {
256+
$creationDate = DateTime::createFromFormat(
257+
'Y:m:d H:i:s',
258+
$source['DateTimeOriginal']
259+
);
260+
}
261+
262+
$mappedData = array(
263+
Exif::APERTURE => false,
264+
Exif::AUTHOR => false,
265+
Exif::CAMERA => false,
266+
Exif::CAPTION => false,
267+
Exif::COPYRIGHT => false,
268+
Exif::CREATION_DATE => $creationDate,
269+
Exif::CREDIT => false,
270+
Exif::EXPOSURE => false,
263271
Exif::FOCAL_LENGTH => $focalLength,
264-
Exif::FOCAL_DISTANCE => (!isset($source[self::SECTION_COMPUTED]['FocusDistance'])) ? false : $source[self::SECTION_COMPUTED]['FocusDistance'],
265-
Exif::HEADLINE => (!isset($source[self::SECTION_IPTC]['headline'])) ? false : $source[self::SECTION_IPTC]['headline'],
266-
Exif::HEIGHT => (!isset($source[self::SECTION_COMPUTED]['Height'])) ? false : $source[self::SECTION_COMPUTED]['Height'],
272+
Exif::FOCAL_DISTANCE => false,
273+
Exif::HEADLINE => false,
274+
Exif::HEIGHT => false,
267275
Exif::HORIZONTAL_RESOLUTION => $horResolution,
268-
Exif::ISO => (!isset($source['ISOSpeedRatings'])) ? false : $source['ISOSpeedRatings'],
269-
Exif::JOB_TITLE => (!isset($source[self::SECTION_IPTC]['jobtitle'])) ? false : $source[self::SECTION_IPTC]['jobtitle'],
270-
Exif::KEYWORDS => (!isset($source[self::SECTION_IPTC]['keywords'])) ? false : $source[self::SECTION_IPTC]['keywords'],
271-
Exif::SOFTWARE => (!isset($source['Software'])) ? false : $source['Software'],
272-
Exif::SOURCE => (!isset($source[self::SECTION_IPTC]['source'])) ? false : $source[self::SECTION_IPTC]['source'],
273-
Exif::TITLE => (!isset($source[self::SECTION_IPTC]['title'])) ? false : $source[self::SECTION_IPTC]['title'],
276+
Exif::ISO => false,
277+
Exif::JOB_TITLE => false,
278+
Exif::KEYWORDS => false,
279+
Exif::SOFTWARE => false,
280+
Exif::SOURCE => false,
281+
Exif::TITLE => false,
274282
Exif::VERTICAL_RESOLUTION => $vertResolution,
275-
Exif::WIDTH => (!isset($source[self::SECTION_COMPUTED]['Width'])) ? false : $source[self::SECTION_COMPUTED]['Width'],
283+
Exif::WIDTH => false,
276284
);
285+
286+
$arrMapping = array(
287+
array(
288+
Exif::AUTHOR => 'Artist',
289+
Exif::CAMERA => 'Model',
290+
Exif::EXPOSURE => 'ExposureTime',
291+
Exif::ISO => 'ISOSpeedRatings',
292+
Exif::SOFTWARE => 'Software',
293+
),
294+
self::SECTION_COMPUTED => array(
295+
Exif::APERTURE => 'ApertureFNumber',
296+
Exif::FOCAL_DISTANCE => 'FocusDistance',
297+
Exif::HEIGHT => 'Height',
298+
Exif::WIDTH => 'Width',
299+
),
300+
self::SECTION_IPTC => array(
301+
Exif::CAPTION => 'caption',
302+
Exif::COPYRIGHT => 'copyright',
303+
Exif::CREDIT => 'credit',
304+
Exif::HEADLINE => 'headline',
305+
Exif::JOB_TITLE => 'jobtitle',
306+
Exif::KEYWORDS => 'keywords',
307+
Exif::SOURCE => 'source',
308+
Exif::TITLE => 'title',
309+
),
310+
);
311+
312+
foreach ($arrMapping as $key => $arrFields) {
313+
if (array_key_exists($key, $source)) {
314+
$arrSource = $source[$key];
315+
} else {
316+
$arrSource = $source;
317+
}
318+
319+
foreach ($arrFields as $mappedField => $field) {
320+
if (isset($arrSource[$field])) {
321+
$mappedData[$mappedField] = $arrSource[$field];
322+
}
323+
}
324+
}
325+
326+
return $mappedData;
277327
}
278328
}

‎tests/PHPExif/Reader/Adapter/NativeTest.php

+51
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,42 @@ public function testMapDataReturnsArray()
187187
$this->assertInternalType('array', $this->adapter->mapData(array()));
188188
}
189189

190+
/**
191+
* @group native
192+
* @covers \PHPExif\Reader\Adapter\Native::mapData
193+
*/
194+
public function testMapDataMapsFirstLevel()
195+
{
196+
$result = $this->adapter->mapData(
197+
array(
198+
'Software' => 'Foo',
199+
)
200+
);
201+
$this->assertEquals(
202+
'Foo',
203+
$result[\PHPExif\Exif::SOFTWARE]
204+
);
205+
}
206+
207+
/**
208+
* @group native
209+
* @covers \PHPExif\Reader\Adapter\Native::mapData
210+
*/
211+
public function testMapDataMapsSecondLevel()
212+
{
213+
$result = $this->adapter->mapData(
214+
array(
215+
\PHPExif\Reader\Adapter\Native::SECTION_COMPUTED => array(
216+
'Height' => '1500'
217+
)
218+
)
219+
);
220+
$this->assertEquals(
221+
1500,
222+
$result[\PHPExif\Exif::HEIGHT]
223+
);
224+
}
225+
190226
/**
191227
* @group native
192228
* @covers \PHPExif\Reader\Adapter\Native::mapData
@@ -266,4 +302,19 @@ public function testMapDataVerticalResolutionIsCalculated()
266302

267303
$this->assertEquals(240, $result[\PHPExif\Exif::VERTICAL_RESOLUTION]);
268304
}
305+
306+
/**
307+
* @group native-curr
308+
* @covers \PHPExif\Reader\Adapter\Native::mapData
309+
*/
310+
public function testMapDataCreationDateIsConvertedToDatetime()
311+
{
312+
$result = $this->adapter->mapData(
313+
array(
314+
'DateTimeOriginal' => '2013:06:30 12:34:56',
315+
)
316+
);
317+
318+
$this->assertInstanceOf('DateTime', $result[\PHPExif\Exif::CREATION_DATE]);
319+
}
269320
}

0 commit comments

Comments
 (0)
Please sign in to comment.