Skip to content

Commit 9810d2f

Browse files
authored
Merge pull request #961 from tranvantri/master
2 parents 59251b2 + 33dbc8f commit 9810d2f

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"illuminate/contracts": "^6.0|^8.0|^9.0",
4040
"illuminate/support": "^6.0|^8.0|^9.0",
4141
"laragraph/utils": "^1",
42-
"thecodingmachine/safe": "^1.3",
42+
"thecodingmachine/safe": "^1.1|^2.4",
4343
"webonyx/graphql-php": "^14.6.4"
4444
},
4545
"require-dev": {

src/Helpers.php

+33
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace Rebing\GraphQL;
55

66
use Closure;
7+
use OutOfBoundsException;
78

89
class Helpers
910
{
@@ -23,4 +24,36 @@ public static function applyEach(Closure $callback, $valueOrValues)
2324

2425
return $callback($valueOrValues);
2526
}
27+
28+
/**
29+
* Check compatible ability to use thecodingmachine/safe.
30+
*
31+
* @return string|false
32+
*/
33+
public static function shouldUseSafe(string $methodName)
34+
{
35+
$packageName = 'thecodingmachine/safe';
36+
$safeVersion = \Composer\InstalledVersions::getVersion($packageName);
37+
38+
if (!$safeVersion) {
39+
throw new OutOfBoundsException("Package {$packageName} is being replaced or provided but is not really installed");
40+
}
41+
42+
$skipFunctions = [
43+
'uksort',
44+
];
45+
46+
// Version 2.
47+
if (version_compare($safeVersion, '2', '>=')) {
48+
if (\in_array(str_replace('\\Safe\\', '', $methodName), $skipFunctions)) {
49+
return false;
50+
}
51+
}
52+
53+
if (!\is_callable($methodName)) {
54+
return false;
55+
}
56+
57+
return $methodName;
58+
}
2659
}

src/Support/AliasArguments/ArrayKeyChange.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
declare(strict_types = 1);
44
namespace Rebing\GraphQL\Support\AliasArguments;
55

6+
use Rebing\GraphQL\Helpers;
7+
68
class ArrayKeyChange
79
{
810
public function modify(array $array, array $pathKeyMappings): array
@@ -21,9 +23,17 @@ public function modify(array $array, array $pathKeyMappings): array
2123
*/
2224
private function orderPaths(array $paths): array
2325
{
24-
\Safe\uksort($paths, function (string $a, string $b): int {
26+
$callback = function (string $a, string $b): int {
2527
return $this->pathLevels($b) <=> $this->pathLevels($a);
26-
});
28+
};
29+
30+
$functionName = Helpers::shouldUseSafe('\\Safe\\uksort');
31+
32+
if (\is_callable($functionName)) {
33+
$functionName($paths, $callback);
34+
} else {
35+
uksort($paths, $callback);
36+
}
2737

2838
return $paths;
2939
}

0 commit comments

Comments
 (0)