vCardFormat::getFormattedPhoto()   A
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 13
cp 0
rs 9.2222
c 0
b 0
f 0
cc 6
nc 3
nop 0
crap 42
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/qrcode-library project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\QrCode\Format;
13
14
use Da\QrCode\Exception\InvalidConfigException;
15
use Da\QrCode\Traits\EmailTrait;
16
use Da\QrCode\Traits\UrlTrait;
17
18
/**
19
 * Class vCard creates a valid vCard 4.0 QrCode string
20
 *
21
 * @author Antonio Ramirez <[email protected]>
22
 * @link https://www.2amigos.us/
23
 * @package Da\QrCode\Format
24
 */
25
class vCardFormat extends AbstractFormat
26
{
27
    use EmailTrait;
28
    use UrlTrait;
29
30
    /**
31
     * @var string the name
32
     */
33
    public $name;
34
    /**
35
     * @var string the full name
36
     */
37
    public $fullName;
38
    /**
39
     * @var string the address
40
     */
41
    public $address;
42
    /**
43
     * @var string the nickname
44
     */
45
    public $nickName;
46
    /**
47
     * @var string the work phone
48
     */
49
    public $workPhone;
50
    /**
51
     * @var string the home phone
52
     */
53
    public $homePhone;
54
    /**
55
     * @var string a date in the format YYYY-MM-DD or ISO 860
56
     */
57
    public $birthday;
58
    /**
59
     * @var string a date in the format YYYY-MM-DD or ISO 860
60
     */
61
    public $anniversary;
62
    /**
63
     * @var string the gender
64
     */
65
    public $gender;
66
    /**
67
     * @var string the categories. A list of "tags" that can be used to describe the object represented by this vCard.
68
     *             e.g., developer,designer,climber,swimmer
69
     */
70
    public $categories;
71
    /**
72
     * @var string the instant messaging and presence protocol (instant messenger id)
73
     */
74
    public $impp;
75
    /**
76
     * @var string the photo
77
     */
78
    public $photo;
79
    /**
80
     * @var string the role e.g., Executive
81
     */
82
    public $role;
83
    /**
84
     * @var string the name and optionally the unit(s) of the organization
85
     *             associated with the vCard object. This property is based on the X.520 Organization Name
86
     *             attribute and the X.520 Organization Unit attribute.
87
     */
88
    public $organization;
89
    /**
90
     * @var string notes
91
     */
92
    public $note;
93
    /**
94
     * @var string language of the user
95
     */
96
    public $lang;
97
98
    /**
99
     * @throws InvalidConfigException
100
     * @return string
101
     */
102
    public function getText(): string
103
    {
104
        $data = [];
105
        $data[] = 'BEGIN:VCARD';
106
        $data[] = 'VERSION:4.0';
107
        $data[] = "N:{$this->name}";
108
        $data[] = "FN:{$this->fullName}";
109
        $data[] = "ADR:{$this->address}";
110
        $data[] = "NICKNAME:{$this->nickName}";
111
        $data[] = "EMAIL;TYPE=PREF,INTERNET:{$this->email}";
112
        $data[] = "TEL;TYPE=WORK:{$this->workPhone}";
113
        $data[] = "TEL;TYPE=HOME:{$this->homePhone}";
114
        $data[] = "BDAY:{$this->birthday}";
115
        $data[] = "GENDER:{$this->gender}";
116
        $data[] = "CATEGORIES:{$this->categories}";
117
        $data[] = "IMPP:{$this->impp}";
118
        $data[] = $this->getFormattedPhoto();
119
        $data[] = "ROLE:{$this->role}";
120
        $data[] = "URL:{$this->url}";
121
        $data[] = "ORG:{$this->organization}";
122
        $data[] = "NOTE:{$this->note}";
123
        $data[] = "LANG:{$this->lang}";
124
        $data[] = 'END:VCARD';
125
126
        return implode("\n", array_filter($data));
127
    }
128
129
    /**
130
     * @throws InvalidConfigException
131
     * @return string  the formatted photo. Makes sure is of the right image extension.
132
     */
133
    protected function getFormattedPhoto(): ?string
134
    {
135
136
        if ($this->photo !== null) {
137
            $ext = strtolower(substr(strrchr($this->photo, '.'), 1));
138
            if ($ext === 'jpeg' || $ext === 'jpg' || $ext === 'png' || $ext === 'gif') {
139
                $ext = strtoupper($ext);
140
            } else {
141
                throw new InvalidConfigException('Invalid format Image!');
142
            }
143
            return 'PHOTO;VALUE=URL;TYPE=' . $ext . ':' . $this->photo;
144
        }
145
146
        return null;
147
    }
148
}
149