Chris@18: setAbsolute()->toString(TRUE); Chris@18: $this->href = $generated_url->getGeneratedUrl(); Chris@18: $this->uri = $url; Chris@18: $this->rel = $link_relation_types; Chris@18: $this->attributes = $target_attributes; Chris@18: $this->setCacheability($cacheability->addCacheableDependency($generated_url)); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Gets the link's URI. Chris@18: * Chris@18: * @return \Drupal\Core\Url Chris@18: * The link's URI as a Url object. Chris@18: */ Chris@18: public function getUri() { Chris@18: return $this->uri; Chris@18: } Chris@18: Chris@18: /** Chris@18: * Gets the link's URI as a string. Chris@18: * Chris@18: * @return string Chris@18: * The link's URI as a string. Chris@18: */ Chris@18: public function getHref() { Chris@18: return $this->href; Chris@18: } Chris@18: Chris@18: /** Chris@18: * Gets the link's relation types. Chris@18: * Chris@18: * @return string[] Chris@18: * The link's relation types. Chris@18: */ Chris@18: public function getLinkRelationTypes() { Chris@18: return $this->rel; Chris@18: } Chris@18: Chris@18: /** Chris@18: * Gets the link's target attributes. Chris@18: * Chris@18: * @return string[] Chris@18: * The link's target attributes. Chris@18: */ Chris@18: public function getTargetAttributes() { Chris@18: return $this->attributes; Chris@18: } Chris@18: Chris@18: /** Chris@18: * Compares two links by their href. Chris@18: * Chris@18: * @param \Drupal\jsonapi\JsonApiResource\Link $a Chris@18: * The first link. Chris@18: * @param \Drupal\jsonapi\JsonApiResource\Link $b Chris@18: * The second link. Chris@18: * Chris@18: * @return int Chris@18: * The result of strcmp() on the links' hrefs. Chris@18: */ Chris@18: public static function compare(Link $a, Link $b) { Chris@18: return strcmp($a->getHref(), $b->getHref()); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Merges two link objects' relation types and target attributes. Chris@18: * Chris@18: * The links must share the same URI. Chris@18: * Chris@18: * @param \Drupal\jsonapi\JsonApiResource\Link $a Chris@18: * The first link. Chris@18: * @param \Drupal\jsonapi\JsonApiResource\Link $b Chris@18: * The second link. Chris@18: * Chris@18: * @return static Chris@18: * A new JSON:API Link object with the link relation type and target Chris@18: * attributes merged. Chris@18: */ Chris@18: public static function merge(Link $a, Link $b) { Chris@18: assert(static::compare($a, $b) === 0); Chris@18: $merged_rels = array_unique(array_merge($a->getLinkRelationTypes(), $b->getLinkRelationTypes())); Chris@18: $merged_attributes = $a->getTargetAttributes(); Chris@18: foreach ($b->getTargetAttributes() as $key => $value) { Chris@18: if (isset($merged_attributes[$key])) { Chris@18: // The attribute values can be either a string or an array of strings. Chris@18: $value = array_unique(array_merge( Chris@18: is_string($merged_attributes[$key]) ? [$merged_attributes[$key]] : $merged_attributes[$key], Chris@18: is_string($value) ? [$value] : $value Chris@18: )); Chris@18: } Chris@18: $merged_attributes[$key] = count($value) === 1 ? reset($value) : $value; Chris@18: } Chris@18: $merged_cacheability = (new CacheableMetadata())->addCacheableDependency($a)->addCacheableDependency($b); Chris@18: return new static($merged_cacheability, $a->getUri(), $merged_rels, $merged_attributes); Chris@18: } Chris@18: Chris@18: }