Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base modifications #144

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion src/ApiConnectors/ArticleApiConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function get(string $code, Office $office): Article
// Make a request to read a single Article. Set the required values
$request_article = new Request\Read\Article();
$request_article
->setOffice($office->getCode())
->setOffice($office)
->setCode($code);

// Send the Request document and set the response to this instance.
Expand Down
81 changes: 80 additions & 1 deletion src/ApiConnectors/BaseApiConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ public function __construct(AuthenticatedConnection $connection)
$this->connection = $connection;
}

/**
* Will return the current connection
*
* @return \PhpTwinfield\Secure\AuthenticatedConnection
* @throws Exception
*/
public function getConnection(): AuthenticatedConnection
{
return $this->connection;
}

/**
* @see sendXmlDocument()
* @throws Exception
Expand Down Expand Up @@ -161,4 +172,72 @@ protected function getFinderService(): FinderService
{
return $this->connection->getAuthenticatedClient(Services::FINDER());
}
}

/**
* Convert options array to an ArrayOfString which is accepted by Twinfield.
*
* In some cases you are not allowed to change certain options (such as the dimtype, which should always be DEB when using CustomerApiConnector->ListAll()),
* in which case the $forcedOptions parameter will be set by the ApiConnector for this option, which will override any user settings in $options
*
* @param array $options
* @param array $forcedOptions
* @return array
* @throws Exception
*/
public function convertOptionsToArrayOfString(array $options, array $forcedOptions = []): array {
if (isset($options['ArrayOfString'])) {
return $options;
}

$optionsArrayOfString = ['ArrayOfString' => []];

foreach ($forcedOptions as $key => $value) {
unset($options[$key]);
$optionsArrayOfString['ArrayOfString'][] = array($key, $value);
}

foreach ($options as $key => $value) {
$optionsArrayOfString['ArrayOfString'][] = array($key, $value);
}

return $optionsArrayOfString;
}

/**
* Map the response of a listAll to an array of the requested class
*
* Is used by child ApiConnectors to map a $data object received by the FinderService to one or more new entities of $objectClass
* using the methods and attributes in $methodToAttributeMap. Returns an array of $objectClass objects
*
* @param string $objectClass
* @param $data
* @param array $methodToAttributeMap
* @return array
* @throws Exception
*/
public function mapListAll(string $objectClass, $data, array $methodToAttributeMap): array {
if ($data->TotalRows == 0) {
return [];
}

$objects = [];

foreach ($data->Items->ArrayOfString as $responseArrayElement) {
$object = new $objectClass();

if (isset($responseArrayElement->string[0])) {
$elementArray = $responseArrayElement->string;
} else {
$elementArray = $responseArrayElement;
}

foreach ($methodToAttributeMap as $key => $method) {
$object->$method($elementArray[$key]);
}

$objects[] = $object;
}

return $objects;
}
}
2 changes: 1 addition & 1 deletion src/ApiConnectors/CustomerApiConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function get(string $code, Office $office): Customer
// Make a request to read a single customer. Set the required values
$request_customer = new Request\Read\Customer();
$request_customer
->setOffice($office->getCode())
->setOffice($office)
->setCode($code);

// Send the Request document and set the response to this instance.
Expand Down
2 changes: 1 addition & 1 deletion src/ApiConnectors/InvoiceApiConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function get(string $code, string $invoiceNumber, Office $office)
$request_invoice
->setCode($code)
->setNumber($invoiceNumber)
->setOffice($office->getCode());
->setOffice($office);

// Send the Request document and set the response to this instance
$response = $this->sendXmlDocument($request_invoice);
Expand Down
2 changes: 1 addition & 1 deletion src/ApiConnectors/SupplierApiConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function get($code, Office $office): Supplier
// Make a request to read a single customer. Set the required values
$request_customer = new Request\Read\Supplier();
$request_customer
->setOffice($office->getCode())
->setOffice($office)
->setCode($code);

$response = $this->sendXmlDocument($request_customer);
Expand Down
7 changes: 3 additions & 4 deletions src/BaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @author Jop peters <[email protected]>
*/
abstract class BaseObject
abstract class BaseObject implements HasMessageInterface
{
private $result;
private $messages;
Expand All @@ -31,12 +31,11 @@ public function getMessages()
return $this->messages;
}

public function addMessage(Message $message)
public function addMessage(Message $message): void
{
$this->messages[] = $message;

return $this;
}

public function setMessages($messages)
{
$this->messages = $messages;
Expand Down
18 changes: 15 additions & 3 deletions src/DomDocuments/BaseDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PhpTwinfield\DomDocuments;

use PhpTwinfield\Enums\PerformanceType;
use PhpTwinfield\HasMessageInterface;
use PhpTwinfield\Office;
use PhpTwinfield\Transactions\TransactionFields\FreeTextFields;
use PhpTwinfield\Transactions\TransactionFields\StartAndCloseValueFields;
Expand Down Expand Up @@ -156,13 +157,24 @@ protected function appendPerformanceTypeFields(\DOMElement $element, $object): v
* Use this instead of createElement().
*
* @param string $tag
* @param string $textContent
* @param string|null $textContent
* @param HasMessageInterface|null $object
* @param array $methodToAttributeMap
* @return \DOMElement
*/
final protected function createNodeWithTextContent(string $tag, string $textContent): \DOMElement
final protected function createNodeWithTextContent(string $tag, ?string $textContent, ?HasMessageInterface $object = null, array $methodToAttributeMap = []): \DOMElement
{
$element = $this->createElement($tag);
$element->textContent = $textContent;

if ($textContent != null) {
$element->textContent = $textContent;
}

if (isset($object)) {
foreach ($methodToAttributeMap as $attributeName => $method) {
$element->setAttribute($attributeName, $object->$method());
}
}

return $element;
}
Expand Down
16 changes: 16 additions & 0 deletions src/HasCodeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace PhpTwinfield;

/**
* Provides an interface for Objects with getCode functionality
*
*/
interface HasCodeInterface
{
/**
* Gets the code from an object
*/
public function getCode(): ?string;

}
20 changes: 20 additions & 0 deletions src/HasMessageInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace PhpTwinfield;

use PhpTwinfield\Message\Message;

/**
* Provides an interface for BaseObject
*
* @see BaseObject
* @see Message
*
*/
interface HasMessageInterface
{
/**
* Adds an error message to an object
*/
public function addMessage(Message $message): void;
}
Loading