XMPParser::getPhotoMechanic()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Jackal\ImageMerge\Metadata\Parser;
4
5
use DateTime;
6
use Exception;
7
use Jackal\ImageMerge\Model\File\FileObjectInterface;
8
9
/**
10
 * Class XMPParser
11
 * @package Jackal\ImageMerge\Metadata\Parser
12
 */
13
class XMPParser extends AbstractParser
14
{
15
    /**
16
     * XMPParser constructor.
17
     * @param FileObjectInterface $file
18
     */
19
    public function __construct(FileObjectInterface $file)
20
    {
21
        $content = $file->getContents();
22
23
        $xmp_data_start = strpos($content, '<x:xmpmeta');
24
        $xmp_data_end = strpos($content, '</x:xmpmeta>');
25
26
        $xmp_data = '';
27
        if ($xmp_data_start !== false) {
28
            $xmp_length = $xmp_data_end - $xmp_data_start;
29
            $xmp_data = substr($content, $xmp_data_start, $xmp_length + 12);
30
        }
31
32
        $xmp_arr = [];
33
        foreach ([
34
                     'creator_email' => '<Iptc4xmpCore:CreatorContactInfo[^>]+?CiEmailWork="([^"]*)"',
35
                     'owner' => '<rdf:Description[^>]+?aux:OwnerName="([^"]*)"',
36
                     'created_at' => '<rdf:Description[^>]+?xmp:CreateDate="([^"]*)"',
37
                     'modified_at' => '<rdf:Description[^>]+?xmp:ModifyDate="([^"]*)"',
38
                     'label' => '<rdf:Description[^>]+?xmp:Label="([^"]*)"',
39
                     'credit' => '<rdf:Description[^>]+?photoshop:Credit="([^"]*)"',
40
                     'source' => '<rdf:Description[^>]+?photoshop:Source="([^"]*)"',
41
                     'caption_writer' => '<rdf:Description[^>]+?photoshop:CaptionWriter="([^"]*)"',
42
43
                     'photomechanic_prefs' => '<rdf:Description[^>]+?photomechanic:Prefs="([^"]*)"',
44
                     'photomechanic_pm_version' => '<rdf:Description[^>]+?photomechanic:PMVersion="([^"]*)"',
45
                     'photomechanic_tagged' => '<rdf:Description[^>]+?photomechanic:Tagged="([^"]*)"',
46
                     'photomechanic_color_class' => '<rdf:Description[^>]+?photomechanic:ColorClass="([^"]*)"',
47
48
                     'headline' => '<rdf:Description[^>]+?photoshop:Headline="([^"]*)"',
49
                     'city' => '<rdf:Description[^>]+?photoshop:City="([^"]*)"',
50
                     'state' => '<rdf:Description[^>]+?photoshop:State="([^"]*)"',
51
                     'country' => '<rdf:Description[^>]+?photoshop:Country="([^"]*)"',
52
                     'country_code' => '<rdf:Description[^>]+?Iptc4xmpCore:CountryCode="([^"]*)"',
53
                     'location' => '<rdf:Description[^>]+?Iptc4xmpCore:Location="([^"]*)"',
54
                     'title' => '<dc:title>\s*<rdf:Alt>\s*(.*?)\s*<\/rdf:Alt>\s*<\/dc:title>',
55
                     'description' => '<dc:description>\s*<rdf:Alt>\s*(.*?)\s*<\/rdf:Alt>\s*<\/dc:description>',
56
                     'creator' => '<dc:creator>\s*<rdf:Seq>\s*(.*?)\s*<\/rdf:Seq>\s*<\/dc:creator>',
57
                     'keywords' => '<dc:subject>\s*<rdf:Bag>\s*(.*?)\s*<\/rdf:Bag>\s*<\/dc:subject>',
58
                     'rights' => '<dc:Rights>\s*(.*?)\s*<\/dc:Rights>',
59
                 ] as $key => $regex) {
60
61
            // get a single text string
62
            $xmp_arr[$key] = preg_match("/$regex/is", $xmp_data, $match) ? $match[1] : '';
63
64
            // if string contains a list, then re-assign the variable as an array with the list elements
65
            $xmp_arr[$key] = preg_match_all("/<rdf:li[^>]*>([^>]*)<\/rdf:li>/is", $xmp_arr[$key], $match) ? $match[1] : $xmp_arr[$key];
66
67
            $this->data[$key] = $this->sanitizeChars($xmp_arr[$key]);
68
        }
69
    }
70
71
    /**
72
     * @return array
73
     */
74
    public function getPhotoMechanic()
75
    {
76
        return [
77
            'prefs' => $this->getSingleValue('photomechanic_prefs'),
78
            'pm_version' => $this->getSingleValue('photomechanic_pm_version'),
79
            'tagged' => $this->getBooleanValue('photomechanic_tagged'),
80
            'color_class' => $this->getSingleValue('photomechanic_color_class'),
81
        ];
82
    }
83
84
    /**
85
     * @return null|string
86
     */
87
    public function getCaptionWriter()
88
    {
89
        return $this->getSingleValue('caption_writer');
90
    }
91
92
    /**
93
     * @return null|string
94
     */
95
    public function getCreator()
96
    {
97
        return $this->getSingleValue('creator');
98
    }
99
100
    /**
101
     * @return string|null
102
     */
103
    public function getDescription()
104
    {
105
        return $this->getSingleValue('description');
106
    }
107
108
    /**
109
     * @return DateTime
110
     * @throws Exception
111
     */
112
    public function getCreationDateTime()
113
    {
114
        return new DateTime($this->getSingleValue('created_at'));
115
    }
116
117
    /**
118
     * @return array|null|string
119
     */
120
    public function getKeywords()
121
    {
122
        return $this->getValue('keywords');
123
    }
124
125
    /**
126
     * @return mixed
127
     */
128
    public function getCopyrights()
129
    {
130
        return $this->data['rights'];
131
    }
132
133
    /**
134
     * @param $valueArr
135
     * @return mixed
136
     */
137
    private function sanitizeChars($valueArr)
138
    {
139
        if (is_array($valueArr)) {
140
            foreach ($valueArr as &$value) {
141
                $value = preg_replace('/&#x(A{1});/', "\n", $value);
142
            }
143
        }
144
145
        return $valueArr;
146
    }
147
148
    /**
149
     * @return array
150
     * @throws Exception
151
     */
152
    public function toArray()
153
    {
154
        return [
155
            'photomechanic' => $this->getPhotoMechanic(),
156
            'caption' => $this->getCaptionWriter(),
157
            'created_by' => $this->getCreator(),
158
            'description' => $this->getDescription(),
159
            'created_at' => $this->getCreationDateTime(),
160
            'keywords' => $this->getKeywords(),
161
            'copyrights' => $this->getCopyrights(),
162
        ];
163
    }
164
}
165