Skip to content

Commit 2b9245b

Browse files
committed
Adds support for Sandbox API root
1 parent 7a70021 commit 2b9245b

5 files changed

Lines changed: 59 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
v0.0.4
5+
------
6+
7+
* Adds support for Sandbox API root
8+
9+
410
v0.0.3
511
------
612

autoload.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
/**
33
* From https://www.php-fig.org/psr/psr-4/examples/.
44
*/
5+
6+
// Load helper functions that are not provided by classes
7+
require_once './src/lib/array.php';
8+
59
spl_autoload_register(function ($class) {
610
// project-specific namespace prefix
711
$prefix = 'Alma\\API\\';

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
"autoload": {
2222
"psr-4": {
2323
"Alma\\API\\": "src/"
24-
}
24+
},
25+
"files": [
26+
"src/lib/array.php"
27+
]
2528
}
2629
}

src/Client.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ class DependenciesError extends \Exception
4545

4646
class Client implements LoggerAwareInterface
4747
{
48-
const API_URL = 'https://api.getalma.eu';
48+
const LIVE_API_URL = 'https://api.getalma.eu';
49+
const SANDBOX_API_URL = 'https://api.sandbox.getalma.eu';
4950

5051
protected $context;
5152

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

96-
$options = array_merge(array(
97-
'api_root' => self::API_URL,
97+
$options = alma_array_merge_recursive(array(
98+
'api_root' => array(TEST_MODE => self::SANDBOX_API_URL, LIVE_MODE => self::LIVE_API_URL),
9899
'force_tls' => 2,
99100
'mode' => LIVE_MODE,
100101
'logger' => new NullLogger(),

src/lib/array.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
// https://secure.php.net/manual/en/function.array-merge-recursive.php#104145
4+
// Addition: will automatically discard null values
5+
function alma_array_merge_recursive() {
6+
7+
if ( func_num_args() < 2 ) {
8+
trigger_error( __FUNCTION__ . ' needs two or more array arguments', E_USER_WARNING );
9+
10+
return null;
11+
}
12+
$arrays = func_get_args();
13+
$merged = array();
14+
while ( $arrays ) {
15+
$array = array_shift( $arrays );
16+
if ( $array === null ) {
17+
continue;
18+
}
19+
if ( ! is_array( $array ) ) {
20+
trigger_error( __FUNCTION__ . ' encountered a non array argument', E_USER_WARNING );
21+
22+
return null;
23+
}
24+
if ( ! $array ) {
25+
continue;
26+
}
27+
foreach ( $array as $key => $value ) {
28+
if ( is_string( $key ) ) {
29+
if ( is_array( $value ) && array_key_exists( $key, $merged ) && is_array( $merged[ $key ] ) ) {
30+
$merged[ $key ] = call_user_func( __FUNCTION__, $merged[ $key ], $value );
31+
} else {
32+
$merged[ $key ] = $value;
33+
}
34+
} else {
35+
$merged[] = $value;
36+
}
37+
}
38+
}
39+
40+
return $merged;
41+
}

0 commit comments

Comments
 (0)