Passed
Push — master ( b8156a...5ec36b )
by Stefan
01:47
created

VCardContactReader::parsePhoto()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 4
nc 4
nop 2
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\VCard;
5
6
/**
7
 * Helper class that parses property lines from a VCard format file.
8
 *
9
 * @package SKien-VCard
10
 * @since 1.0.4
11
 * @version 1.0.4
12
 * @author Stefanius <[email protected]>
13
 * @copyright MIT License - see the LICENSE file for details
14
 */
15
class VCardContactReader
16
{
17
    use VCardHelper;
18
19
    /** @var VCardContact the contact to write     */
20
    protected VCardContact $oContact;
21
22
    /**
23
     * Create a contact reader object.
24
     * @param VCardContact $oContact    the contact to build
25
     */
26
    function __construct(VCardContact $oContact)
27
    {
28
        $this->oContact = $oContact;
29
    }
30
31
    /**
32
     * Add property from import file.
33
     * @param string $strName
34
     * @param array $aParams
35
     * @param string $strValue
36
     */
37
    public function addProperty(string $strName, array $aParams, string $strValue) : void
38
    {
39
        // table to parse property depending on propertyname.
40
        // value have to be either name of method with signature
41
        //
42
        //      methodname( string strValue, array aParams )
43
        //
44
        // or
45
        //      propertyname  ( => unmasked value will be assigned to property of contact object)
46
        //
47
        $aMethodOrProperty = array(
48
            'N'             => 'parseName',
49
            'ADR'           => 'parseAdr',
50
            'TEL'           => 'parseTel',
51
            'EMAIL'         => 'parseEMail',
52
            'CATEGORIES'    => 'parseCategories',
53
            'CATEGORY'      => 'parseCategories',
54
            'ORG'           => 'parseOrg',
55
            'PHOTO'         => 'parsePhoto',
56
            'NICKNAME'      => 'setNickName',
57
            'TITLE'         => 'setPosition',
58
            'ROLE'          => 'setRole',
59
            'URL'           => 'setHomepage',
60
            'NOTE'          => 'setNote',
61
            'LABEL'         => 'setLabel',
62
            'BDAY'          => 'setDateOfBirth',
63
            'X-WAB-GENDER'  => 'setGender'
64
        );
65
66
        // supported only by vcard version 2.1
67
        if (isset($aParams['ENCODING']) && $aParams['ENCODING'] == 'QUOTED-PRINTABLE') {
68
            $strValue = quoted_printable_decode($strValue);
69
        }
70
71
        if (isset($aMethodOrProperty[$strName])) {
72
            $strPtr = $aMethodOrProperty[$strName];
73
            if (method_exists($this, $strPtr)) {
74
                // call method
75
                call_user_func_array(array($this, $strPtr), array($strValue, $aParams));
76
            } elseif (method_exists($this->oContact, $strPtr)) {
77
                // call setter from contact with unmasket value
78
                call_user_func_array(array($this->oContact, $strPtr), array($strValue));
79
            }
80
        }
81
    }
82
83
    /**
84
     * Explode string into name components.
85
     * Order of the components separated by ';':
86
     *  - family name
87
     *  - given name
88
     *  - additional name(s) (not supported)
89
     *  - honorific prefixes
90
     *  - honorific suffixes
91
     *  delimitered by semicolon (be aware of masked delimiters)
92
     * @param string $strValue
93
     * @param array $aParams
94
     */
95
    protected function parseName(string $strValue, array $aParams) : void
96
    {
97
        $aSplit = $this->explodeMaskedString(';', $strValue);
98
        // family name; given name; ; honorific prefixes; honorific suffixes
99
        $strLastName = $this->unmaskString($aSplit[0]);
100
        $strFirstName = isset($aSplit[1]) ? $this->unmaskString($aSplit[1]) : '';
101
        $this->oContact->setName($strLastName, $strFirstName);
102
        if (isset($aSplit[3])) {
103
            $this->oContact->setPrefix($this->unmaskString($aSplit[3]));
104
        }
105
        if (isset($aSplit[4])) {
106
            $this->oContact->setSuffix($this->unmaskString($aSplit[4]));
107
        }
108
    }
109
110
    /**
111
     * @param string $strValue
112
     * @param array $aParams
113
     * @see VCardAddress::parseFullAddress()
114
     */
115
    protected function parseAdr(string $strValue, array $aParams) : void
116
    {
117
        $oAdr = new VCardAddress();
118
        $oAdr->parseFullAddress($strValue, $aParams);
119
        $this->oContact->addAddress($oAdr, false);
120
    }
121
122
    /**
123
     * Unmask value and add with typeinfo to phone list.
124
     * @param string $strValue
125
     * @param array $aParams
126
     */
127
    protected function parseTel(string $strValue, array $aParams) : void
128
    {
129
        $strValue = $this->unmaskString($strValue);
130
        $this->oContact->addPhone($strValue, $aParams['TYPE'], strpos($aParams['TYPE'], 'PREF') !== false);
131
    }
132
133
    /**
134
     * Unmask value and add to email list.
135
     * @param string $strValue
136
     * @param array $aParams
137
     */
138
    protected function parseEMail(string $strValue, array $aParams) : void
139
    {
140
        $strValue = $this->unmaskString($strValue);
141
        $this->oContact->addEMail($strValue, strpos($aParams['TYPE'], 'PREF') !== false);
142
    }
143
144
    /**
145
     * Split into company and section.
146
     * @param string $strValue
147
     * @param array $aParams
148
     */
149
    protected function parseOrg(string $strValue, array $aParams) : void
150
    {
151
        $aSplit = $this->explodeMaskedString(';', $strValue);
152
        $this->oContact->setOrganisation($this->unmaskString($aSplit[0]));
153
        if (isset($aSplit[1])) {
154
            $this->oContact->setSection($this->unmaskString($aSplit[1]));
155
        }
156
    }
157
158
    /**
159
     * Split comma separated categories.
160
     * @param string $strValue
161
     * @param array $aParams
162
     */
163
    protected function parseCategories(string $strValue, array $aParams) : void
164
    {
165
        $aSplit = $this->explodeMaskedString(',', $strValue);
166
        foreach ($aSplit as $strCategory) {
167
            $this->oContact->addCategory($this->unmaskString($strCategory));
168
        }
169
    }
170
171
    /**
172
     * @param string $strValue
173
     * @param array $aParams
174
     */
175
    protected function parsePhoto(string $strValue, array $aParams) : void
176
    {
177
        $strEncoding = isset($aParams['ENCODING']) ? $aParams['ENCODING'] : '';
178
        if ($strEncoding == 'B' || $strEncoding == 'BASE64') {
179
            $strType = strtolower($aParams['TYPE']);
180
            $this->oContact->setPortraitBlob('data:image/' . $strType . ';base64,' . $strValue);
181
        } else {
182
            // assuming URL value... e.g. export from google contacts
183
            $this->oContact->setPortraitFile($strValue);
184
        }
185
    }
186
}