Exception
2 months ago
Helper.php
2 months ago
OperationParams.php
2 months ago
RequestError.php
2 months ago
ServerConfig.php
2 months ago
StandardServer.php
2 months ago
OperationParams.php
149 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Vendor\GraphQL\Server; |
| 4 | |
| 5 | /** |
| 6 | * Structure representing parsed HTTP parameters for Automattic\WooCommerce\Vendor\GraphQL operation. |
| 7 | * |
| 8 | * The properties in this class are not strictly typed, as this class |
| 9 | * is only meant to serve as an intermediary representation which is |
| 10 | * not yet validated. |
| 11 | */ |
| 12 | class OperationParams |
| 13 | { |
| 14 | /** |
| 15 | * Id of the query (when using persisted queries). |
| 16 | * |
| 17 | * Valid aliases (case-insensitive): |
| 18 | * - id |
| 19 | * - queryId |
| 20 | * - documentId |
| 21 | * |
| 22 | * @api |
| 23 | * |
| 24 | * @var mixed should be string|null |
| 25 | */ |
| 26 | public $queryId; |
| 27 | |
| 28 | /** |
| 29 | * A document containing Automattic\WooCommerce\Vendor\GraphQL operations and fragments to execute. |
| 30 | * |
| 31 | * @api |
| 32 | * |
| 33 | * @var mixed should be string|null |
| 34 | */ |
| 35 | public $query; |
| 36 | |
| 37 | /** |
| 38 | * The name of the operation in the document to execute. |
| 39 | * |
| 40 | * @api |
| 41 | * |
| 42 | * @var mixed should be string|null |
| 43 | */ |
| 44 | public $operation; |
| 45 | |
| 46 | /** |
| 47 | * Values for any variables defined by the operation. |
| 48 | * |
| 49 | * @api |
| 50 | * |
| 51 | * @var mixed should be array<string, mixed> |
| 52 | */ |
| 53 | public $variables; |
| 54 | |
| 55 | /** |
| 56 | * Reserved for implementors to extend the protocol however they see fit. |
| 57 | * |
| 58 | * @api |
| 59 | * |
| 60 | * @var mixed should be array<string, mixed> |
| 61 | */ |
| 62 | public $extensions; |
| 63 | |
| 64 | /** |
| 65 | * Executed in read-only context (e.g. via HTTP GET request)? |
| 66 | * |
| 67 | * @api |
| 68 | */ |
| 69 | public bool $readOnly; |
| 70 | |
| 71 | /** |
| 72 | * The raw params used to construct this instance. |
| 73 | * |
| 74 | * @api |
| 75 | * |
| 76 | * @var array<string, mixed> |
| 77 | */ |
| 78 | public array $originalInput; |
| 79 | |
| 80 | /** |
| 81 | * Creates an instance from given array. |
| 82 | * |
| 83 | * @param array<string, mixed> $params |
| 84 | * |
| 85 | * @api |
| 86 | */ |
| 87 | public static function create(array $params, bool $readonly = false): OperationParams |
| 88 | { |
| 89 | $instance = new static(); |
| 90 | |
| 91 | $params = array_change_key_case($params, \CASE_LOWER); |
| 92 | $instance->originalInput = $params; |
| 93 | |
| 94 | $params += [ |
| 95 | 'query' => null, |
| 96 | 'queryid' => null, |
| 97 | 'documentid' => null, // alias to queryid |
| 98 | 'id' => null, // alias to queryid |
| 99 | 'operationname' => null, |
| 100 | 'variables' => null, |
| 101 | 'extensions' => null, |
| 102 | ]; |
| 103 | |
| 104 | foreach ($params as &$value) { |
| 105 | if ($value === '') { |
| 106 | $value = null; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | $instance->query = $params['query']; |
| 111 | $instance->queryId = $params['queryid'] ?? $params['documentid'] ?? $params['id']; |
| 112 | $instance->operation = $params['operationname']; |
| 113 | $instance->variables = static::decodeIfJSON($params['variables']); |
| 114 | $instance->extensions = static::decodeIfJSON($params['extensions']); |
| 115 | $instance->readOnly = $readonly; |
| 116 | |
| 117 | // Apollo server/client compatibility |
| 118 | if ( |
| 119 | isset($instance->extensions['persistedQuery']['sha256Hash']) |
| 120 | && $instance->queryId === null |
| 121 | ) { |
| 122 | $instance->queryId = $instance->extensions['persistedQuery']['sha256Hash']; |
| 123 | } |
| 124 | |
| 125 | return $instance; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Decodes the value if it is JSON, otherwise returns it unchanged. |
| 130 | * |
| 131 | * @param mixed $value |
| 132 | * |
| 133 | * @return mixed |
| 134 | */ |
| 135 | protected static function decodeIfJSON($value) |
| 136 | { |
| 137 | if (! is_string($value)) { |
| 138 | return $value; |
| 139 | } |
| 140 | |
| 141 | $decoded = json_decode($value, true); |
| 142 | if (json_last_error() === \JSON_ERROR_NONE) { |
| 143 | return $decoded; |
| 144 | } |
| 145 | |
| 146 | return $value; |
| 147 | } |
| 148 | } |
| 149 |