FeedWriter\Item::addEnclosure PHP Méthode

addEnclosure() public méthode

Not supported in RSS 1.0 feeds. See RFC 4288 for syntactical correct MIME types. Note that you should avoid the use of more than one enclosure in one item, since some RSS aggregators don't support it.
public addEnclosure ( string $url, integer $length, string $type, boolean $multiple = TRUE ) : self
$url string The URL of the media.
$length integer The length of the media.
$type string The MIME type attribute of the media.
$multiple boolean Specifies if multiple enclosures are allowed
Résultat self
    public function addEnclosure($url, $length, $type, $multiple = TRUE)
    {
        if ($this->version == Feed::RSS1) {
            throw new InvalidOperationException('Media attachment is not supported in RSS1 feeds.');
        }
        // the length parameter should be set to 0 if it can't be determined
        // see http://www.rssboard.org/rss-profile#element-channel-item-enclosure
        if (!is_numeric($length) || $length < 0) {
            throw new \InvalidArgumentException('The length parameter must be an integer and greater or equals to zero.');
        }
        // 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.');
        }
        $attributes = array('length' => $length, 'type' => $type);
        if ($this->version == Feed::RSS2) {
            $attributes['url'] = $url;
            $this->addElement('enclosure', '', $attributes, FALSE, $multiple);
        } else {
            $attributes['href'] = $url;
            $attributes['rel'] = 'enclosure';
            $this->addElement('atom:link', '', $attributes, FALSE, $multiple);
        }
        return $this;
    }