jaredhowland/contacts

📇 PHP 地址簿联系人库(为联系人生成 vCard 文件)

v6.0.0 2023-09-11 20:30 UTC

README

Scrutinizer Build StyleCI Packagist-dl Packagist MIT License

联系人

Contacts 是一个用于创建包含个人或组织地址簿信息的文件的 PHP 库。目前,仅支持 vCard 3.0 格式导出。

功能

  • vCard 3.0 兼容性
    • 生成的 vCard 应与所有使用 vCard 的应用程序兼容
    • 包含 iOS 和 macOS 联系人应用特有的字段(SupervisorAnniversarySpouseChild
  • 灵活:未来的实现将包括其他常用存储联系信息的格式——其他 vCard 格式、microformats、Google 联系人格式、.csv 格式等。
  • 使用流畅接口构建联系人信息的方法链
  • 方法参数顺序通常直接来自 标准 本身

安装

Contacts 可作为 Composer 使用

  1. 如果需要,请安装 Composer
  2. 将以下内容添加到您的 composer.json 文件中
"require": {
   "jaredhowland/contacts": "~6.0"
}

用法

输入

这是一个详尽的示例。代码具有良好的文档说明,您应该能够在 IDE 中获得使用它所需的所有提示。在大多数情况下,您只需要这些字段的一小部分来创建 vCard。

<?php
   require 'vendor/autoload.php';

   use \Contacts\Options; // Only needed if you must override the default settings
   use \Contacts\Vcard;

   // Set desired options
   $options = new Options();
   $options->setDataDirectory('./'); // Tell app where to save `.vcf` file

   $vcard = new Vcard($options);
   $vcard->addFullName('Jane Doe');
   $vcard->addName('Doe', 'Jane');
   $vcard->addNickname('Janie, Jan');
   $vcard->addPhoto('https://raw.githubusercontent.com/jaredhowland/contacts/master/tests/files/photo.jpg');
   $vcard->addBirthday(10, 2, null);
   $vcard->addAddress(null, null, '123 Main', 'Provo', 'UT', '84602', 'United States', ['dom', 'postal', 'parcel', 'work']);
   $vcard->addAddress(null, null, '123 Main', 'Provo', 'UT', '84602', 'United States', ['dom', 'postal', 'parcel', 'home']);
   $vcard->addLabel('Jane Doe\n123 Main St\nProvo, UT 84602', ['dom', 'parcel']);
   $vcard->addTelephone('555-555-5555', ['cell', 'iphone']);
   $vcard->addEmail('jane_doe@domain.com');
   $vcard->addTimeZone('-7');
   $vcard->addLatLong(40.3333331, -111.7777775);
   $vcard->addTitle('System Administrator');
   $vcard->addRole('Programmer');
   // $vcard->addAgent($agent); NOT SUPPORTED
   $vcard->addOrganizations(['Awesome Company']);
   $vcard->addCategories(['School', 'Work']);
   $vcard->addNote('Not much is known about Jane Doe.');
   $vcard->addSortString('Doe');
   // $vcard->addSound($sound); NOT SUPPORTED
   $vcard->addUrl('https://www.example.com');
   // $vcard->addKey($key); NOT SUPPORTED
   $vcard->addAnniversary('2010-10-10');
   $vcard->addSupervisor('Jane Smith');
   $vcard->addSpouse('John Doe');
   $vcard->addChild('Jeff Doe');
   $vcard->addChild('Lisa Doe');
   $vcard->addExtendedType('TWITTER', '@jared_howland');
   $vcard->addUniqueIdentifier();
   $vcard->addRevision('2023-09-05'); // Added automatically if you don't call this method

   $directory->buildVcard(true, 'example');
?>

或者,您可以链接方法来构建 vCard

 <?php
   require '../vendor/autoload.php';

   use \Contacts\Options;
   use \Contacts\Vcard;

   // Set desired options
   $options = new Options();
   $options->setDataDirectory('./'); // Tell app where to save `.vcf` file

   $vcard = new Vcard($options);
   $vcard->addFullName('Jane Doe')
         ->addName('Doe', 'Jane')
         ->addNickname('Janie, Jan')
         ->addPhoto('https://raw.githubusercontent.com/jaredhowland/contacts/master/tests/files/photo.jpg')
         ->addBirthday(10, 2, null)
         ->addAddress(null, null, '123 Main', 'Provo', 'UT', '84602', 'United States', ['dom', 'postal', 'parcel', 'work'])
         ->addAddress(null, null, '123 Main', 'Provo', 'UT', '84602', 'United States', ['dom', 'postal', 'parcel', 'home'])
         ->addLabel('Jane Doe\n123 Main St\nProvo, UT 84602', ['dom', 'parcel'])
         ->addTelephone('555-555-5555', ['cell', 'iphone'])
         ->addEmail('jane_doe@domain.com')
         ->addTimeZone('-7')
         ->addLatLong(40.3333331, -111.7777775)
         ->addTitle('System Administrator')
         ->addRole('Programmer')
         ->addOrganizations(['Awesome Company'])
         ->addCategories(['School', 'Work'])
         ->addNote('Not much is known about Jane Doe.')
         ->addSortString('Doe')
         ->addUrl('http://www.example.com')
         ->addAnniversary('2010-10-10')
         ->addSupervisor('Jane Smith')
         ->addSpouse('John Doe')
         ->addChild('Jeff Doe')
         ->addChild('Lisa Doe')
         ->addExtendedType('TWITTER', '@jared_howland')
         ->addUniqueIdentifier()
         ->addRevision('2023-09-05') /* Added automatically with the current date and time if you don't call this method */
         ->buildVcard(true, 'example');
   // $vcard->addAgent($agent); NOT SUPPORTED
   // $vcard->addSound($sound); NOT SUPPORTED
   // $vcard->addKey($key); NOT SUPPORTED
?>

输出

BEGIN:VCARD
VERSION:3.0
FN:Jane Doe
N:Doe;Jane;;;
NICKNAME:Janie,Jan
PHOTO;ENCODING=b;TYPE=JPEG:/9j/4QBwRXhpZgAASUkqAAgAAAABAJiCAgBLAAAAGgAAAA
 AAAABDb3B5cmlnaHQgQllVIFB …rest of binary-encoded photo
BDAY;X-APPLE-OMIT-YEAR=1604:1604-02-10
ADR;TYPE=dom,postal,parcel,work:;;123 Main;Provo;UT;84602;United States
ADR;TYPE=dom,postal,parcel,home:;;123 Main;Provo;UT;84602;United States
LABEL;TYPE=dom,parcel:Jane Doe\n123 Main St\nProvo\, UT 84602
TEL;TYPE=cell,iphone:(555) 555-5555
EMAIL;TYPE=internet:jane_doe@domain.com
TZ:-07:00
GEO:40.333333;-111.777778
TITLE:System Administrator
ROLE:Programmer
ORG:Awesome Company
CATEGORIES:School,Work
NOTE:Not much is known about Jane Doe.
SORT-STRING:Doe
URL:https://www.example.com
item1.X-ABDATE;type=pref:2010-10-10
item1.X-ABLabel:_$!<Anniversary>!$_
item2.X-ABRELATEDNAMES:Jane Smith
item2.X-ABLabel:_$!<Manager>!$_
item3.X-ABRELATEDNAMES:John Doe
item3.X-ABLabel:_$!<Spouse>!$_
item4.X-ABRELATEDNAMES:Jeff Doe
item4.X-ABLabel:_$!<Child>!$_
item5.X-ABRELATEDNAMES:Lisa Doe
item5.X-ABLabel:_$!<Child>!$_
X-TWITTER:@jared_howland
UID:5a32a74023b097.12918287
REV:2023-09-05T00:00:00Z
END:VCARD

选项

Options 类中的可用选项和默认值

  • setDataDirectory: ./data/
  • setDefaultAreaCode: null
  • setFormatUsTelephone: true

使用适当的getter

  • getDataDirectory()
  • getDefaultAreaCode()
  • isFormatUsTelephone()

贡献

已知问题

  • 不支持 BDAY 字段的日期时间值(仅日期值)。没有计划实现。
  • 不支持 TZ 字段的文本值(仅 UTC 偏移量值)。没有计划实现。
  • 以下 vCard 元素目前不支持(没有计划实现)
    • AGENT
    • SOUND
    • KEY

https://github.com/jeroendesloovere/vcard 启发