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
<?php
namespace ICanBoogie\HTTP;
class RequestOptionsMapper implements RequestOptions
{
public function map(array &$options, array &$env)
{
$mappers = $this->get_mappers();
foreach ($options as $option => &$value)
{
if (empty($mappers[$option]))
{
throw new \InvalidArgumentException("Option not supported: `$option`.");
}
$value = $mappers[$option]($value, $env);
if ($value === null)
{
unset($options[$option]);
}
}
}
protected function get_mappers()
{
return [
self::OPTION_PATH_PARAMS => function($value) { return $value; },
self::OPTION_QUERY_PARAMS => function($value) { return $value; },
self::OPTION_REQUEST_PARAMS => function($value) { return $value; },
self::OPTION_COOKIE => function($value) { return $value; },
self::OPTION_FILES => function($value) { return $value; },
self::OPTION_HEADERS => function($value) { return ($value instanceof Headers) ? $value : new Headers($value); },
self::OPTION_CACHE_CONTROL => function($value, array &$env) { $env['HTTP_CACHE_CONTROL'] = $value; },
self::OPTION_CONTENT_LENGTH => function($value, array &$env) { $env['CONTENT_LENGTH'] = $value; },
self::OPTION_IP => function($value, array &$env) { if ($value) $env['REMOTE_ADDR'] = $value; },
self::OPTION_IS_LOCAL => function($value, array &$env) { if ($value) $env['REMOTE_ADDR'] = '::1'; },
self::OPTION_IS_DELETE => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_DELETE; },
self::OPTION_IS_CONNECT => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_CONNECT; },
self::OPTION_IS_GET => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_GET; },
self::OPTION_IS_HEAD => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_HEAD; },
self::OPTION_IS_OPTIONS => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_OPTIONS; },
self::OPTION_IS_PATCH => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_PATCH; },
self::OPTION_IS_POST => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_POST; },
self::OPTION_IS_PUT => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_PUT; },
self::OPTION_IS_TRACE => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = Request::METHOD_TRACE; },
self::OPTION_IS_XHR => function($value, array &$env) { if ($value) $env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'; else unset($env['HTTP_X_REQUESTED_WITH']); },
self::OPTION_METHOD => function($value, array &$env) { if ($value) $env['REQUEST_METHOD'] = $value; },
self::OPTION_PATH => function($value, array &$env) { $env['REQUEST_URI'] = $value; },
self::OPTION_REFERER => function($value, array &$env) { $env['HTTP_REFERER'] = $value; },
self::OPTION_URI => function($value, array &$env) { $env['REQUEST_URI'] = $value; $qs = strpos($value, '?'); $env['QUERY_STRING'] = $qs === false ? '' : substr($value, $qs + 1); },
self::OPTION_USER_AGENT => function($value, array &$env) { $env['HTTP_USER_AGENT'] = $value; }
];
}
}