for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace PTS\Routing\Middlewares;
use Psr\Http\Message\RequestInterface;
class CheckXHR
{
const ONLY_XHR = 1;
const ONLY_NO_XHR = 2;
protected $state;
/**
* CheckXHR constructor.
* @param int $state
*/
public function __construct(int $state = self::ONLY_XHR)
$this->state = $state;
}
* @param RequestInterface $request
* @param callable $next
* @return mixed
public function __invoke(RequestInterface $request, callable $next)
$isXHR = 'XMLHttpRequest' === $request->getHeader('X-Requested-With');
switch ($this->state) {
case self::ONLY_XHR:
if (!$isXHR) { return null; }
break;
case self::ONLY_NO_XHR:
if ($isXHR) { return null; }
return $next($request);