ExifProperties::getPixelYDimension()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the kaloa/xmp package.
5
 *
6
 * For full copyright and license information, please view the LICENSE file
7
 * that was distributed with this source code.
8
 */
9
10
namespace Kaloa\Xmp\Properties;
11
12
use DateTime;
13
use DOMNode;
14
15
/**
16
 * Extracts the Exif properties from an XMP document referenced by an XPath
17
 * instance.
18
 *
19
 * The descriptions for all getters are taken from
20
 * <a href="http://www.exiv2.org/tags-xmp-exif.html">exiv2.org</a>.
21
 *
22
 * @todo More EXIF properties will be added in later versions.
23
 */
24
class ExifProperties extends AbstractProperties
25
{
26
    /**
27
     * See getDateTimeOriginal.
28
     *
29
     * @var DateTime|null
30
     */
31
    private $dateTimeOriginal = null;
32
33
    /**
34
     * See getExifVersion.
35
     *
36
     * @var string
37
     */
38
    private $exifVersion = '';
39
40
    /**
41
     * See getPixelXDimension.
42
     *
43
     * @var string
44
     */
45
    private $pixelXDimension = '';
46
47
    /**
48
     * See getPixelYDimension.
49
     *
50
     * @var string
51
     */
52
    private $pixelYDimension = '';
53
54
    /**
55
     * Tries to fill an entity instance variable with corresponding data from
56
     * the XMP document.
57
     *
58
     * Exif data might be added either as attributes or as independent elements.
59
     * This method checks for both. If both types are found, element content
60
     * will overwrite attribute content.
61
     *
62
     * @param string $entity
63
     */
64 1
    private function fill($entity)
65
    {
66 1
        $value = '';
67 1
        $whatLcfirst = lcfirst($entity);
68
69 1
        foreach ($this->xPath->query('//rdf:Description') as $node) {
70
            /* @var $node DOMNode */
71 1
            if ($node->hasAttributes()) {
72 1
                $attribute = $node->attributes->getNamedItemNS(
73 1
                    'http://ns.adobe.com/exif/1.0/',
74
                    $entity
75 1
                );
76
77 1
                if ($attribute !== null) {
78 1
                    $value = $attribute->nodeValue;
79 1
                }
80 1
            }
81 1
        }
82
83 1
        if ($value === '') {
84 1
            foreach ($this->xPath->query('//exif:' . $entity) as $node) {
85 1
                $value = $node->nodeValue;
86 1
            }
87 1
        }
88
89 1
        $this->{$whatLcfirst} = $value;
90 1
    }
91
92
    /**
93
     * Retrieves all properties from the underlying XMP document.
94
     */
95 1
    final protected function init()
96
    {
97 1
        $this->fill('DateTimeOriginal');
98
99 1
        if (trim($this->dateTimeOriginal) !== '') {
100 1
            $this->dateTimeOriginal = DateTime::createFromFormat(
101 1
                'Y-m-d\TH:i:s.uP',
102 1
                $this->dateTimeOriginal
103 1
            );
104 1
        } else {
105 1
            $this->dateTimeOriginal = null;
106
        }
107
108 1
        $this->fill('ExifVersion');
109 1
        $this->fill('PixelXDimension');
110 1
        $this->fill('PixelYDimension');
111
112
        // Free our reference to the XPath instance.
113 1
        $this->xPath = null;
114 1
    }
115
116
    /**
117
     * Returns date and time when original image was generated, in ISO 8601
118
     * format.
119
     *
120
     * @return DateTime|null
121
     */
122 1
    public function getDateTimeOriginal()
123
    {
124 1
        return $this->dateTimeOriginal;
125
    }
126
127
    /**
128
     * Returns the EXIF version number.
129
     *
130
     * @return string
131
     */
132 1
    public function getExifVersion()
133
    {
134 1
        return $this->exifVersion;
135
    }
136
137
    /**
138
     * Return the image width, in pixels.
139
     *
140
     * @return string
141
     */
142 1
    public function getPixelXDimension()
143
    {
144 1
        return $this->pixelXDimension;
145
    }
146
147
    /**
148
     * Returns the image height, in pixels.
149
     *
150
     * @return string
151
     */
152 1
    public function getPixelYDimension()
153
    {
154 1
        return $this->pixelYDimension;
155
    }
156
}
157