Skip to content

Issue/26 lowercase input keys #27

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 2 commits into from
Dec 30, 2016
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
28 changes: 28 additions & 0 deletions src/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ public function getMetadataFromFile($filePath)

$this->augmentDataWithIptcRawData($filePath, $data);

$data = $this->normalizeArrayKeys($data);

// map the data:
$mapper = $this->getMapper();
$metadata = new Metadata(
Expand All @@ -120,6 +122,32 @@ public function getMetadataFromFile($filePath)
return $metadata;
}

/**
* Lowercases the keys for given array
*
* @param array $data
*
* @return array
*/
private function normalizeArrayKeys(array $data)
{
$keys = array_keys($data);
$keys = array_map('strtolower', $keys);
$values = array_values($data);
$values = array_map(function ($value) {
if (!is_array($value)) {
return $value;
}

return $this->normalizeArrayKeys($value);
}, $values);

return array_combine(
$keys,
$values
);
}

/**
* Adds data from iptcparse to the original raw EXIF data
*
Expand Down
6 changes: 3 additions & 3 deletions src/Reader/Mapper/Exif/ApertureFieldMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ public function mapField($field, array $input, &$output)
{
$this->guardInvalidArguments($field, $input, $output);

if (!array_key_exists('COMPUTED', $input)
|| !array_key_exists('ApertureFNumber', $input['COMPUTED'])) {
if (!array_key_exists('computed', $input)
|| !array_key_exists('aperturefnumber', $input['computed'])) {
return;
}

$aperture = Aperture::fromFocalLength(
$input['COMPUTED']['ApertureFNumber']
$input['computed']['aperturefnumber']
);

$output = $output->withAperture($aperture);
Expand Down
10 changes: 5 additions & 5 deletions src/Reader/Mapper/Exif/CoordinatesFieldMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ public function mapField($field, array $input, &$output)
{
$this->guardInvalidArguments($field, $input, $output);

if (!(array_key_exists('GPSLatitude', $input) && array_key_exists('GPSLongitude', $input))) {
if (!(array_key_exists('gpslatitude', $input) && array_key_exists('gpslongitude', $input))) {
return;
}

$latitude = $this->extractGpsCoordinate($input['GPSLatitude']);
$longitude = $this->extractGpsCoordinate($input['GPSLongitude']);
$latitudeRef = empty($input['GPSLatitudeRef'][0]) ? 'N' : $input['GPSLatitudeRef'][0];
$longitudeRef = empty($input['GPSLongitudeRef'][0]) ? 'E' : $input['GPSLongitudeRef'][0];
$latitude = $this->extractGpsCoordinate($input['gpslatitude']);
$longitude = $this->extractGpsCoordinate($input['gpslongitude']);
$latitudeRef = empty($input['gpslatituderef'][0]) ? 'N' : $input['gpslatituderef'][0];
$longitudeRef = empty($input['gpslongituderef'][0]) ? 'E' : $input['gpslongituderef'][0];

$coordinates = new Coordinates(
new DigitalDegrees(
Expand Down
4 changes: 2 additions & 2 deletions src/Reader/Mapper/Exif/DateTimeFieldMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public function mapField($field, array $input, &$output)
{
$this->guardInvalidArguments($field, $input, $output);

if (!array_key_exists('DateTimeOriginal', $input)) {
if (!array_key_exists('datetimeoriginal', $input)) {
return;
}

$datetimeOriginal = new DateTimeImmutable($input['DateTimeOriginal']);
$datetimeOriginal = new DateTimeImmutable($input['datetimeoriginal']);

$output = $output->withCreationDate($datetimeOriginal);
}
Expand Down
10 changes: 5 additions & 5 deletions src/Reader/Mapper/Exif/DimensionsFieldMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ public function mapField($field, array $input, &$output)
{
$this->guardInvalidArguments($field, $input, $output);

if (!array_key_exists('COMPUTED', $input)) {
if (!array_key_exists('computed', $input)) {
return;
}
$input = $input['COMPUTED'];
$input = $input['computed'];

if (!(array_key_exists('Width', $input) && array_key_exists('Height', $input))) {
if (!(array_key_exists('width', $input) && array_key_exists('height', $input))) {
return;
}

$dimensions = new Dimensions(
Width::pixels((int) $input['Width']),
Height::pixels((int) $input['Height'])
Width::pixels((int) $input['width']),
Height::pixels((int) $input['height'])
);

$output = $output->withDimensions($dimensions);
Expand Down
4 changes: 2 additions & 2 deletions src/Reader/Mapper/Exif/ExposureTimeFieldMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public function mapField($field, array $input, &$output)
{
$this->guardInvalidArguments($field, $input, $output);

if (!array_key_exists('ExposureTime', $input)) {
if (!array_key_exists('exposuretime', $input)) {
return;
}

$iso = new ExposureTime($input['ExposureTime']);
$iso = new ExposureTime($input['exposuretime']);

$output = $output->withExposureTime($iso);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Reader/Mapper/Exif/FilenameFieldMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public function mapField($field, array $input, &$output)
{
$this->guardInvalidArguments($field, $input, $output);

if (!array_key_exists('FileName', $input)) {
if (!array_key_exists('filename', $input)) {
return;
}

$filename = new Filename($input['FileName']);
$filename = new Filename($input['filename']);

$output = $output->withFilename($filename);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Reader/Mapper/Exif/FilesizeFieldMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public function mapField($field, array $input, &$output)
{
$this->guardInvalidArguments($field, $input, $output);

if (!array_key_exists('FileSize', $input)) {
if (!array_key_exists('filesize', $input)) {
return;
}

$filesize = new Filesize($input['FileSize']);
$filesize = new Filesize($input['filesize']);

$output = $output->withFilesize($filesize);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Reader/Mapper/Exif/FocusDistanceFieldMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ public function mapField($field, array $input, &$output)
{
$this->guardInvalidArguments($field, $input, $output);

if (!array_key_exists('COMPUTED', $input)
|| !array_key_exists('FocusDistance', $input['COMPUTED'])) {
if (!array_key_exists('computed', $input)
|| !array_key_exists('focusdistance', $input['computed'])) {
return;
}

$focusDistance = new FocusDistance(
$input['COMPUTED']['FocusDistance']
$input['computed']['focusdistance']
);

$output = $output->withFocusDistance($focusDistance);
Expand Down
4 changes: 2 additions & 2 deletions src/Reader/Mapper/Exif/IsoFieldMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public function mapField($field, array $input, &$output)
{
$this->guardInvalidArguments($field, $input, $output);

if (!array_key_exists('ISOSpeedRatings', $input)) {
if (!array_key_exists('isospeedratings', $input)) {
return;
}

$iso = new IsoSpeed((int) $input['ISOSpeedRatings']);
$iso = new IsoSpeed((int) $input['isospeedratings']);

$output = $output->withIsoSpeed($iso);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Reader/Mapper/Exif/MakeFieldMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public function mapField($field, array $input, &$output)
{
$this->guardInvalidArguments($field, $input, $output);

if (!array_key_exists('Make', $input)) {
if (!array_key_exists('make', $input)) {
return;
}

$make = new Make($input['Make']);
$make = new Make($input['make']);

$output = $output->withMake($make);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Reader/Mapper/Exif/MimeTypeFieldMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public function mapField($field, array $input, &$output)
{
$this->guardInvalidArguments($field, $input, $output);

if (!array_key_exists('MimeType', $input)) {
if (!array_key_exists('mimetype', $input)) {
return;
}

$mimeType = new MimeType($input['MimeType']);
$mimeType = new MimeType($input['mimetype']);

$output = $output->withMimeType($mimeType);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Reader/Mapper/Exif/ModelFieldMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public function mapField($field, array $input, &$output)
{
$this->guardInvalidArguments($field, $input, $output);

if (!array_key_exists('Model', $input)) {
if (!array_key_exists('model', $input)) {
return;
}

$model = new Model($input['Model']);
$model = new Model($input['model']);

$output = $output->withModel($model);
}
Expand Down
20 changes: 3 additions & 17 deletions src/Reader/Mapper/Exif/ResolutionFieldMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,6 @@
*/
class ResolutionFieldMapper implements FieldMapper
{
/**
* @var array
*/
protected $map = [
HorizontalResolution::class => [
'dataField' => 'XResolution',
'method' => 'withHorizontalResolution',
],
VerticalResolution::class => [
'dataField' => 'YResolution',
'method' => 'withVerticalResolution',
],
];

use GuardInvalidArgumentsForExifTrait;

/**
Expand All @@ -58,13 +44,13 @@ public function mapField($field, array $input, &$output)
{
$this->guardInvalidArguments($field, $input, $output);

if (!(array_key_exists('XResolution', $input) && array_key_exists('YResolution', $input))) {
if (!(array_key_exists('xresolution', $input) && array_key_exists('yresolution', $input))) {
return;
}

$resolution = new Resolution(
LineResolution::dpi($input['XResolution']), // horizontal
LineResolution::dpi($input['YResolution']) // vertical
LineResolution::dpi($input['xresolution']), // horizontal
LineResolution::dpi($input['yresolution']) // vertical
);

$output = $output->withResolution($resolution);
Expand Down
4 changes: 2 additions & 2 deletions src/Reader/Mapper/Exif/SoftwareFieldMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public function mapField($field, array $input, &$output)
{
$this->guardInvalidArguments($field, $input, $output);

if (!array_key_exists('Software', $input)) {
if (!array_key_exists('software', $input)) {
return;
}

$software = new Software($input['Software']);
$software = new Software($input['software']);

$output = $output->withSoftware($software);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/Reader/Mapper/Exif/ApertureFieldMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class ApertureFieldMapperTest extends BaseFieldMapperTest
* @var array
*/
protected $validInput = [
'COMPUTED' => [
'ApertureFNumber' => 'f/5.6',
'computed' => [
'aperturefnumber' => 'f/5.6',
],
];

Expand Down Expand Up @@ -67,7 +67,7 @@ public function testMapFieldHasDataInOutput()
);

$this->assertEquals(
$this->validInput['COMPUTED']['ApertureFNumber'],
$this->validInput['computed']['aperturefnumber'],
(string) $newData
);
}
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/Reader/Mapper/Exif/CoordinatesFieldMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ class CoordinatesFieldMapperTest extends BaseFieldMapperTest
* @var array
*/
protected $validInput = [
'GPSLatitude' => ['4000/100', '4400/100', '30822/1000'],
'GPSLongitude' => [73, 59, 21.508],
'GPSLatitudeRef' => 'N',
'GPSLongitudeRef' => 'W',
'gpslatitude' => ['4000/100', '4400/100', '30822/1000'],
'gpslongitude' => [73, 59, 21.508],
'gpslatituderef' => 'N',
'gpslongituderef' => 'W',
];

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/Reader/Mapper/Exif/DateTimeFieldMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class DateTimeFieldMapperTest extends BaseFieldMapperTest
* @var array
*/
protected $validInput = [
'DateTimeOriginal' => '2016-11-17 20:00:00',
'datetimeoriginal' => '2016-11-17 20:00:00',
];

/**
Expand All @@ -65,7 +65,7 @@ public function testMapFieldHasDataInOutput()
);

$this->assertEquals(
$this->validInput['DateTimeOriginal'],
$this->validInput['datetimeoriginal'],
$newData->format('Y-m-d H:i:s')
);
}
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/Reader/Mapper/Exif/DimensionsFieldMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ class DimensionsFieldMapperTest extends BaseFieldMapperTest
* @var array
*/
protected $validInput = [
'COMPUTED' => [
'Height' => 1024,
'Width' => 2048,
'computed' => [
'height' => 1024,
'width' => 2048,
],
];

Expand Down Expand Up @@ -82,7 +82,7 @@ public function testMapFieldAbortsWhenNotCorrectDataInInput()
$output = new Exif;
$mapper = new $this->fieldMapperClass();

$mapper->mapField($field, ['COMPUTED' => ['foo' => 'bar',]], $output);
$mapper->mapField($field, ['computed' => ['foo' => 'bar',]], $output);
$dimensions = $output->getDimensions();

$this->assertNull($dimensions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ExposureTimeFieldMapperTest extends BaseFieldMapperTest
* @var array
*/
protected $validInput = [
'ExposureTime' => '10/300',
'exposuretime' => '10/300',
];

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/Reader/Mapper/Exif/FilenameFieldMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class FilenameFieldMapperTest extends BaseFieldMapperTest
* @var array
*/
protected $validInput = [
'FileName' => 'IMG_01234.JPG',
'filename' => 'IMG_01234.JPG',
];

/**
Expand All @@ -65,7 +65,7 @@ public function testMapFieldHasDataInOutput()
);

$this->assertEquals(
$this->validInput['FileName'],
$this->validInput['filename'],
(string) $newData
);
}
Expand Down
Loading