Skip to content

Commit

Permalink
Fixes the bug on custom stream options for serverGet(). Fixes #55.
Browse files Browse the repository at this point in the history
  • Loading branch information
uzyn committed Jun 21, 2013
1 parent 4e28188 commit 040470e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
50 changes: 49 additions & 1 deletion lib/Opauth/OpauthStrategy.php
Expand Up @@ -386,7 +386,7 @@ public static function serverPost($url, $data, $options = array(), &$responseHea
'content' => $query
));

$stream = array_merge_recursive($options, $stream);
$stream = self::arrayReplaceRecursive($stream, $options);

return self::httpRequest($url, $stream, $responseHeaders);
}
Expand Down Expand Up @@ -490,4 +490,52 @@ public static function envReplace($value, $dictionary) {
return $value;
}

/**
* array_replace_recursive() polyfill for PHP 5.2
* From: http://sg.php.net/manual/en/function.array-replace-recursive.php#92574
*
* @param array $array The array in which elements are replaced.
* @param array $array1 The array from which elements will be extracted
* @return array Returns an array or null if an error occurs.
*/
public static function arrayReplaceRecursive($array, $array1) {

if (!function_exists('array_replace_recursive')) {
function array_replace_recursive($array, $array1) {
function recurse($array, $array1) {
foreach ($array1 as $key => $value) {

if (!isset($array[$key]) || (isset($array[$key]) && !is_array($array[$key]))) {
$array[$key] = array();
}

if (is_array($value)) {
$value = recurse($array[$key], $value);
}

$array[$key] = $value;
}
return $array;
}

$args = func_get_args();

$array = $args[0];

if (!is_array($array)) {
return $array;
}

for ($i = 1; $i < count($args); $i++) {
if (is_array($args[$i])) {
$array = recurse($array, $args[$i]);
}
}

return $array;
}
}

return array_replace_recursive($array, $array1);
}
}
23 changes: 23 additions & 0 deletions tests/Opauth/OpauthStrategyTest.php
Expand Up @@ -114,6 +114,29 @@ public function testClientGet() {
$this->assertContains("Location: $fullUrl", $headers_list);
}

public function testArrayReplaceRecursive() {
$array = array('http' => array(
'method' => 'POST',
'content' => 'lorem ipsum'
));

$replacement = array('http' => array(
'content' => 'New content',
'new_key' => 'some value'
), 'another_key' => 919);

$replaced = OpauthStrategy::arrayReplaceRecursive($array, $replacement);

$this->assertEquals($replaced, array(
'http' => array(
'method' => 'POST',
'content' => 'New content',
'new_key' => 'some value'
),
'another_key' => 919
));
}

/**
* Instantiate OpauthStrategy with test config suitable for testing
*
Expand Down

0 comments on commit 040470e

Please sign in to comment.