| Total Complexity | 74 |
| Total Lines | 459 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like vCard often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use vCard, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 92 | class vCard |
||
| 93 | { |
||
| 94 | /** |
||
| 95 | * @var array array of properties |
||
| 96 | */ |
||
| 97 | public $properties; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var string filename |
||
| 101 | */ |
||
| 102 | public $filename; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var string encoding |
||
| 106 | */ |
||
| 107 | public $encoding = "ENCODING=QUOTED-PRINTABLE"; |
||
| 108 | |||
| 109 | |||
| 110 | /** |
||
| 111 | * mise en forme du numero de telephone |
||
| 112 | * |
||
| 113 | * @param int $number numero de telephone |
||
| 114 | * @param string $type Type ('cell') |
||
| 115 | * @return void |
||
| 116 | */ |
||
| 117 | public function setPhoneNumber($number, $type = "") |
||
| 118 | { |
||
| 119 | // type may be PREF | WORK | HOME | VOICE | FAX | MSG | CELL | PAGER | BBS | CAR | MODEM | ISDN | VIDEO or any senseful combination, e.g. "PREF;WORK;VOICE" |
||
| 120 | $key = "TEL"; |
||
| 121 | if ($type != "") { |
||
| 122 | $key .= ";" . $type; |
||
| 123 | } |
||
| 124 | $key .= ";VALUE=uri"; |
||
| 125 | //$key .= ";".$this->encoding; |
||
| 126 | $this->properties[$key] = 'tel:' . $number; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * mise en forme de la photo |
||
| 131 | * warning NON TESTE ! |
||
| 132 | * |
||
| 133 | * @param string $type Type 'image/jpeg' or 'JPEG' |
||
| 134 | * @param string $photo Photo |
||
| 135 | * @return void |
||
| 136 | */ |
||
| 137 | public function setPhoto($type, $photo) |
||
| 138 | { |
||
| 139 | // $type = "GIF" | "JPEG" |
||
| 140 | //$this->properties["PHOTO;MEDIATYPE=$type;ENCODING=BASE64"] = base64_encode($photo); |
||
| 141 | $this->properties["PHOTO;MEDIATYPE=$type"] = $photo; // must be url of photo |
||
| 142 | //$this->properties["PHOTO;TYPE=$type;ENCODING=BASE64"] = base64_encode($photo); // must be content of image |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * mise en forme du nom format |
||
| 147 | * |
||
| 148 | * @param string $name Name |
||
| 149 | * @return void |
||
| 150 | */ |
||
| 151 | public function setFormattedName($name) |
||
| 152 | { |
||
| 153 | $this->properties["FN;" . $this->encoding] = encode($name); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * mise en forme du nom complete |
||
| 158 | * |
||
| 159 | * @param string $family Family name |
||
| 160 | * @param string $first First name |
||
| 161 | * @param string $additional Additional (e.g. second name, nick name) |
||
| 162 | * @param string $prefix Title prefix (e.g. "Mr.", "Ms.", "Prof.") |
||
| 163 | * @param string $suffix Suffix (e.g. "sen." for senior, "jun." for junior) |
||
| 164 | * @return void |
||
| 165 | */ |
||
| 166 | public function setName($family = "", $first = "", $additional = "", $prefix = "", $suffix = "") |
||
| 167 | { |
||
| 168 | //$this->properties["N;".$this->encoding] = encode($family).";".encode($first).";".encode($additional).";".encode($prefix).";".encode($suffix); |
||
| 169 | $this->properties["N"] = encode($family) . ";" . encode($first) . ";" . encode($additional) . ";" . encode($prefix) . ";" . encode($suffix); |
||
| 170 | $this->filename = "$first%20$family.vcf"; |
||
| 171 | if (empty($this->properties["FN"])) { |
||
| 172 | $this->setFormattedName(trim("$prefix $first $additional $family $suffix")); |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * mise en forme de l'anniversaire |
||
| 178 | * |
||
| 179 | * @param integer $date Date |
||
| 180 | * @return void |
||
| 181 | */ |
||
| 182 | public function setBirthday($date) |
||
| 183 | { |
||
| 184 | // $date format is YYYY-MM-DD - RFC 2425 and RFC 2426 for vcard v3 |
||
| 185 | // $date format is YYYYMMDD or ISO8601 for vcard v4 |
||
| 186 | $this->properties["BDAY"] = dol_print_date($date, 'dayxcard'); |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Address |
||
| 191 | * |
||
| 192 | * @param string $postoffice Postoffice |
||
| 193 | * @param string $extended Extended |
||
| 194 | * @param string $street Street |
||
| 195 | * @param string $city City |
||
| 196 | * @param string $region Region |
||
| 197 | * @param string $zip Zip |
||
| 198 | * @param string $country Country |
||
| 199 | * @param string $type Type |
||
| 200 | * @param string $label Label |
||
| 201 | * @return void |
||
| 202 | */ |
||
| 203 | public function setAddress($postoffice = "", $extended = "", $street = "", $city = "", $region = "", $zip = "", $country = "", $type = "", $label = "") |
||
| 204 | { |
||
| 205 | // $type may be DOM | INTL | POSTAL | PARCEL | HOME | WORK or any combination of these: e.g. "WORK;PARCEL;POSTAL" |
||
| 206 | $key = "ADR"; |
||
| 207 | if ($type != "") { |
||
| 208 | $key .= ";" . $type; |
||
| 209 | } |
||
| 210 | if ($label != "") { |
||
| 211 | $key .= ';LABEL="' . encode($label) . '"'; |
||
| 212 | } |
||
| 213 | $key .= ";" . $this->encoding; |
||
| 214 | $this->properties[$key] = encode($postoffice) . ";" . encode($extended) . ";" . encode($street) . ";" . encode($city) . ";" . encode($region) . ";" . encode($zip) . ";" . encode($country); |
||
| 215 | |||
| 216 | //if ($this->properties["LABEL;".$type.";".$this->encoding] == '') { |
||
| 217 | //$this->setLabel($postoffice, $extended, $street, $city, $region, $zip, $country, $type); |
||
| 218 | //} |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Address (old standard) |
||
| 223 | * |
||
| 224 | * @param string $postoffice Postoffice |
||
| 225 | * @param string $extended Extended |
||
| 226 | * @param string $street Street |
||
| 227 | * @param string $city City |
||
| 228 | * @param string $region Region |
||
| 229 | * @param string $zip Zip |
||
| 230 | * @param string $country Country |
||
| 231 | * @param string $type Type |
||
| 232 | * @return void |
||
| 233 | * @deprecated |
||
| 234 | */ |
||
| 235 | public function setLabel($postoffice = "", $extended = "", $street = "", $city = "", $region = "", $zip = "", $country = "", $type = "HOME") |
||
| 236 | { |
||
| 237 | $label = ""; |
||
| 238 | if ($postoffice != "") { |
||
| 239 | $label .= "$postoffice\r\n"; |
||
| 240 | } |
||
| 241 | if ($extended != "") { |
||
| 242 | $label .= "$extended\r\n"; |
||
| 243 | } |
||
| 244 | if ($street != "") { |
||
| 245 | $label .= "$street\r\n"; |
||
| 246 | } |
||
| 247 | if ($zip != "") { |
||
| 248 | $label .= "$zip "; |
||
| 249 | } |
||
| 250 | if ($city != "") { |
||
| 251 | $label .= "$city\r\n"; |
||
| 252 | } |
||
| 253 | if ($region != "") { |
||
| 254 | $label .= "$region\r\n"; |
||
| 255 | } |
||
| 256 | if ($country != "") { |
||
| 257 | $country .= "$country\r\n"; |
||
| 258 | } |
||
| 259 | |||
| 260 | $this->properties["LABEL;$type;" . $this->encoding] = encode($label); |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Add a e-mail address to this vCard |
||
| 265 | * |
||
| 266 | * @param string $address E-mail address |
||
| 267 | * @param string $type (optional) The type of the e-mail (typical "PREF" or "INTERNET") |
||
| 268 | * @return void |
||
| 269 | */ |
||
| 270 | public function setEmail($address, $type = "") |
||
| 271 | { |
||
| 272 | $key = "EMAIL"; |
||
| 273 | if ($type == "PREF") { |
||
| 274 | $key .= ";PREF=1"; |
||
| 275 | } elseif (!empty($type)) { |
||
| 276 | $key .= ";TYPE=" . dol_strtolower($type); |
||
| 277 | } |
||
| 278 | $this->properties[$key] = $address; |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * mise en forme de la note |
||
| 283 | * |
||
| 284 | * @param string $note Note |
||
| 285 | * @return void |
||
| 286 | */ |
||
| 287 | public function setNote($note) |
||
| 288 | { |
||
| 289 | $this->properties["NOTE;" . $this->encoding] = encode($note); |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * mise en forme de la fonction |
||
| 294 | * |
||
| 295 | * @param string $title Title |
||
| 296 | * @return void |
||
| 297 | */ |
||
| 298 | public function setTitle($title) |
||
| 299 | { |
||
| 300 | $this->properties["TITLE;" . $this->encoding] = encode($title); |
||
| 301 | } |
||
| 302 | |||
| 303 | |||
| 304 | /** |
||
| 305 | * mise en forme de la societe |
||
| 306 | * |
||
| 307 | * @param string $org Org |
||
| 308 | * @return void |
||
| 309 | */ |
||
| 310 | public function setOrg($org) |
||
| 311 | { |
||
| 312 | $this->properties["ORG;" . $this->encoding] = encode($org); |
||
| 313 | } |
||
| 314 | |||
| 315 | |||
| 316 | /** |
||
| 317 | * mise en forme du logiciel generateur |
||
| 318 | * |
||
| 319 | * @param string $prodid Prodid |
||
| 320 | * @return void |
||
| 321 | */ |
||
| 322 | public function setProdId($prodid) |
||
| 323 | { |
||
| 324 | $this->properties["PRODID"] = encode($prodid); |
||
| 325 | } |
||
| 326 | |||
| 327 | |||
| 328 | /** |
||
| 329 | * mise en forme du logiciel generateur |
||
| 330 | * |
||
| 331 | * @param string $uid Uid |
||
| 332 | * @return void |
||
| 333 | */ |
||
| 334 | public function setUID($uid) |
||
| 335 | { |
||
| 336 | $this->properties["UID"] = encode($uid); |
||
| 337 | } |
||
| 338 | |||
| 339 | |||
| 340 | /** |
||
| 341 | * mise en forme de l'url |
||
| 342 | * |
||
| 343 | * @param string $url URL |
||
| 344 | * @param string $type Type |
||
| 345 | * @return void |
||
| 346 | */ |
||
| 347 | public function setURL($url, $type = "") |
||
| 348 | { |
||
| 349 | // $type may be WORK | HOME |
||
| 350 | $key = "URL"; |
||
| 351 | if ($type != "") { |
||
| 352 | $key .= ";$type"; |
||
| 353 | } |
||
| 354 | $this->properties[$key] = $url; |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * permet d'obtenir une vcard |
||
| 359 | * |
||
| 360 | * @return string |
||
| 361 | */ |
||
| 362 | public function getVCard() |
||
| 363 | { |
||
| 364 | $text = "BEGIN:VCARD\r\n"; |
||
| 365 | $text .= "VERSION:4.0\r\n"; // With V4, all encoding are UTF-8 |
||
| 366 | //$text.= "VERSION:2.1\r\n"; |
||
| 367 | foreach ($this->properties as $key => $value) { |
||
| 368 | $newkey = preg_replace('/-.*$/', '', $key); // remove suffix -twitter, -facebook, ... |
||
| 369 | $text .= $newkey . ":" . $value . "\r\n"; |
||
| 370 | } |
||
| 371 | $text .= "REV:" . date("Ymd") . "T" . date("His") . "Z\r\n"; |
||
| 372 | //$text .= "MAILER: Dolibarr\r\n"; |
||
| 373 | $text .= "END:VCARD\r\n"; |
||
| 374 | return $text; |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * permet d'obtenir le nom de fichier |
||
| 379 | * |
||
| 380 | * @return string Filename |
||
| 381 | */ |
||
| 382 | public function getFileName() |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Return a VCARD string |
||
| 389 | * See RFC https://datatracker.ietf.org/doc/html/rfc6350 |
||
| 390 | * |
||
| 391 | * @param Object $object Object (User or Contact) |
||
| 392 | * @param Societe|null $company Company. May be null |
||
|
|
|||
| 393 | * @param Translate $langs Lang object |
||
| 394 | * @param string $urlphoto Full public URL of photo |
||
| 395 | * @return string String |
||
| 396 | */ |
||
| 397 | public function buildVCardString($object, $company, $langs, $urlphoto = '') |
||
| 551 | } |
||
| 552 | |||
| 553 | |||
| 554 | /* Example from Microsoft Outlook 2019 |
||
| 555 | |||
| 556 | BEGIN:VCARD |
||
| 557 | VERSION:2.1 |
||
| 558 | |||
| 559 | N;LANGUAGE=de:surename;forename;secondname;Sir;jun. |
||
| 560 | FN:Sir surename secondname forename jun. |
||
| 561 | ORG:Companyname |
||
| 562 | TITLE:position |
||
| 563 | TEL;WORK;VOICE:work-phone-number |
||
| 582 |