All checks were successful
		
		
	
	🚀 Deploy website on push / 🎉 Deploy (push) Successful in 19s
				
			Signed-off-by: rodude123 <rodude123@gmail.com>
		
			
				
	
	
		
			153 lines
		
	
	
		
			4.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			153 lines
		
	
	
		
			4.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace api\utils\feedGenerator;
 | 
						|
 | 
						|
/**
 | 
						|
 * Universal Feed Writer
 | 
						|
 *
 | 
						|
 * FeedItem class - Used as a feed element in FeedWriter class
 | 
						|
 *
 | 
						|
 * @package         UniversalFeedWriter
 | 
						|
 * @author          Anis uddin Ahmad <anisniit@gmail.com>
 | 
						|
 * @link            http://www.ajaxray.com/projects/rss
 | 
						|
 */
 | 
						|
class FeedItem
 | 
						|
{
 | 
						|
    private array $elements = []; // Collection of feed elements
 | 
						|
    private string $version;
 | 
						|
 | 
						|
    /**
 | 
						|
     * Constructor
 | 
						|
     *
 | 
						|
     * @param string $version (RSS1/RSS2/ATOM) RSS2 is the default.
 | 
						|
     */
 | 
						|
    public function __construct(string $version = RSS2)
 | 
						|
    {
 | 
						|
        $this->version = $version;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Add an element to elements array
 | 
						|
     *
 | 
						|
     * @param string $elementName The tag name of an element
 | 
						|
     * @param string $content The content of the tag
 | 
						|
     * @param array|null $attributes Attributes (if any) in 'attrName' => 'attrValue' format
 | 
						|
     */
 | 
						|
    public function addElement(string $elementName, string $content, ?array $attributes = null): void
 | 
						|
    {
 | 
						|
        $this->elements[$elementName]['name'] = $elementName;
 | 
						|
        $this->elements[$elementName]['content'] = $content;
 | 
						|
        $this->elements[$elementName]['attributes'] = $attributes;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Set multiple feed elements from an array.
 | 
						|
     * Elements that have attributes cannot be added by this method
 | 
						|
     *
 | 
						|
     * @param array $elementArray Array of elements in 'tagName' => 'tagContent' format.
 | 
						|
     */
 | 
						|
    public function addElementArray(array $elementArray): void
 | 
						|
    {
 | 
						|
        foreach ($elementArray as $elementName => $content)
 | 
						|
        {
 | 
						|
            $this->addElement($elementName, $content);
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Return the collection of elements in this feed item
 | 
						|
     *
 | 
						|
     * @return array
 | 
						|
     */
 | 
						|
    public function getElements(): array
 | 
						|
    {
 | 
						|
        return $this->elements;
 | 
						|
    }
 | 
						|
 | 
						|
    // Wrapper functions ------------------------------------------------------
 | 
						|
 | 
						|
    /**
 | 
						|
     * Set the 'description' element of the feed item
 | 
						|
     *
 | 
						|
     * @param string $description The content of the 'description' element
 | 
						|
     */
 | 
						|
    public function setDescription(string $description): void
 | 
						|
    {
 | 
						|
        $tag = ($this->version === ATOM) ? 'summary' : 'description';
 | 
						|
        $this->addElement($tag, $description);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Set the 'title' element of the feed item
 | 
						|
     *
 | 
						|
     * @param string $title The content of the 'title' element
 | 
						|
     */
 | 
						|
    public function setTitle(string $title): void
 | 
						|
    {
 | 
						|
        $this->addElement('title', $title);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Set the 'date' element of the feed item
 | 
						|
     *
 | 
						|
     * @param string|int $date The content of the 'date' element
 | 
						|
     */
 | 
						|
    public function setDate(string|int $date): void
 | 
						|
    {
 | 
						|
        if (!is_numeric($date))
 | 
						|
        {
 | 
						|
            $date = strtotime($date);
 | 
						|
        }
 | 
						|
 | 
						|
        if ($this->version === ATOM)
 | 
						|
        {
 | 
						|
            $tag = 'updated';
 | 
						|
            $value = date(DATE_ATOM, $date);
 | 
						|
        }
 | 
						|
        elseif ($this->version === RSS2)
 | 
						|
        {
 | 
						|
            $tag = 'pubDate';
 | 
						|
            $value = date(DATE_RSS, $date);
 | 
						|
        }
 | 
						|
        else
 | 
						|
        {
 | 
						|
            $tag = 'dc:date';
 | 
						|
            $value = date("Y-m-d", $date);
 | 
						|
        }
 | 
						|
 | 
						|
        $this->addElement($tag, $value);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Set the 'link' element of the feed item
 | 
						|
     *
 | 
						|
     * @param string $link The content of the 'link' element
 | 
						|
     */
 | 
						|
    public function setLink(string $link): void
 | 
						|
    {
 | 
						|
        if ($this->version === RSS2 || $this->version === RSS1)
 | 
						|
        {
 | 
						|
            $this->addElement('link', $link);
 | 
						|
        }
 | 
						|
        else
 | 
						|
        {
 | 
						|
            $this->addElement('link', '', ['href' => $link]);
 | 
						|
            $this->addElement('id', FeedWriter::uuid($link, 'urn:uuid:'));
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Set the 'encloser' element of the feed item
 | 
						|
     * For RSS 2.0 only
 | 
						|
     *
 | 
						|
     * @param string $url The url attribute of the encloser tag
 | 
						|
     * @param string $length The length attribute of the encloser tag
 | 
						|
     * @param string $type The type attribute of the encloser tag
 | 
						|
     */
 | 
						|
    public function setEncloser(string $url, string $length, string $type): void
 | 
						|
    {
 | 
						|
        $attributes = ['url' => $url, 'length' => $length, 'type' => $type];
 | 
						|
        $this->addElement('enclosure', '', $attributes);
 | 
						|
    }
 | 
						|
}
 |