Skip to content

Commit 455960c

Browse files
committed
Added "Get Invoices" example
Unit Tests improved
1 parent 0944cf5 commit 455960c

File tree

7 files changed

+107
-316
lines changed

7 files changed

+107
-316
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/php -f
2+
<?php
3+
/**
4+
* IPEXB2B - Example how to get Invoices
5+
*
6+
* @author Vítězslav Dvořák <info@vitexsofware.cz>
7+
* @copyright (G) 2018 Vitex Software
8+
*/
9+
10+
namespace Example\IPEXB2B;
11+
12+
include_once './config.php';
13+
include_once '../vendor/autoload.php';
14+
15+
$grabber = new \IPEXB2B\ApiClient(null, ['section' => 'invoices']);
16+
$grabber->setUrlParams(['monthOffset' => -1]);
17+
18+
$response = $grabber->requestData('postpaid');
19+
if (count($response)) {
20+
echo json_encode($response, JSON_PRETTY_PRINT);
21+
$grabber->addStatusMessage(count($response).' Invoices', 'success');
22+
} else {
23+
$grabber->addStatusMessage('Obtaining Invoices failed', 'warning');
24+
}

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ clean:
1515
rm -rf debian/php-ipex-b2b-doc
1616
rm -rf debian/*.log
1717
rm -rf docs/*
18-
rm -rf vendor/*
18+
rm -rf vendor/* composer.lock
1919

2020
doc:
2121
debian/apigendoc.sh

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"require": {
1212
"php": ">= 5.6",
1313
"ext-curl": "*",
14-
"vitexsoftware/ease-framework": ">=1.3"
14+
"vitexsoftware/ease-framework": "dev-master"
1515
},
1616
"autoload": {
1717
"psr-4": {

debian/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "PHP Library for interaction with restapi.ipex.cz",
44
"homepage": "https://github.com/Spoje-NET/ipex-b2b",
55
"license": "GPL-2.0+",
6-
"version": "0.1",
6+
"version": "0.2",
77
"type": "library",
88
"authors": [
99
{

nbproject/private/private.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
3+
<coverage xmlns="http://www.netbeans.org/ns/code-coverage/1" enabled="true"/>
34
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
45
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
56
<group/>

src/IPEXB2B/ApiClient.php

Lines changed: 31 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,10 @@ class ApiClient extends \Ease\Brick
249249
protected $ignoreNotFound = false;
250250

251251
/**
252-
* Array of errors caused by last request
253-
* @var array
252+
* Token handling object live here
253+
* @var Token
254254
*/
255-
private $errors = [];
256-
protected $tokener = null;
255+
protected $tokener = null;
257256

258257
/**
259258
* Class for read only interaction with IPEX.
@@ -297,24 +296,6 @@ public function setUp($options = [])
297296
$this->updateApiURL();
298297
}
299298

300-
/**
301-
* Set up one of properties
302-
*
303-
* @param array $options array of given properties
304-
* @param string $name name of property to process
305-
* @param string $constant load default property value from constant
306-
*/
307-
public function setupProperty($options, $name, $constant = null)
308-
{
309-
if (isset($options[$name])) {
310-
$this->$name = $options[$name];
311-
} else {
312-
if (is_null($this->$name) && !empty($constant) && defined($constant)) {
313-
$this->$name = constant($constant);
314-
}
315-
}
316-
}
317-
318299
/**
319300
* Inicializace CURL
320301
*/
@@ -352,6 +333,7 @@ public function processInit($init)
352333
* Set section for communication
353334
*
354335
* @param string $section section pathName to use
336+
*
355337
* @return boolean section switching status
356338
*/
357339
public function setSection($section)
@@ -371,67 +353,16 @@ public function getSection()
371353
return $this->section;
372354
}
373355

374-
/**
375-
* Převede rekurzivně Objekt na pole.
376-
*
377-
* @param object|array $object
378-
*
379-
* @return array
380-
*/
381-
public static function object2array($object)
382-
{
383-
$result = null;
384-
if (is_object($object)) {
385-
$objectData = get_object_vars($object);
386-
if (is_array($objectData) && count($objectData)) {
387-
$result = array_map('self::object2array', $objectData);
388-
}
389-
} else {
390-
if (is_array($object)) {
391-
foreach ($object as $item => $value) {
392-
$result[$item] = self::object2array($value);
393-
}
394-
} else {
395-
$result = $object;
396-
}
397-
}
398-
399-
return $result;
400-
}
401-
402-
/**
403-
* Převede rekurzivně v poli všechny objekty na jejich identifikátory.
404-
*
405-
* @param object|array $object
406-
*
407-
* @return array
408-
*/
409-
public static function objectToID($object)
410-
{
411-
$resultID = null;
412-
if (is_object($object)) {
413-
$resultID = $object->__toString();
414-
} else {
415-
if (is_array($object)) {
416-
foreach ($object as $item => $value) {
417-
$resultID[$item] = self::objectToID($value);
418-
}
419-
} else { //String
420-
$resultID = $object;
421-
}
422-
}
423-
424-
return $resultID;
425-
}
426-
427356
/**
428357
* Připraví data pro odeslání do FlexiBee
429358
*
430359
* @param string $data
360+
*
361+
* @return string Data strored
431362
*/
432363
public function setPostFields($data)
433364
{
434-
$this->postFields = $data;
365+
return $this->postFields = $data;
435366
}
436367

437368
/**
@@ -468,40 +399,24 @@ public function sectionUrlWithSuffix($urlSuffix)
468399
return $sectionUrl;
469400
}
470401

471-
/**
472-
* Add params to url
473-
*
474-
* @param string $url originall url
475-
* @param array $params value to add
476-
* @param boolean $override replace already existing values ?
477-
*
478-
* @return string url with parameters added
479-
*/
480-
public function addUrlParams($url, $params, $override = false)
481-
{
482-
$urlParts = parse_url($url);
483-
$urlFinal = '';
484-
if (array_key_exists('scheme', $urlParts)) {
485-
$urlFinal .= $urlParts['scheme'].'://'.$urlParts['host'];
486-
}
487-
if (array_key_exists('path', $urlParts)) {
488-
$urlFinal .= $urlParts['path'];
489-
}
490-
if (array_key_exists('query', $urlParts)) {
491-
parse_str($urlParts['query'], $queryUrlParams);
492-
$urlParams = $override ? array_merge($params, $queryUrlParams) : array_merge($queryUrlParams,
493-
$params);
494-
} else {
495-
$urlParams = $params;
496-
}
497-
if (!empty($urlParams) && is_array($urlParams)) {
498-
$urlFinal .= '?'.http_build_query($urlParams);
402+
403+
/**
404+
* Add UrlParams to Requests URL
405+
*
406+
* @param array $urlParams
407+
*
408+
* @return array all urlParams
409+
*/
410+
public function setUrlParams($urlParams){
411+
if(is_array($this->urlParams)){
412+
$this->urlParams = array_merge($this->urlParams, $urlParams);
499413
} else {
500-
$urlFinal .= '?'.$urlParams;
414+
$this->urlParams = $urlParams;
501415
}
502-
return $urlFinal;
416+
return $this->urlParams;
503417
}
504418

419+
505420
/**
506421
* Update $this->apiURL
507422
*/
@@ -526,13 +441,13 @@ public function requestData($urlSuffix = null, $method = 'GET',
526441

527442
if (preg_match('/^http/', $urlSuffix)) {
528443
$url = $urlSuffix;
529-
} elseif ($urlSuffix[0] == '/') {
444+
} elseif (strlen ($urlSuffix) && ($urlSuffix[0] == '/')) {
530445
$url = $this->url.$urlSuffix;
531446
} else {
532447
$url = $this->sectionUrlWithSuffix($urlSuffix);
533448
}
534449

535-
$responseCode = $this->doCurlRequest($url, $method, $format);
450+
$responseCode = $this->doCurlRequest( \Ease\Shared::addUrlParams( $url, $this->urlParams ), $method, $format);
536451

537452
return strlen($this->lastCurlResponse) ? $this->parseResponse($this->rawResponseToArray($this->lastCurlResponse,
538453
$this->responseMimeType), $responseCode) : null;
@@ -622,10 +537,10 @@ public function parseError(array $responseDecoded)
622537
/**
623538
* Vykonej HTTP požadavek
624539
*
625-
* @link https://www.ipex.eu/api/dokumentace/ref/urls/ Sestavování URL
626540
* @param string $url URL požadavku
627541
* @param string $method HTTP Method GET|POST|PUT|OPTIONS|DELETE
628542
* @param string $format požadovaný formát komunikace
543+
*
629544
* @return int HTTP Response CODE
630545
*/
631546
public function doCurlRequest($url, $method, $format = null)
@@ -683,34 +598,15 @@ public function doCurlRequest($url, $method, $format = null)
683598
}
684599

685600
/**
686-
* Convert XML to array.
687-
*
688-
* @param string $xml
689-
*
690-
* @return array
601+
* Load Data Row from IPEX
602+
*
603+
* @param string $key what we want to get
604+
*
605+
* @return int loaded columns count
691606
*/
692-
public static function xml2array($xml)
693-
{
694-
$arr = [];
695-
696-
if (is_string($xml)) {
697-
$xml = simplexml_load_string($xml);
698-
}
699-
700-
foreach ($xml->children() as $r) {
701-
if (count($r->children()) == 0) {
702-
$arr[$r->getName()] = strval($r);
703-
} else {
704-
$arr[$r->getName()][] = self::xml2array($r);
705-
}
706-
}
707-
708-
return $arr;
709-
}
710-
711607
public function loadFromIPEX($key)
712608
{
713-
return $this->takeData($this->requestData(is_array($key) ? $this->addUrlParams(null,
609+
return $this->takeData($this->requestData(is_array($key) ? \Ease\Shared::addUrlParams(null,
714610
$key) : $key));
715611
}
716612

@@ -719,6 +615,7 @@ public function loadFromIPEX($key)
719615
*
720616
* @param array $resultData
721617
* @param string $url URL
618+
*
722619
* @return boolean Log save success
723620
*/
724621
public function logResult($resultData = null, $url = null)

0 commit comments

Comments
 (0)