2017 © Pedro Peláez
 

library xero

An elegant Laravel 4 wrapper for the official Xero API.

image

daursu/xero

An elegant Laravel 4 wrapper for the official Xero API.

  • Tuesday, October 6, 2015
  • by daursu
  • Repository
  • 4 Watchers
  • 2 Stars
  • 279 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 8 Forks
  • 4 Open issues
  • 4 Versions
  • 1 % Grown

The README.md

Xero API wrapper for Laravel 4

Latest Stable Version Total Downloads Latest Unstable Version License, (*1)

This is a wrapper for the official Xero API php library, found at: https://github.com/XeroAPI/XeroOAuth-PHP, (*2)

Note: I have not implemented the entire API, instead I have created the core logic which can then be extended (see the examples below)., (*3)

I have tested the library only with a Private app, but it should work for the others., (*4)


Installation

Using composer simply add this to your composer.json file:, (*5)

"require": {
  "daursu/xero": "dev-master"
}

Use composer to install this package., (*6)

$ composer update

Registering the Package

Register the service provider within the providers array found in app/config/app.php:, (*7)

'providers' => array(
    // ...

    'Daursu\Xero\XeroServiceProvider',
)

Publish the configuration file

php artisan config:publish daursu/xero

This should create a new file in app/config/packages/daursu/xero/config.php. Update this file with your own settings and API key. There is also a folder called certs, where I recommend you to put your certificates., (*8)

Here is a guide how to generate your public/private key http://developer.xero.com/documentation/advanced-docs/public-private-keypair/, (*9)

Usage

The syntax is very simillar to the Laravel Eloquent one., (*10)


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")'));

Create or update a record

This is pretty straight forward as well., (*11)

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

Collections

Collections are used when you need to specify multiple relationships (ie. A contact might have multiple addresses., (*12)

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();

Output methods


// You can output an object using different methods $address->toArray(); $address->toJson(); $address->toXML();

Extend the library

I have not implemented all the models that Xero provides, however it is very easy to implement. Here is an example of adding a new model called CreditNote., (*13)

// File CreditNote.php
<?php namespace Daursu\Xero\Models;

class CreditNote extends BaseModel {

    /**
     * The name of the primary column.
     *
     * @var string
     */
    protected $primary_column = 'CreditNoteID';

}

That's it. You can now use it:, (*14)

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);


Feel free to fork and send pull requests if you extend this library., (*15)

Changelog

  • Version 0.2 - Fix namespaces to adhere to PSR-0. Fixes certain autoload issues.
  • Version 0.1 - Initial release

License & Credits

Credits go to the official Xero API library found at https://github.com/XeroAPI/XeroOAuth-PHP., (*16)

This code is licensed under the MIT license. Feel free to modify and distribute., (*17)

http://www.softwareassistance.net, (*18)

http://www.computerassistance.uk.com, (*19)

The Versions

06/10 2015

dev-develop

dev-develop http://www.softwareassistance.net

An elegant Laravel 4 wrapper for the official Xero API.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dan Ursu

laravel illuminate xero payroll

29/06 2015

dev-master

9999999-dev http://www.softwareassistance.net

An elegant Laravel 4 wrapper for the official Xero API.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dan Ursu

laravel illuminate xero payroll

22/01 2015

v0.2

0.2.0.0 http://www.softwareassistance.net

An elegant Laravel 4 wrapper for the official Xero API.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dan Ursu

laravel illuminate xero payroll

06/11 2014

v0.1

0.1.0.0 http://www.softwareassistance.net

An elegant Laravel 4 wrapper for the official Xero API.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dan Ursu

laravel illuminate xero payroll