daursu / xero
一个优雅的 Laravel 4 对官方 Xero API 的包装。
Requires
- php: >=5.4.0
- illuminate/support: ~4.1
Requires (Dev)
- mockery/mockery: ~0.9
This package is auto-updated.
Last update: 2024-09-10 10:00:22 UTC
README
这是一个官方 Xero API PHP 库的包装器,可在以下位置找到:https://github.com/XeroAPI/XeroOAuth-PHP
注意:我没有实现整个 API,而是创建了核心逻辑,然后可以扩展(见下面的示例)。
我只测试了与私有应用程序一起使用此库,但它应该适用于其他应用程序。
安装
使用 composer,只需将其添加到您的 composer.json 文件中
"require": {
"daursu/xero": "dev-master"
}
使用 composer 安装此包。
$ composer update
注册包
在 app/config/app.php
中的 providers
数组中注册服务提供者
'providers' => array( // ... 'Daursu\Xero\XeroServiceProvider', )
发布配置文件
php artisan config:publish daursu/xero
这应该在 app/config/packages/daursu/xero/config.php
中创建一个新文件。使用自己的设置和 API 密钥更新此文件。还有一个名为 certs
的文件夹,我建议您将证书放在那里。
以下是生成您的公钥/私钥的指南 http://developer.xero.com/documentation/advanced-docs/public-private-keypair/
用法
语法与 Laravel Eloquent 非常相似。
use \Daursu\Xero\Models\Invoice; use \Daursu\Xero\Models\Contact; // Retrieve all the invoices $invoices = Invoice::get(); foreach ($invoices as $invoice) { print_r($invoice->toArray()); print_r($invoice->getId()); print_r($invoice->InvoiceID); } // Retrive a single invoice $invoice = Invoice::find("0263f2bd-5825-476b-b6cf-6b76896a8cff"); var_dump($invoice); // The get method also takes additional parameters $contact = Contact::get(array('where' => 'Name.Contains("Dan")'));
创建或更新记录
这也相当简单。
use \Daursu\Xero\Models\Invoice; use \Daursu\Xero\Models\Contact; // Initialize from an array $invoice = new Invoice(array( 'Type' => 'ACCREC', 'Status' => 'DRAFT', 'Date' => date('Y-m-d'), ... )); // Now you will need to attach a contact to the invoice // Note that this time I am not passing an array to the constructor, // this is just another way you can initialize objects $contact = new Contact(); $contact->Name = "John Smith"; // Now you can assign it like this $invoice->Contact = $contact; // or $invoice->setRelationship($contact); // Save the invoice $invoice->save(); // returns true or false // Other methods $invoice->update(); $invoice->create(); print_r($invoice->toArray()); // should have all the properties populated once it comes back from Xero
集合
当您需要指定多个关系时(例如,一个联系人有多个地址)使用集合。
use \Daursu\Xero\Models\Contact; use \Daursu\Xero\Models\Address; $contact = new Contact; $contact->name = "John"; // IMPORTANT: A collection can only contain a single type of model // in this case it can only hold addresses. $collection = Address::newCollection(array( array('AddressType' => 'NEW', 'AddressLine1' => 'Cambridge', 'AddressLine2' => 'England'), array('AddressType' => 'OTHER', 'AddressLine1' => 'London', 'AddressLine2' => 'England'), )); // Push an new item $collection->push(array('AddressType' => 'STREET', 'AddressLine1' => 'Street', 'AddressLine2' => 'England')); // Push an existing object $address = new Address(array('AddressType' => 'OBJECT', 'AddressLine1' => 'Oxford', 'AddressLine2' => 'England')); $collection->push($address); // Now set the relationship $contact->setRelationship($collection); // Or like this $contact->Addresses = $collection; // Save the contact $contact->save();
输出方法
// You can output an object using different methods $address->toArray(); $address->toJson(); $address->toXML();
扩展库
我没有实现 Xero 提供的所有模型,但是实现起来非常简单。以下是一个添加名为 CreditNote
的新模型的示例。
// File CreditNote.php <?php namespace Daursu\Xero\Models; class CreditNote extends BaseModel { /** * The name of the primary column. * * @var string */ protected $primary_column = 'CreditNoteID'; }
就这样。您现在可以使用它了
use \Daursu\Xero\Models\CreditNote; use \Daursu\Xero\Models\Contact; $creditNote = new CreditNote(); $creditNote->Type = 'ACCPAYCREDIT'; $creditNote->Contact = new Contact(array("Name"=> "John"); $creditNote->save(); // Create a collection of credit notes $collection = CreditNote::newCollection(); $collection->push($creditNote);
如果您扩展了这个库,请随时分支并发送拉取请求。
变更日志
- 版本 0.2 - 修复命名空间以符合 PSR-0。修复了某些自动加载问题。
- 版本 0.1 - 首次发布
许可证 & 致谢
致谢官方 Xero API 库,可在以下位置找到:https://github.com/XeroAPI/XeroOAuth-PHP。
此代码受 MIT 许可证许可。您可以根据需要修改和分发。