Completed
Pull Request — develop (#2)
by Tom Van
03:35
created

DateTimeFieldMapper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 45
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSupportedFields() 0 6 1
A mapField() 0 14 3
1
<?php
2
/**
3
 * Mapper for mapping data between raw input and a datetime object
4
 *
5
 * @category    PHPExif
6
 * @copyright   Copyright (c) 2016 Tom Van Herreweghe <[email protected]>
7
 * @license     http://github.com/PHPExif/php-exif-exiftool/blob/master/LICENSE MIT License
8
 * @link        http://github.com/PHPExif/php-exif-exiftool for the canonical source repository
9
 * @package     Exiftool
10
 */
11
12
namespace PHPExif\Adapter\Exiftool\Reader\Mapper\Exif;
13
14
use PHPExif\Common\Data\ExifInterface;
15
use PHPExif\Common\Mapper\FieldMapper;
16
use PHPExif\Common\Mapper\GuardInvalidArgumentsForExifTrait;
17
use \DateTimeImmutable;
18
19
/**
20
 * Mapper
21
 *
22
 * @category    PHPExif
23
 * @package     Exiftool
24
 */
25
class DateTimeFieldMapper implements FieldMapper
26
{
27
    use GuardInvalidArgumentsForExifTrait;
28
    use ValidKeysTrait;
29
30
    /**
31
     * @var array
32
     */
33
    private $validKeys = [
34
        'system:filemodifydate',
35
        'composite:subsecdatetimeoriginal',
36
        'composite:subsecmodifydate',
37
        'exififd:datetimeoriginal',
38
        'exififd:createdate',
39
        'ifd0:modifydate',
40
    ];
41
42
    /**
43
     * {@inheritDoc}
44
     */
45
    public function getSupportedFields()
46
    {
47
        return array(
48
            DateTimeImmutable::class,
49
        );
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55
    public function mapField($field, array $input, &$output)
56
    {
57
        $this->guardInvalidArguments($field, $input, $output);
58
59
        foreach ($this->validKeys as $key) {
60
            if (!array_key_exists($key, $input)) {
61
                continue;
62
            }
63
64
            $datetimeOriginal = new DateTimeImmutable($input[$key]);
65
            $output = $output->withCreationDate($datetimeOriginal);
66
            break;
67
        }
68
    }
69
}
70