public function setAtomLink($href, $rel = null, $type = null, $hreflang = null, $title = null, $length = null)
{
$data = array('href' => $href);
if ($rel != null) {
if (!is_string($rel) || empty($rel)) {
throw new \InvalidArgumentException('rel parameter must be a string and a valid relation identifier.');
}
$data['rel'] = $rel;
}
if ($type != null) {
// Regex used from RFC 4287, page 41
if (!is_string($type) || preg_match('/.+\\/.+/', $type) != 1) {
throw new \InvalidArgumentException('type parameter must be a string and a MIME type.');
}
$data['type'] = $type;
}
if ($hreflang != null) {
// Regex used from RFC 4287, page 41
if (!is_string($hreflang) || preg_match('/[A-Za-z]{1,8}(-[A-Za-z0-9]{1,8})*/', $hreflang) != 1) {
throw new \InvalidArgumentException('hreflang parameter must be a string and a valid language code.');
}
$data['hreflang'] = $hreflang;
}
if ($title != null) {
if (!is_string($title) || empty($title)) {
throw new \InvalidArgumentException('title parameter must be a string and not empty.');
}
$data['title'] = $title;
}
if ($length != null) {
if (!is_int($length) || $length < 0) {
throw new \InvalidArgumentException('length parameter must be a positive integer.');
}
$data['length'] = (string) $length;
}
// ATOM spec. has some restrictions on atom:link usage
// See RFC 4287, page 12 (4.1.1)
if ($this->version == Feed::ATOM) {
foreach ($this->channels as $key => $value) {
if ($key != 'atom:link') {
continue;
}
// $value is an array , so check every element
foreach ($value as $linkItem) {
$attrib = $linkItem['attributes'];
// Only one link with relation alternate and same hreflang & type is allowed.
if (@$attrib['rel'] == 'alternate' && @$attrib['hreflang'] == $hreflang && @$attrib['type'] == $type) {
throw new InvalidOperationException('The feed must not contain more than one link element with a' . ' relation of "alternate" that has the same combination of type and hreflang attribute values.');
}
}
}
}
return $this->setChannelElement('atom:link', '', $data, true);
}