|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EventEspresso\core\domain\services\graphql; |
|
4
|
|
|
|
|
5
|
|
|
use GraphQLRelay\Relay; |
|
6
|
|
|
|
|
7
|
|
|
class Utilities |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* This sets up the "allowed" args, and translates the GraphQL-friendly keys to model |
|
11
|
|
|
* friendly keys. |
|
12
|
|
|
* |
|
13
|
|
|
* @param array $where_args |
|
14
|
|
|
* @param array $arg_mapping array where keys are GQL field names and values are EE modal field names |
|
15
|
|
|
* @param array $id_fields The fields to convert from global IDs to DB IDs. |
|
16
|
|
|
* @param array $options Additional parameters for modifying args: [ |
|
17
|
|
|
* 'include_all_args' => bool, // will return ALL args in $where_args if true |
|
18
|
|
|
* 'use_IN_operator' => bool, // arrays of IDs will use SQL IN clause if true |
|
19
|
|
|
* ] |
|
20
|
|
|
* @return array |
|
21
|
|
|
*/ |
|
22
|
|
|
public function sanitizeWhereArgs(array $where_args, array $arg_mapping, array $id_fields, array $options = []) |
|
23
|
|
|
{ |
|
24
|
|
|
// if "include_all_args" is true, then the incoming $where_args array |
|
25
|
|
|
// will be copied to the outgoing $where_params prior to sanitizing the fields. |
|
26
|
|
|
// so ALL elements in the $where_args array will be present in the $where_params array |
|
27
|
|
|
$include_all_args = isset($options['include_all_args']) |
|
28
|
|
|
? filter_var($options['include_all_args'], FILTER_VALIDATE_BOOLEAN) |
|
29
|
|
|
: false; |
|
30
|
|
|
// if "use_IN_operator" is true, then any ID args found in the $id_fields array |
|
31
|
|
|
// will have their values converted to use an SQL "IN" clause format |
|
32
|
|
|
// if the value returned from Relay::fromGlobalId() is an array of IDs |
|
33
|
|
|
$use_IN_operator = isset($options['use_IN_operator']) |
|
34
|
|
|
? filter_var($options['use_IN_operator'], FILTER_VALIDATE_BOOLEAN) |
|
35
|
|
|
: false; |
|
36
|
|
|
$where_params = $include_all_args ? $where_args : []; |
|
37
|
|
|
foreach ($where_args as $arg => $value) { |
|
38
|
|
|
if (! array_key_exists($arg, $arg_mapping)) { |
|
39
|
|
|
continue; |
|
40
|
|
|
} |
|
41
|
|
|
if (is_array($value) && ! empty($value)) { |
|
42
|
|
|
$value = array_map( |
|
43
|
|
|
static function ($value) { |
|
44
|
|
|
if (is_string($value)) { |
|
45
|
|
|
$value = sanitize_text_field($value); |
|
46
|
|
|
} |
|
47
|
|
|
return $value; |
|
48
|
|
|
}, |
|
49
|
|
|
$value |
|
50
|
|
|
); |
|
51
|
|
|
} elseif (is_string($value)) { |
|
52
|
|
|
$value = sanitize_text_field($value); |
|
53
|
|
|
} |
|
54
|
|
|
if (in_array($arg, $id_fields, true)) { |
|
55
|
|
|
$ID = $this->convertGlobalId($value); |
|
56
|
|
|
// Use the proper operator. |
|
57
|
|
|
$value = $use_IN_operator && is_array($ID) ? ['IN', $ID] : $ID; |
|
58
|
|
|
} |
|
59
|
|
|
$where_params[ $arg_mapping[ $arg ] ] = $value; |
|
60
|
|
|
} |
|
61
|
|
|
return $where_params; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Converts global ID to DB ID. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string|string[] $ID |
|
69
|
|
|
* @return mixed |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function convertGlobalId($ID) |
|
72
|
|
|
{ |
|
73
|
|
|
if (is_array($ID)) { |
|
74
|
|
|
return array_map([$this, 'convertGlobalId'], $ID); |
|
75
|
|
|
} |
|
76
|
|
|
$parts = Relay::fromGlobalId($ID); |
|
77
|
|
|
return ! empty($parts['id']) ? $parts['id'] : null; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|