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

DateTimeFieldMapper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 64
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getValidKeys() 0 4 1
A setValidKeys() 0 4 1
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     Common
24
 */
25
class DateTimeFieldMapper implements FieldMapper
26
{
27
    use GuardInvalidArgumentsForExifTrait;
28
29
    /**
30
     * @var array
31
     */
32
    private $validKeys = [
33
        'system:filemodifydate',
34
        'composite:subsecdatetimeoriginal',
35
        'composite:subsecmodifydate',
36
        'exififd:datetimeoriginal',
37
        'exififd:createdate',
38
        'ifd0:modifydate',
39
    ];
40
41
    /**
42
     * Getter for validKeys
43
     *
44
     * @return array
45
     */
46
    public function getValidKeys()
47
    {
48
        return $this->validKeys;
49
    }
50
51
    /**
52
     * Setter for validKeys
53
     *
54
     * @param array $validKeys
55
     */
56
    public function setValidKeys(array $validKeys)
57
    {
58
        $this->validKeys = $validKeys;
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64
    public function getSupportedFields()
65
    {
66
        return array(
67
            DateTimeImmutable::class,
68
        );
69
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74
    public function mapField($field, array $input, &$output)
75
    {
76
        $this->guardInvalidArguments($field, $input, $output);
77
78
        foreach ($this->validKeys as $key) {
79
            if (!array_key_exists($key, $input)) {
80
                continue;
81
            }
82
83
            $datetimeOriginal = new DateTimeImmutable($input[$key]);
84
            $output = $output->withCreationDate($datetimeOriginal);
85
            break;
86
        }
87
    }
88
}
89