artnum / bizcuit-bexio
使用token认证和cURL访问Bexio.com的PHP API
v0.2.0-alpha
2023-11-03 12:35 UTC
Requires
- php: >=8.2
Requires (Dev)
- vimeo/psalm: ^5.15
This package is auto-updated.
Last update: 2024-09-22 13:44:20 UTC
README
这个库允许访问Bexio API。它围绕cURL构建,使用token认证,并提供大量功能。
它抽象了不同的API版本(例如4年内的4个版本)以使每个端点看起来相同。它通过类组合来实现这一点,例如
class BexioContact extends BexioAPI { protected $type = 'contact'; protected $class = 'BizCuit\BXObject\Contact'; protected $query = 'BizCuit\BXQuery\Contact'; use tBexioV2Api, tBexioObject, tBexioCollection; }
仍在积极开发中,它还提供工具来构建短期(减轻速率限制)和长期(只读)数据缓存,以便在服务上游服务中断时继续工作。
认可
我在开发这个库并不意味着我认可Bexio产品。我之所以这么做是因为我需要它,但如果我有选择,我不会使用bexio。支持相当傲慢,并尽可能多地驱赶像我这样的开发者,应用程序非常慢(通过API获取一个项目需要等待500毫秒,即使从官方前端,你花费的时间比工作时间还要多)而且很贵。
安装
通过composer composer require "artnum/bizcuit-bexio @dev"
文档
文档可在https://bizcuit.ch找到
使用示例
$context = new BizCuit\BexioCTX($bexio_api_token); $bills = new BizCuit\BexioBills($context); $files = new BizCuit\BexioFile($context); $billQuery = $bills->newQuery(); $billQuery->add('status', 'DRAFT'); $billInDraft = $bills->search($billQuery); foreach($billInDraft as $bill) { foreach($bill->attachment_ids as $fileUUID) { $fileWithContent = $file->get($fileUUID); } }
可用类
每个类代表一个端点,添加更多类相当容易,我只是没有花时间,以后会随着使用添加。
- BexioCountry
- BexioQuote
- BexioInvoice
- BexioOrder
- BexioContact
- BexioProject
- BexioContactRelation
- BexioAdditionalAddress
- BexioNote
- BexioUser
- BexioBusinessActivity
- BexioSalutation
- BexioTitle
- BexioProjectType
- BexioProjectStatus
- BexioBills
- BexioFile
为端点创建新类
为了添加只读类,你可以用ROObject定义它,例如,BexioProjectStatus
class BexioProjectStatus extends BexioAPI { protected $type = 'pr_project_state'; protected $class = 'BizCuit\BXObject\ROObject'; protected $query = 'BizCuit\BXQuery\ROObject'; use tBexioV2Api, tBexioObject, tBexioCollection; }
为了创建完整的类,你需要定义对象类和查询类(但你可以有带有完整类型对象类的查询类ROObject),例如BexioContact(选择因为它在API中引发了错误,并且根据我最后一次与公司的通话:"API不是战略性的",这意味着他们并不真正关心这一点)。
/* file bxobject.php */ class Contact extends BXObject { const NR = null; const createProperties = [ 'nr', 'contact_type_id', 'name_1', 'name_2', 'salutation_id', 'salutation_form', 'titel_id', 'birthday', 'address', 'postcode', 'city', 'country_id', 'mail', 'mail_second', 'phone_fixed', 'phone_fixed_second', 'phone_mobile', 'fax', 'url', 'skype_name', 'remarks', 'language_id', 'contact_group_ids', 'contact_branch_ids', 'user_id', 'owner_id' ]; /* API BUG when requesting a contact, if no salutation are set, the result * will have salutation_id = 0, but when sending back to update, edit or * replace the contact, the server complains with this value not being to * "null", so this is to fix that. */ const nullableProperties = [ 'salutation_id' ]; } /* file bxquery.php */ class Contact extends BXQuery { function __construct() { parent::__construct([ 'id', 'name_1', 'name_2', 'nr', 'address', 'mail', 'mail_second', 'postcode', 'city', 'country_id', 'contact_group_ids', 'contact_type_id', 'updated_at', 'user_id', 'phone_fixed', 'phone_mobile', 'fax' ]); } } /* file bexio.php */ class BexioContact extends BexioAPI { protected $type = 'contact'; protected $class = 'BizCuit\BXObject\Contact'; protected $query = 'BizCuit\BXQuery\Contact'; use tBexioV2Api, tBexioObject, tBexioCollection; }
可用于类组合的可用特性
tBexioObject
- function new ():BXObject (创建此类型的新对象)
- function delete(Int|String|BXObject $id): Bool (删除对象)
- function get (Int|String|BXObject $id, array $options = []):BXObject (获取对象)
- function set (BXObject $content):BXObject|false (保存对象)
- function update (BXObject $content):BXObject|false (更新对象)
tBexioCollection
- function getIdName ():string (获取用于此集合的id属性名称)
- function newQuery ():BXquery (为此集合准备查询对象)
- function search (BXQuery $query, Int $offset = 0, Int $limit = 500):array (在集合上执行搜索)
- function list (Int $offset = 0, Int $limit = 500):array (列出集合)
tBexioV2Api, tBexioV3Api和tBexioV4Api
仅用于设置正确的API编号
tBexioArchiveable
- function archive (BXObject $content):bool (存档对象)
- function unarchive (BXObject $content):bool (取消存档对象)
贝克西编号对象
- function getByNumber (Int|String $id): BXObject (通过编号获取对象,而不是通过ID)
贝克西PDF对象
- function getPDF(Int|BXObject $id): BXObject (获取对象的PDF文件)
贝克西项目对象
- function listByProject (Int|BXObject $projectId): Array (按项目ID列出)