Created
April 11, 2017 08:47
-
-
Save titpetric/e1414880099e0e5a90d87d56736bd87f to your computer and use it in GitHub Desktop.
Transform GraphQL fields into JSON to use them for filtering native PHP arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** A poor mans GraphQL fields parser | |
* | |
* Try to convert graphQL fields to JSON and then just use json_decode to produce an array. | |
*/ | |
class Fields | |
{ | |
/** Parse GraphQL fields into an array */ | |
public static function parse($query) | |
{ | |
$query = str_replace("{", "{\n", $query); | |
$query = str_replace("}", "\n}\n", $query); | |
$query = array_map("trim", explode("\n", $query)); | |
foreach ($query as $k => $line) { | |
// strip comments | |
$line = explode("#", $line); | |
$line = $line[0]; | |
// skip opening or closing tags | |
if ($line === "{" || $line === "") { | |
continue; | |
} | |
// declare as object value | |
if (strpos($line, "{") !== false) { | |
$name = trim(str_replace("{", "", $line)); | |
$query[$k] = '"' . $name . '": {'; | |
continue; | |
} | |
if (strpos($line, "}") !== false) { | |
$query[$k] .= ','; | |
continue; | |
} | |
$query[$k] = '"' . $line . '": true,'; | |
} | |
$query = implode("", $query); | |
// cut last comma | |
$query = substr($query, 0, -1); | |
// cut trailing commas | |
$query = str_replace(",}", "}", $query); | |
// produce php array | |
$retval = json_decode($query, true); | |
if (is_null($retval)) { | |
throw new \Exception(sprintf("Error when parsing GraphQL fields: '%s'", $query)); | |
} | |
return $retval; | |
} | |
/** Filter a PHP array with the GraphQL fields provided by parse() */ | |
public static function execute($query, $data) | |
{ | |
$filter = self::parse($query); | |
$result = array_intersect_key_recursive($data, $filter); | |
return $result; | |
} | |
} | |
function array_intersect_key_recursive($arr, $filter) | |
{ | |
$is_int = true; | |
foreach ($arr as $k => $v) { | |
if (!is_int($k) || !is_array($v)) { | |
$is_int = false; | |
break; | |
} | |
} | |
if ($is_int) { | |
foreach ($arr as $k => $v) { | |
$arr[$k] = array_intersect_key_recursive($v, $filter); | |
} | |
return $arr; | |
} | |
$retval = array(); | |
foreach ($filter as $key => $value) { | |
if (!isset($arr[$key])) { | |
continue; | |
} | |
if (is_array($value)) { | |
$retval[$key] = array_intersect_key_recursive($arr[$key], $value); | |
continue; | |
} | |
$retval[$key] = $arr[$key]; | |
} | |
return $retval; | |
} |
Author
titpetric
commented
Jan 28, 2022
via email
Try it
…On Thu, Jan 27, 2022, 22:15 Jorge ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
when calling $response = Fields::execute($_GET['graphql_query'],
$response); what its response?
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/e1414880099e0e5a90d87d56736bd87f#gistcomment-4044746>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABY7EHJ2VP3FAK3Q7TQ6J3UYGYW7ANCNFSM5M7B5VWQ>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment