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
<?php
namespace Gedcomx\Rs\Client\Options;
use GuzzleHttp\Psr7\Request;
/**
* This is a helper class for managing headers in REST API requests.
*
* Class HeaderParameter
*
* @package Gedcomx\Rs\Client\Options
*/
class HeaderParameter implements StateTransitionOption
{
/**
* The accept language header
*/
const LANG = "Accept-Language";
/**
* The locale header
*/
const LOCALE = self::LANG;
/**
* The if-none-match header
*/
const IF_NONE_MATCH = "If-None-Match";
/**
* The if-modified-since header
*/
const IF_MODIFIED_SINCE = "If-Modified-Since";
/**
* The if-match header
*/
const IF_MATCH = "If-Match";
/**
* The if-unmodified-since header
*/
const IF_UNMODIFIED_SINCE = "If-Unmodified-Since";
/**
* The ETag (entity tag) header
*/
const ETAG = "ETag";
/**
* The last-modified header
*/
const LAST_MODIFIED = "Last-Modified";
private $replace;
private $name;
private $value;
/**
* Constructs a header parameter with 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 header parameters to the REST API request.
*
* @param Request $request
* @return Request $request
*/
public function apply(Request $request)
{
if ($this->replace) {
return $request->withHeader($this->name, $this->value);
} else {
return $request->withAddedHeader($this->name, $this->value);
}
}
/**
* Creates an accept-language header parameter.
*
* @param string $value
*
* @return HeaderParameter
*/
public static function lang($value)
{
return new HeaderParameter(true, self::LANG, $value);
}
/**
* Creates an accept-language header parameter.
*
* @param string $value
*
* @return HeaderParameter
*/
public static function locale($value)
{
return new HeaderParameter(true, self::LOCALE, $value);
}
}