Last active
September 5, 2018 12:43
-
-
Save malles/153446acba710281e2f45832fb16af65 to your computer and use it in GitHub Desktop.
Returns array of data from TwinfieldApi objects for json_encode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use PhpTwinfield\Customer; | |
use PhpTwinfield\CustomerAddress; | |
use PhpTwinfield\CustomerBank; | |
use PhpTwinfield\Exception; | |
use PhpTwinfield\Supplier; | |
use PhpTwinfield\SupplierAddress; | |
use PhpTwinfield\SupplierBank; | |
abstract class JsonSerializer { | |
protected const SUPPLIER_CLASS = 'PhpTwinfield\Supplier'; | |
protected const CUSTOMER_CLASS = 'PhpTwinfield\Customer'; | |
/** | |
* @param mixed $object | |
* @return array | |
* @throws Exception | |
*/ | |
public static function toArray ($object) { | |
$class = get_class($object); | |
switch ($class) { | |
case self::SUPPLIER_CLASS: | |
$data = self::serializeSupplier($object); | |
break; | |
case self::CUSTOMER_CLASS: | |
$data = self::serializeCustomer($object); | |
break; | |
default: | |
throw new Exception(sprintf('Class %s not in JsonSerializer', $class)); | |
break; | |
} | |
return $data; | |
} | |
/** | |
* @param Supplier $supplier | |
* @return array | |
*/ | |
public static function serializeSupplier(Supplier $supplier) { | |
$data = [ | |
'status' => $supplier->getStatus(), | |
'code' => $supplier->getCode(), | |
'uid' => $supplier->getUID(), | |
'name' => $supplier->getName(), | |
'inuse' => $supplier->getInUse(), | |
'behaviour' => $supplier->getBehaviour(), | |
'touched' => $supplier->getTouched(), | |
'beginperiod' => $supplier->getBeginPeriod(), | |
'endperiod' => $supplier->getEndPeriod(), | |
'endyear' => $supplier->getEndYear(), | |
'website' => $supplier->getWebsite(), | |
'editdimensionname' => $supplier->getEditDimensionName(), | |
'office' => $supplier->getOffice(), | |
'duedays' => $supplier->getDueDays(), | |
'payavailable' => $supplier->getPayAvailable(), | |
'paycode' => $supplier->getPayCode(), | |
'ebilling' => $supplier->getEBilling(), | |
'ebillmail' => $supplier->getEBillMail(), | |
'addresses' => [], | |
'banks' => [], | |
]; | |
foreach ($supplier->getAddresses() as $index => $address) { | |
/** @var SupplierAddress $address */ | |
$address_data = [ | |
'id' => $address->getID(), | |
'type' => $address->getType(), | |
'default' => $address->getDefault(), | |
'name' => $address->getName(), | |
'contact' => $address->getContact(), | |
'country' => $address->getCountry(), | |
'city' => $address->getCity(), | |
'postcode' => $address->getPostcode(), | |
'telephone' => $address->getTelephone(), | |
'telefax' => $address->getFax(), | |
'email' => $address->getEmail(), | |
'field1' => $address->getField1(), | |
'field2' => $address->getField2(), | |
'field3' => $address->getField3(), | |
'field4' => $address->getField4(), | |
'field5' => $address->getField5(), | |
'field6' => $address->getField6(), | |
]; | |
$data['addresses'][$index] = $address_data; | |
} | |
foreach ($supplier->getBanks() as $index => $bank) { | |
/** @var SupplierBank $bank */ | |
$address_data = [ | |
'id' => $bank->getID(), | |
'default' => $bank->getDefault(), | |
'ascription' => $bank->getAscription(), | |
'accountnumber' => $bank->getAccountnumber(), | |
'field2' => $bank->getAddressField2(), | |
'field3' => $bank->getAddressField3(), | |
'bankname' => $bank->getBankname(), | |
'biccode' => $bank->getBiccode(), | |
'city' => $bank->getCity(), | |
'country' => $bank->getCountry(), | |
'iban' => $bank->getIban(), | |
'natbiccode' => $bank->getNatbiccode(), | |
'postcode' => $bank->getPostcode(), | |
'state' => $bank->getState(), | |
]; | |
$data['banks'][$index] = $address_data; | |
} | |
return $data; | |
} | |
/** | |
* @param Customer $customer | |
* @return array | |
*/ | |
public static function serializeCustomer(Customer $customer) { | |
$data = [ | |
'status' => $customer->getStatus(), | |
'code' => $customer->getCode(), | |
'uid' => $customer->getUID(), | |
'name' => $customer->getName(), | |
'inuse' => $customer->getInUse(), | |
'behaviour' => $customer->getBehaviour(), | |
'touched' => $customer->getTouched(), | |
'beginperiod' => $customer->getBeginPeriod(), | |
'beginyear' => $customer->getBeginYear(), | |
'endperiod' => $customer->getEndPeriod(), | |
'endyear' => $customer->getEndYear(), | |
'website' => $customer->getWebsite(), | |
'editdimensionname' => $customer->getEditDimensionName(), | |
'office' => $customer->getOffice(), | |
'country' => $customer->getCountry(), | |
'duedays' => $customer->getDueDays(), | |
'payavailable' => $customer->getPayAvailable(), | |
'paycode' => $customer->getPayCode(), | |
'ebilling' => $customer->getEBilling(), | |
'ebillmail' => $customer->getEBillMail(), | |
'vatcode' => $customer->getVatCode(), | |
'mandate' => [], | |
'credit_management' => [], | |
'addresses' => [], | |
'banks' => [], | |
]; | |
if ($mandate = $customer->getCollectMandate()) { | |
$data['mandate'] = [ | |
'id' => $mandate->getID(), | |
'signaturedate' => $mandate->getSignatureDate(), | |
'firstrundate' => $mandate->getFirstRunDate(), | |
]; | |
} | |
if ($creditManagement = $customer->getCreditManagement()) { | |
$data['credit_management'] = [ | |
'responsibleuser' => $creditManagement->getResponsibleUser(), | |
'basecreditlimit' => $creditManagement->getBaseCreditLimit(), | |
'sendreminder' => $creditManagement->getSendReminder(), | |
'reminderemail' => $creditManagement->getReminderEmail(), | |
'blocked' => $creditManagement->getBlocked(), | |
'freetext1' => $creditManagement->getFreeText1(), | |
'freetext2' => $creditManagement->getFreeText2(), | |
'freetext3' => $creditManagement->getFreeText3(), | |
'comment' => $creditManagement->getComment(), | |
]; | |
} | |
foreach ($customer->getAddresses() as $index => $address) { | |
/** @var CustomerAddress $address */ | |
$address_data = [ | |
'id' => $address->getID(), | |
'type' => $address->getType(), | |
'default' => $address->getDefault(), | |
'name' => $address->getName(), | |
'contact' => $address->getContact(), | |
'country' => $address->getCountry(), | |
'city' => $address->getCity(), | |
'postcode' => $address->getPostcode(), | |
'telephone' => $address->getTelephone(), | |
'telefax' => $address->getFax(), | |
'email' => $address->getEmail(), | |
'field1' => $address->getField1(), | |
'field2' => $address->getField2(), | |
'field3' => $address->getField3(), | |
'field4' => $address->getField4(), | |
'field5' => $address->getField5(), | |
'field6' => $address->getField6(), | |
]; | |
$data['addresses'][$index] = $address_data; | |
} | |
foreach ($customer->getBanks() as $index => $bank) { | |
/** @var CustomerBank $bank */ | |
$address_data = [ | |
'id' => $bank->getID(), | |
'default' => $bank->getDefault(), | |
'ascription' => $bank->getAscription(), | |
'accountnumber' => $bank->getAccountnumber(), | |
'field2' => $bank->getAddressField2(), | |
'field3' => $bank->getAddressField3(), | |
'bankname' => $bank->getBankname(), | |
'biccode' => $bank->getBiccode(), | |
'city' => $bank->getCity(), | |
'country' => $bank->getCountry(), | |
'iban' => $bank->getIban(), | |
'natbiccode' => $bank->getNatbiccode(), | |
'postcode' => $bank->getPostcode(), | |
'state' => $bank->getState(), | |
]; | |
$data['banks'][$index] = $address_data; | |
} | |
return $data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment