Class Link
PSR-13 Link implementation.
Represents a hypermedia link as defined in RFC 5988 and RFC 8288.
Usage
$link = new Link('/api/users', 'self');
$link = $link->withRel('collection');
$link = $link->withAttribute('type', 'application/json');
Method Summary
-
__construct() public
Constructor.
-
getAttributes() public
Returns a list of attributes that describe the target URI.
-
getHref() public
Returns the target of the link.
-
getRels() public
Returns the relationship type(s) of the link.
-
isTemplated() public
Returns whether or not this is a templated link.
-
withAttribute() public
Returns an instance with the specified attribute added.
-
withHref() public
Returns an instance with the specified href.
-
withRel() public
Returns an instance with the specified relationship included.
-
withoutAttribute() public
Returns an instance with the specified attribute excluded.
-
withoutRel() public
Returns an instance with the specified relationship excluded.
Method Detail
__construct() ¶ public
__construct(string $href = '', array<string>|string $rels = [], array<string, string|bool|int|float|array<string>> $attributes = [])
Constructor.
Parameters
-
string$href optional The link URI.
-
array<string>|string$rels optional The link relation(s).
-
array<string, string|bool|int|float|array<string>>$attributes optional Additional attributes.
getAttributes() ¶ public
getAttributes(): array
Returns a list of attributes that describe the target URI.
Returns
arraygetHref() ¶ public
getHref(): string
Returns the target of the link.
The target link must be one of:
- An absolute URI, as defined by RFC 5988.
- A relative URI, as defined by RFC 5988. The base of the relative link is assumed to be known based on context by the client.
- A URI template as defined by RFC 6570.
If a URI template is returned, isTemplated() MUST return True.
Returns
stringgetRels() ¶ public
getRels(): string[]
Returns the relationship type(s) of the link.
This method returns 0 or more relationship types for a link, expressed as an array of strings.
Returns
string[]isTemplated() ¶ public
isTemplated(): bool
Returns whether or not this is a templated link.
Returns
boolwithAttribute() ¶ public
withAttribute(string $attribute, string|Stringable|int|float|bool|array $value): static
Returns an instance with the specified attribute added.
If the specified attribute is already present, it will be overwritten with the new value.
Parameters
-
string$attribute -
string|Stringable|int|float|bool|array$value
Returns
staticwithHref() ¶ public
withHref(string|Stringable $href): static
Returns an instance with the specified href.
Parameters
-
string|Stringable$href
Returns
staticwithRel() ¶ public
withRel(string $rel): static
Returns an instance with the specified relationship included.
If the specified rel is already present, this method MUST return normally without errors, but without adding the rel a second time.
Parameters
-
string$rel
Returns
staticwithoutAttribute() ¶ public
withoutAttribute(string $attribute): static
Returns an instance with the specified attribute excluded.
If the specified attribute is not present, this method MUST return normally without errors.
Parameters
-
string$attribute
Returns
staticwithoutRel() ¶ public
withoutRel(string $rel): static
Returns an instance with the specified relationship excluded.
If the specified rel is already not present, this method MUST return normally without errors.
Parameters
-
string$rel
Returns
static