for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Psr7Middlewares\Utils;
use Psr\Http\Message\ServerRequestInterface;
use Psr7Middlewares\Middleware;
/**
* Trait to save middleware related things as request attributes.
*/
trait AttributeTrait
{
* Store an attribute in the request.
*
* @param ServerRequestInterface $request
* @param string $name
* @param mixed $value
* @return ServerRequestInterface
private static function setAttribute(ServerRequestInterface $request, $name, $value)
$attributes = $request->getAttribute(Middleware::KEY, []);
$attributes[$name] = $value;
return $request->withAttribute(Middleware::KEY, $attributes);
}
* Retrieves an attribute from the request.
* @return mixed
private static function getAttribute(ServerRequestInterface $request, $name)
$attributes = $request->getAttribute(Middleware::KEY);
if (isset($attributes[$name])) {
return $attributes[$name];
* Check whether an attribute exists.
* @return bool
private static function hasAttribute(ServerRequestInterface $request, $name)
if (empty($attributes)) {
return false;
return array_key_exists($name, $attributes);