Accessing the array from PHP Client library for Exact Online

871 views Asked by At

In order to store orders placed by customers with certain information from the WooCommerce REST API to the Exact Online dashboard via REST API. I had used PHP Client library for Exact Online.

While there was an issue -- after storing the data to Exact Online couldn't able to access the particular fields from an array. Specifically, when an REST API call has been made with

 /api/v1/{division}/bulk/CRM/Accounts?
 $filter=ID eq Email eq'[email protected]'&$select=ID,Email

OR

$url = "https://start.exactonline.de/api/v1/" + mandant.number + "/crm/Accounts?
$filter=Email eq '" + orderitem.email + "'&$select=ID,Code";

Here you will the below code

$customer = [
        'address'       => 'No.22/N, 91 Cross, XYZ Street, ABC Road',
        'address2'      => 'DEF',
        'city'          => 'GHI',
        'customerid'    => '999',
        'country'       => 'DE',
        'name'          => 'Nishanth',
        'zipcode'       => '123456'
];


// Create a new account

$account->AddressLine1 = $customer['address'];
$account->AddressLine2 = $customer['address2'];
$account->City = $customer['city'];
$account->Code = $customer['customerid'];
$account->Country = $customer['country'];
$account->IsSales = 'true';
$account->Name = $customer['name'];
$account->Postcode = $customer['zipcode'];
$account->Email = '[email protected]';
$account->Status = 'C';
$account->save();

This was the result obtained after filtering with Email ID

echo '<pre>'; print_r($account->filter("Email eq '[email protected]'"));


[attributes:protected] => Array
 (
    [Accountant] => 
    [AddressLine1] => No.22/N, 91 Cross, XYZ Street, ABC Road
    [AddressLine2] => DEF
    [City] => GHI
    [Code] => 1000
    [ConsolidationScenario] => 6
    [Country] => DE 
    [CountryName] => Germany
    [Created] => /Date(1555764341137)/
    [Creator] => 5bbfbd93-52f1-4f4b-b34e-fd213e479f8e
    [CreatorFullName] => Nishanth
    [Division] => 54810
    [Email] => [email protected]
    [ID] => bb124287-647c-4267-bd60-004efa1302aa
    [LogoThumbnailUrl] => https://start.exactonline.de//docs/images/placeholder_account_myeol.png
    [LogoUrl] => https://start.exactonline.de//docs/images/placeholder_account_myeol.png
    [Modified] => /Date(1555764341137)/
    [Modifier] => 5bbfbd93-52f1-4f4b-b34e-fd213e479f8e
    [ModifierFullName] => Nishanth
    [Name] => Nishanth Jay
    [Postcode] => 123456
 )

Making use of below built-In functions such as,

public function find()
{
    $result = $this->connection()->get($this->url);
    return new self($this->connection(), $result);
}
public function findWithSelect($select = '')
{
    $result = $this->connection()->get($this->url, [
        '$select' => $select
    ]);

    return new self($this->connection(), $result);
}

Despite calling with the built-In functions below:

$Accounts->filter("Email eq '[email protected]'")[0]->findWithSelect('ID');

$checkAccount = $Accounts->filter("Email eq '[email protected]'");
$checkAccount[0]->find('ID');
$checkAccount[0]->findWithSelect('Code'); 

The above built in library resulted in

[attributes:protected] => Array
 (
 )

How should I need to access the particular attributes from an array?

Likewise, retrieving the CurrentDivision from the Library was made ease with calls such as $getCurrentDivision->findWithSelect('CurrentDivision'); or with $getDivision->CurrentDivision;

Why couldn't the same function wouldn't work after retrieving an array?

2

There are 2 answers

0
Nɪsʜᴀɴᴛʜ ॐ On

In order to access the protected attributes from an array you will find in the below piece of snippets

$checkAccount = $Accounts->filter("Email eq '[email protected]'");
$getAccountId = $checkAccount[0]->ID;
$getAccountCode = $checkAccount[0]->Code;

Since I had tried with findWithSelect() and find():

$Accounts->filter("Email eq '[email protected]'")[0]->findWithSelect('ID');

But didn't yield any values resulted in blank values

2
Crayons On

You can use the closure bind method of accessing private or protected properties of an object. Here's an example static function.

class Helper {

    /**
    * Helper method to access private
    * or protected properties of an object
    * using the closure bind method
    */
    public static function accessPrivate($object, $property) {
        $bind = Closure::bind(
            function($prop) {
                return $this->$prop;
            },
            $object,
            $object
        );
        return $bind($property);
    }
}

Useage:

$code = Helper::accessPrivate($checkAccount, "Code");