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
<?php
namespace App\Provider;
use Symfony\Component\HttpFoundation\Response;
class TransitionsProvider
{
protected $transitions;
public function __construct()
{
$this->transitions = [];
}
public function addTransition($uri, $relations)
{
if (! is_array($relations)) {
$relations = preg_split('/[\s,]+/', $relations, -1, PREG_SPLIT_NO_EMPTY);
}
foreach ($relations as $rel) {
if (! isset($this->transitions[$uri])) {
$this->transitions[$uri] = [$rel];
} else {
$this->transitions[$uri][] = $rel;
}
}
}
public function setHeaders(Response $response)
{
$tr = [];
foreach ($this->transitions as $uri => $rel) {
$tr[] = "<$uri>; rel=\"" . join(' ', array_unique($rel)) . "\"";
}
if (! empty($tr)) {
$response->headers->set('Link', join(', ', $tr));
}
}
}