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