Skip to content

Mappers for all fields #2

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

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
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: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"keywords": ["EXIF", "IPTC", "jpeg", "tiff", "exiftool"],
"require": {
"php": "^5.6 || ^7.0",
"ext-exif": "*",
"miljar/php-exif-common": "dev-develop"
},
"require-dev": {
Expand Down
229 changes: 121 additions & 108 deletions composer.lock

Large diffs are not rendered by default.

50 changes: 39 additions & 11 deletions src/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @package Exiftool
*/

namespace PHPExif\Adapter\Native;
namespace PHPExif\Adapter\Exiftool;

use PHPExif\Common\Adapter\MapperInterface;
use PHPExif\Common\Adapter\ReaderInterface;
Expand All @@ -31,6 +31,7 @@ final class Reader implements ReaderInterface
{
const PATH = 'path';
const BIN = 'binary';
const NUMERIC = 'numeric';

/**
* @var MapperInterface
Expand All @@ -45,7 +46,7 @@ final class Reader implements ReaderInterface
/**
* @var bool
*/
private $numeric = true;
private $numeric;

/**
* @var string
Expand All @@ -62,11 +63,13 @@ public function __construct(
$defaults = [
self::BIN => 'exiftool',
self::PATH => '/usr/bin/env',
self::NUMERIC => true,
];
$config = array_replace($defaults, $config);

$this->binary = $config[self::BIN];
$this->path = $config[self::PATH];
$this->numeric = $config[self::NUMERIC];

$this->mapper = $mapper;
}
Expand Down Expand Up @@ -94,12 +97,13 @@ public function getMetadataFromFile($filePath)
)
);

$data = json_decode($result, true);

if (false === $data) {
if (false === $result) {
throw NoExifDataException::fromFile($filePath);
}

$data = json_decode($result, true)[0];
$data = $this->normalizeArrayKeys($data);

// map the data:
$mapper = $this->getMapper();
$metadata = new Metadata(
Expand All @@ -111,14 +115,38 @@ 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
);
}

/**
* Returns the output from given cli command
*
* @param string $command
*
* @throws RuntimeException If the command can't be executed
*
* @return string
* @return string|boolean
*/
protected function getCliOutput($command)
{
Expand All @@ -128,11 +156,11 @@ protected function getCliOutput($command)
2 => array('pipe', 'a')
);
$process = proc_open($command, $descriptorspec, $pipes);

if (!is_resource($process)) {
throw new RuntimeException(
'Could not open a resource to the exiftool binary'
);
return false;
}

$result = stream_get_contents($pipes[1]);
fclose($pipes[0]);
fclose($pipes[1]);
Expand Down
65 changes: 65 additions & 0 deletions src/Reader/Mapper/Exif/ApertureFieldMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* Mapper for mapping data between raw input and an Aperture VO
*
* @category PHPExif
* @copyright Copyright (c) 2016 Tom Van Herreweghe <tom@theanalogguy.be>
* @license http://github.com/PHPExif/php-exif-exiftool/blob/master/LICENSE MIT License
* @link http://github.com/PHPExif/php-exif-exiftool for the canonical source repository
* @package Exiftool
*/

namespace PHPExif\Adapter\Exiftool\Reader\Mapper\Exif;

use PHPExif\Common\Data\ExifInterface;
use PHPExif\Common\Data\ValueObject\Aperture;
use PHPExif\Common\Mapper\FieldMapper;
use PHPExif\Common\Mapper\GuardInvalidArgumentsForExifTrait;

/**
* Mapper
*
* @category PHPExif
* @package Exiftool
*/
class ApertureFieldMapper implements FieldMapper
{
use GuardInvalidArgumentsForExifTrait;
use ValidKeysTrait;

/**
* @var array
*/
private $validKeys = [
'composite:aperture',
'exififd:fnumber',
'exififd:aperturevalue',
];

/**
* {@inheritDoc}
*/
public function getSupportedFields()
{
return array(
Aperture::class,
);
}

/**
* {@inheritDoc}
*/
public function mapField($field, array $input, &$output)
{
$this->guardInvalidArguments($field, $input, $output);

foreach ($this->validKeys as $key) {
if (!array_key_exists($key, $input)) {
continue;
}

$aperture = new Aperture($input[$key]);
$output = $output->withAperture($aperture);
}
}
}
69 changes: 69 additions & 0 deletions src/Reader/Mapper/Exif/DateTimeFieldMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* Mapper for mapping data between raw input and a datetime object
*
* @category PHPExif
* @copyright Copyright (c) 2016 Tom Van Herreweghe <tom@theanalogguy.be>
* @license http://github.com/PHPExif/php-exif-exiftool/blob/master/LICENSE MIT License
* @link http://github.com/PHPExif/php-exif-exiftool for the canonical source repository
* @package Exiftool
*/

namespace PHPExif\Adapter\Exiftool\Reader\Mapper\Exif;

use PHPExif\Common\Data\ExifInterface;
use PHPExif\Common\Mapper\FieldMapper;
use PHPExif\Common\Mapper\GuardInvalidArgumentsForExifTrait;
use \DateTimeImmutable;

/**
* Mapper
*
* @category PHPExif
* @package Exiftool
*/
class DateTimeFieldMapper implements FieldMapper
{
use GuardInvalidArgumentsForExifTrait;
use ValidKeysTrait;

/**
* @var array
*/
private $validKeys = [
'system:filemodifydate',
'composite:subsecdatetimeoriginal',
'composite:subsecmodifydate',
'exififd:datetimeoriginal',
'exififd:createdate',
'ifd0:modifydate',
];

/**
* {@inheritDoc}
*/
public function getSupportedFields()
{
return array(
DateTimeImmutable::class,
);
}

/**
* {@inheritDoc}
*/
public function mapField($field, array $input, &$output)
{
$this->guardInvalidArguments($field, $input, $output);

foreach ($this->validKeys as $key) {
if (!array_key_exists($key, $input)) {
continue;
}

$datetimeOriginal = new DateTimeImmutable($input[$key]);
$output = $output->withCreationDate($datetimeOriginal);
break;
}
}
}
65 changes: 65 additions & 0 deletions src/Reader/Mapper/Exif/ExposureTimeFieldMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* Mapper for mapping data between raw input and a ExposureTime VO
*
* @category PHPExif
* @copyright Copyright (c) 2016 Tom Van Herreweghe <tom@theanalogguy.be>
* @license http://github.com/PHPExif/php-exif-exiftool/blob/master/LICENSE MIT License
* @link http://github.com/PHPExif/php-exif-exiftool for the canonical source repository
* @package Exiftool
*/

namespace PHPExif\Adapter\Exiftool\Reader\Mapper\Exif;

use PHPExif\Common\Data\ExifInterface;
use PHPExif\Common\Data\ValueObject\ExposureTime;
use PHPExif\Common\Mapper\FieldMapper;
use PHPExif\Common\Mapper\GuardInvalidArgumentsForExifTrait;

/**
* Mapper
*
* @category PHPExif
* @package Exiftool
*/
class ExposureTimeFieldMapper implements FieldMapper
{
use GuardInvalidArgumentsForExifTrait;
use ValidKeysTrait;

/**
* @var array
*/
private $validKeys = [
'exififd:exposuretime',
'composite:shutterspeed',
];

/**
* {@inheritDoc}
*/
public function getSupportedFields()
{
return array(
ExposureTime::class,
);
}

/**
* {@inheritDoc}
*/
public function mapField($field, array $input, &$output)
{
$this->guardInvalidArguments($field, $input, $output);

foreach ($this->validKeys as $key) {
if (!array_key_exists($key, $input)) {
continue;
}

$shutterSpeed = new ExposureTime($input[$key]);
$output = $output->withExposureTime($shutterSpeed);
break;
}
}
}
65 changes: 65 additions & 0 deletions src/Reader/Mapper/Exif/FilenameFieldMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* Mapper for mapping data between raw input and a Filename VO
*
* @category PHPExif
* @copyright Copyright (c) 2016 Tom Van Herreweghe <tom@theanalogguy.be>
* @license http://github.com/PHPExif/php-exif-exiftool/blob/master/LICENSE MIT License
* @link http://github.com/PHPExif/php-exif-exiftool for the canonical source repository
* @package Exiftool
*/

namespace PHPExif\Adapter\Exiftool\Reader\Mapper\Exif;

use PHPExif\Common\Data\ExifInterface;
use PHPExif\Common\Data\ValueObject\Filename;
use PHPExif\Common\Mapper\FieldMapper;
use PHPExif\Common\Mapper\GuardInvalidArgumentsForExifTrait;

/**
* Mapper
*
* @category PHPExif
* @package Exiftool
*/
class FilenameFieldMapper implements FieldMapper
{
use GuardInvalidArgumentsForExifTrait;
use ValidKeysTrait;

/**
* @var array
*/
private $validKeys = [
'sourcefile',
'system:filename',
];

/**
* {@inheritDoc}
*/
public function getSupportedFields()
{
return array(
Filename::class,
);
}

/**
* {@inheritDoc}
*/
public function mapField($field, array $input, &$output)
{
$this->guardInvalidArguments($field, $input, $output);

foreach ($this->validKeys as $key) {
if (!array_key_exists($key, $input)) {
continue;
}

$filename = new Filename($input[$key]);
$output = $output->withFilename($filename);
break;
}
}
}
Loading