1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
<?php
namespace Gedcomx\Rs\Client\Options;
use GuzzleHttp\Psr7\Request;
/**
* Represents a generic query string parameter to use in REST API requests.
*
* Class QueryParameter
*
* @package Gedcomx\Rs\Client\Options
*/
class QueryParameter implements StateTransitionOption
{
/**
* The access token query parameter.
*/
const ACCESS_TOKEN = "access_token";
/**
* The count query parameter.
*/
const COUNT = "count";
/**
* The generations query parameter.
*/
const GENERATIONS = "generations";
/**
* The search query parameter.
*/
const SEARCH_QUERY = "q";
/**
* The start query parameter.
*/
const START = "start";
private $replace;
private $name;
private $value;
/**
* Constructs a new generic query parameter using the specified values.
*
* @param boolean $replace
* @param string $name
* @param string $value,...
*/
public function __construct($replace, $name, $value)
{
$this->replace = $replace;
$this->name = $name;
if (func_num_args() > 3) {
$args = func_get_args();
array_shift(array_shift($args));
$this->value = $args;
} else {
$this->value = $value;
}
}
/**
* This method adds the current parameter to the specified REST API request.
*
* @param \GuzzleHttp\Psr7\Request $request
* @return Request
*/
public function apply(Request $request)
{
$queryString = $request->getUri()->getQuery();
$queryParts = \GuzzleHttp\Psr7\parse_query($queryString);
$queryParts[$this->name] = $this->value;
$queryString = \GuzzleHttp\Psr7\build_query($queryParts);
return $request->withUri($request->getUri()->withQuery($queryString));
}
/**
* Creates an access token query string parameter.
*
* @param string $value
*
* @return QueryParameter
*/
public static function accessToken($value)
{
return new QueryParameter(true, self::ACCESS_TOKEN, $value);
}
/**
* Creates a count query string parameter.
*
* @param string $value
*
* @return QueryParameter
*/
public static function count($value)
{
return new QueryParameter(true, self::COUNT, $value);
}
/**
* Creates a generations query string parameter.
*
* @param string $value
*
* @return QueryParameter
*/
public static function generations($value)
{
return new QueryParameter(true, self::GENERATIONS, $value);
}
/**
* Creates a search query string parameter.
*
* @param string $value
*
* @return QueryParameter
*/
public static function searchQuery($value)
{
return new QueryParameter(true, self::SEARCH_QUERY, $value);
}
/**
* Creates a start query string parameter.
*
* @param string $value
*
* @return QueryParameter
*/
public static function start($value)
{
return new QueryParameter(true, self::START, $value);
}
}