Skip to content

Xml Helper

Michael Newman edited this page Jul 28, 2017 · 2 revisions

Included in the Xero wrapper package is an XML Helper Trait.

There are two helper methods available; one for a single resource and one for multiple resources. Each method converts an array into an XML string.

Setup

<?php

namespace App\Http\Controllers\XeroApplication;

use Xero\helpers\XmlHelper;

class XeroAppController
{
    use XmlHelper;

    //do something here
}

Single Resource

$newInvoiceArray = [   
    'Type' => 'ACCREC',
    'Contact' => [
        'ContactID' => 'e32e2130-3d27-443a-8313-48fffa03cf53'
    ],
    'Date' => '2017-03-28',
    'LineAmountTypes' => 'Exclusive',
    'LineItems' => [
        'LineItem' => [
            'Description' => 'Line Item 1',
            'Quantity' => 10.000,
            'UnitAmount' => 11.00,
            'AccountCode' => 200
        ]
    ]
];

return $this->xml_one_resource($newInvoiceArray, 'Invoice');
Returned XML
<Invoice>
    <Type>ACCREC</Type>
    <Contact>
           <ContactID>e32e2130-3d27-443a-8313-48fffa03cf53</ContactID>
    </Contact>
    <LineAmountTypes>Exclusive</LineAmountTypes>
    <LineItems>
        <Description>Line Item 1</Description>
        <Quantity>10.000</Quantity>
        <UnitAmount>11.0</UnitAmount>
        <AccountCode>200</AccountCode>
    </LineItems>
</Invoice>

Multiple Resources

$newInvoicesArray = [
    [
        'Type' => 'ACCREC',
        'Contact' => [
            'ContactID' => 'e32e2130-3d27-443a-8313-48fffa03cf53'
        ],
        'Date' => '2017-03-28',
        'LineAmountTypes' => 'Exclusive',
        'LineItems' => [
            'LineItem' => [
                'Description' => 'Line Item 1',
                'Quantity' => 10.000,
                'UnitAmount' => 11.00,
                'AccountCode' => 200
            ]
        ]
    ],
    [
        'Type' => 'ACCREC',
        'Contact' => [
            'ContactID' => 'e32e2130-3d27-443a-8313-48fffa03cf53'
        ],
        'Date' => '2017-03-28',
        'LineAmountTypes' => 'Exclusive',
        'LineItems' => [
            'LineItem' => [
                'Description' => 'Line Item 1',
                'Quantity' => 10.000,
                'UnitAmount' => 11.00,
                'AccountCode' => 200
            ]
        ]
    ]
];

return $this->xml_multiple_resources($newInvoiceArray, 'Invoice');
Returned XML
<Invoices>
    <Invoice>
        <Type>ACCREC</Type>
        <Contact>
               <ContactID>e32e2130-3d27-443a-8313-48fffa03cf53</ContactID>
        </Contact>
        <LineAmountTypes>Exclusive</LineAmountTypes>
        <LineItems>
            <Description>Line Item 1</Description>
            <Quantity>10.000</Quantity>
            <UnitAmount>11.0</UnitAmount>
            <AccountCode>200</AccountCode>
        </LineItems>
    </Invoice>
    <Invoice>
            <Type>ACCREC</Type>
            <Contact>
                   <ContactID>e32e2130-3d27-443a-8313-48fffa03cf53</ContactID>
            </Contact>
            <LineAmountTypes>Exclusive</LineAmountTypes>
            <LineItems>
                <Description>Line Item 1</Description>
                <Quantity>10.000</Quantity>
                <UnitAmount>11.0</UnitAmount>
                <AccountCode>200</AccountCode>
            </LineItems>
        </Invoice>
</Invoices>
Clone this wiki locally