*/ public $variables; /** * Reserved for implementors to extend the protocol however they see fit. * * @api * * @var mixed should be array */ public $extensions; /** * Executed in read-only context (e.g. via HTTP GET request)? * * @api */ public bool $readOnly; /** * The raw params used to construct this instance. * * @api * * @var array */ public array $originalInput; /** * Creates an instance from given array. * * @param array $params * * @api */ public static function create(array $params, bool $readonly = false): OperationParams { $instance = new static(); $params = array_change_key_case($params, \CASE_LOWER); $instance->originalInput = $params; $params += [ 'query' => null, 'queryid' => null, 'documentid' => null, // alias to queryid 'id' => null, // alias to queryid 'operationname' => null, 'variables' => null, 'extensions' => null, ]; foreach ($params as &$value) { if ($value === '') { $value = null; } } $instance->query = $params['query']; $instance->queryId = $params['queryid'] ?? $params['documentid'] ?? $params['id']; $instance->operation = $params['operationname']; $instance->variables = static::decodeIfJSON($params['variables']); $instance->extensions = static::decodeIfJSON($params['extensions']); $instance->readOnly = $readonly; // Apollo server/client compatibility if ( isset($instance->extensions['persistedQuery']['sha256Hash']) && $instance->queryId === null ) { $instance->queryId = $instance->extensions['persistedQuery']['sha256Hash']; } return $instance; } /** * Decodes the value if it is JSON, otherwise returns it unchanged. * * @param mixed $value * * @return mixed */ protected static function decodeIfJSON($value) { if (! is_string($value)) { return $value; } $decoded = json_decode($value, true); if (json_last_error() === \JSON_ERROR_NONE) { return $decoded; } return $value; } }