Skip to content

Commit

Permalink
Adds support for Sandbox API root
Browse files Browse the repository at this point in the history
  • Loading branch information
olance committed Dec 18, 2018
1 parent 7a70021 commit 2b9245b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

v0.0.4
------

* Adds support for Sandbox API root


v0.0.3
------

Expand Down
4 changes: 4 additions & 0 deletions autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
/**
* From https://www.php-fig.org/psr/psr-4/examples/.
*/

// Load helper functions that are not provided by classes
require_once './src/lib/array.php';

spl_autoload_register(function ($class) {
// project-specific namespace prefix
$prefix = 'Alma\\API\\';
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
"autoload": {
"psr-4": {
"Alma\\API\\": "src/"
}
},
"files": [
"src/lib/array.php"
]
}
}
7 changes: 4 additions & 3 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class DependenciesError extends \Exception

class Client implements LoggerAwareInterface
{
const API_URL = 'https://api.getalma.eu';
const LIVE_API_URL = 'https://api.getalma.eu';
const SANDBOX_API_URL = 'https://api.sandbox.getalma.eu';

protected $context;

Expand Down Expand Up @@ -93,8 +94,8 @@ public function __construct($api_key, $options = array())
throw new ParamsError('An API key is required to instantiate new Alma\Client');
}

$options = array_merge(array(
'api_root' => self::API_URL,
$options = alma_array_merge_recursive(array(
'api_root' => array(TEST_MODE => self::SANDBOX_API_URL, LIVE_MODE => self::LIVE_API_URL),
'force_tls' => 2,
'mode' => LIVE_MODE,
'logger' => new NullLogger(),
Expand Down
41 changes: 41 additions & 0 deletions src/lib/array.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

// https://secure.php.net/manual/en/function.array-merge-recursive.php#104145
// Addition: will automatically discard null values
function alma_array_merge_recursive() {

if ( func_num_args() < 2 ) {
trigger_error( __FUNCTION__ . ' needs two or more array arguments', E_USER_WARNING );

return null;
}
$arrays = func_get_args();
$merged = array();
while ( $arrays ) {
$array = array_shift( $arrays );
if ( $array === null ) {
continue;
}
if ( ! is_array( $array ) ) {
trigger_error( __FUNCTION__ . ' encountered a non array argument', E_USER_WARNING );

return null;
}
if ( ! $array ) {
continue;
}
foreach ( $array as $key => $value ) {
if ( is_string( $key ) ) {
if ( is_array( $value ) && array_key_exists( $key, $merged ) && is_array( $merged[ $key ] ) ) {
$merged[ $key ] = call_user_func( __FUNCTION__, $merged[ $key ], $value );
} else {
$merged[ $key ] = $value;
}
} else {
$merged[] = $value;
}
}
}

return $merged;
}

0 comments on commit 2b9245b

Please sign in to comment.