Hypermedia links (超媒體連接) 逐漸在網路上嶄露頭角,像是HTML contexts and various API format Contexts.
無論如何都沒有一個統一的超媒體格式,所以就沒辦法一統天下
這個規格想要提供PHP developers 有一個簡單通用的方法代表超媒體獨立的序列化格式,這樣System就可以serialize response跟著hypermeida links一起回去。
1. Specification
1.1 Basic links
一個hypermedia link最少包含 URI鎖定目標元素
一個關係,表示目標資源如何關連到資源
Link可能存在多種屬性,根據format來決定 然而額外的屬性可能沒有標準化或太廣泛了,所以這項文件沒有打算將所有的屬性都包含近來
-
Implementing Object - An object that implements one of the interfaces defined by this specification.
-
Serializer - A library or other system that takes one or more Link objects and produces a serialized representation of it in some defined format.
1.2 Attributes
所有的Links 可選擇性的包含0~多個外屬性,超過URL and Relationship ,這裡沒有標準的values可以去檢驗與驗證資料,在這個context下是否可行。在一個特殊的序列化格式下。 通常支援的值為 ‘hreflang’, ‘title’, and ‘type’.
序列化的產生者可能會忽略 在link上的屬性,假設他為了序列化的格式的話,他必須這麼做。 然而還是建議serializer盡力的encode所有的attributes不要造成遺露,為了user-extension,除非 除非 為了避免破壞 serialization format’s definition. 才進行忽略
一些屬性 commonly hreflang可能會顯示多過於一次在context中,因此 attribute的value可能是array,而不是其他的simple value, Serializer可能會encode array,到符合serialized format,
像是 space-separated list , or comma-separated list. etc. 假設給予的attribute,並不被允許有多重值,serializer必須使用第一個值,而忽略其他後面的值
假設attibute value is boolean true Serializer可能會簡寫他,如果有剛好適合的serialization format, 例如HTML permits attributes to have no alue when the attribute’s presence has a boolean meaning This rule applies if and only if the attribute is boolean true
, not for any other “truthy” value in PHP such as integer 1.
If an attribute value is boolean false
, serializers SHOULD 直接讓該值消失 除非false值對於結果有語意上的意義. This rule applies if and only if the attribute is boolean false
, not for any other “falsey” value in PHP such as integer 0.
1.3 Relationships
連結關係被定義為strings,且可以是公開定義關係的simple keyword 或者是absolute URL,表示private relationships
simple keyword
http://www.iana.org/assignments/link-relations/link-relations.xhtml
Optionally the microformats.org registry MAY be used, but this may not be valid in every context:
http://microformats.org/wiki/existing-rel-values
假設relationship沒有被定義在上述的內容之中,則為private,且必須包含一個absolute URI
1.4 Link Templates
RFC 6570 defines a format for URI templates, that is, a pattern for a URI that is expected to be filled in with values provided by a client tool. Some hypermedia formats support templated links while others do not, and may have a special way to denote that a link is a template. A Serializer for a format that does not support URI Templates MUST ignore any templated Links it encounters.
1.5 Evolvable providers
In some cases, a Link Provider may need the ability to have additional links added to it. In others, a link provider is necessarily read-only, with links derived at runtime from some other data source. For that reason, modifiable providers are a secondary interface that may optionally be implemented.
Additionally, some Link Provider objects, such as PSR-7 Response objects, are by design immutable. That means methods to add links to them in-place would be incompatible. Therefore, the EvolvableLinkProviderInterface
’s single method requires that a new object be returned, identical to the original but with an additional Link object included.
1.6 Evolvable link objects
Link objects are in most cases value objects. As such, allowing them to evolve in the same fashion as PSR-7 value objects is a useful option. For that reason, an additional EvolvableLinkInterface is included that provides methods to produce new object instances with a single change. The same model is used by PSR-7 and, thanks to PHP’s copy-on-write behavior, is still CPU and memory efficient.
There is no evolvable method for templated values, however, as the templated value of a link is based exclusively on the href value. It MUST NOT be set independently, but derived from whether or not the href value is an RFC 6570 link template.
2. Package
The interfaces and classes described are provided as part of the psr/link package.
3. Interfaces
3.1 Psr\Link\LinkInterface
https://www.php-fig.org/psr/psr-13/
留言列表