2017-07-14 15:08:47 +03:00
|
|
|
<?php
|
|
|
|
namespace GraphQL\Server;
|
2017-07-16 14:52:38 +03:00
|
|
|
|
2017-07-14 15:08:47 +03:00
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* Structure representing parsed HTTP parameters for GraphQL operation
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
|
|
|
class OperationParams
|
|
|
|
{
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* Id of the query (when using persistent queries).
|
|
|
|
*
|
|
|
|
* Valid aliases (case-insensitive):
|
|
|
|
* - id
|
|
|
|
* - queryId
|
|
|
|
* - documentId
|
|
|
|
*
|
|
|
|
* @api
|
2017-07-14 15:08:47 +03:00
|
|
|
* @var string
|
|
|
|
*/
|
2017-08-19 19:01:46 +03:00
|
|
|
public $queryId;
|
2017-07-14 15:08:47 +03:00
|
|
|
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* @api
|
2017-07-14 15:08:47 +03:00
|
|
|
* @var string
|
|
|
|
*/
|
2017-08-19 19:01:46 +03:00
|
|
|
public $query;
|
2017-07-14 15:08:47 +03:00
|
|
|
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* @api
|
2017-07-14 15:08:47 +03:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $operation;
|
|
|
|
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* @api
|
2017-07-14 15:08:47 +03:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $variables;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $originalInput;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2017-07-17 12:57:30 +03:00
|
|
|
private $readOnly;
|
2017-07-14 15:08:47 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an instance from given array
|
|
|
|
*
|
2017-08-19 19:01:46 +03:00
|
|
|
* @api
|
2017-07-14 15:08:47 +03:00
|
|
|
* @param array $params
|
2017-07-17 12:57:30 +03:00
|
|
|
* @param bool $readonly
|
2017-08-19 19:01:46 +03:00
|
|
|
* @return OperationParams
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
2017-07-17 12:57:30 +03:00
|
|
|
public static function create(array $params, $readonly = false)
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
|
|
|
$instance = new static();
|
|
|
|
|
|
|
|
$params = array_change_key_case($params, CASE_LOWER);
|
2017-07-19 15:30:39 +03:00
|
|
|
$instance->originalInput = $params;
|
2017-07-14 15:08:47 +03:00
|
|
|
|
|
|
|
$params += [
|
|
|
|
'query' => null,
|
|
|
|
'queryid' => null,
|
|
|
|
'documentid' => null, // alias to queryid
|
2017-08-17 22:54:35 +03:00
|
|
|
'id' => null, // alias to queryid
|
2017-07-14 15:08:47 +03:00
|
|
|
'operation' => null,
|
|
|
|
'variables' => null
|
|
|
|
];
|
|
|
|
|
2017-07-19 15:30:39 +03:00
|
|
|
if (is_string($params['variables'])) {
|
|
|
|
$tmp = json_decode($params['variables'], true);
|
|
|
|
if (!json_last_error()) {
|
|
|
|
$params['variables'] = $tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-14 15:08:47 +03:00
|
|
|
$instance->query = $params['query'];
|
2017-08-17 22:54:35 +03:00
|
|
|
$instance->queryId = $params['queryid'] ?: $params['documentid'] ?: $params['id'];
|
2017-07-14 15:08:47 +03:00
|
|
|
$instance->operation = $params['operation'];
|
|
|
|
$instance->variables = $params['variables'];
|
2017-07-17 12:57:30 +03:00
|
|
|
$instance->readOnly = (bool) $readonly;
|
2017-07-14 15:08:47 +03:00
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* @api
|
2017-07-19 15:30:39 +03:00
|
|
|
* @param string $key
|
|
|
|
* @return mixed
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
2017-07-19 15:30:39 +03:00
|
|
|
public function getOriginalInput($key)
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
2017-07-19 15:30:39 +03:00
|
|
|
return isset($this->originalInput[$key]) ? $this->originalInput[$key] : null;
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* Indicates that operation is executed in read-only context
|
|
|
|
* (e.g. via HTTP GET request)
|
|
|
|
*
|
|
|
|
* @api
|
2017-07-14 15:08:47 +03:00
|
|
|
* @return bool
|
|
|
|
*/
|
2017-07-17 12:57:30 +03:00
|
|
|
public function isReadOnly()
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
2017-07-17 12:57:30 +03:00
|
|
|
return $this->readOnly;
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
}
|