miniflux-legacy/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Item.php

231 lines
3.9 KiB
PHP
Raw Normal View History

2014-05-20 20:20:27 +02:00
<?php
namespace PicoFeed\Parser;
2014-05-20 20:20:27 +02:00
/**
* Feed Item.
2014-05-20 20:20:27 +02:00
*
* @author Frederic Guillot
*/
class Item
{
/**
* List of known RTL languages.
*
* @var public
*/
public $rtl = array(
'ar', // Arabic (ar-**)
'fa', // Farsi (fa-**)
'ur', // Urdu (ur-**)
'ps', // Pashtu (ps-**)
'syr', // Syriac (syr-**)
'dv', // Divehi (dv-**)
'he', // Hebrew (he-**)
'yi', // Yiddish (yi-**)
);
2014-05-20 20:20:27 +02:00
/**
* Item id.
2014-05-20 20:20:27 +02:00
*
* @var string
*/
public $id = '';
/**
* Item title.
2014-05-20 20:20:27 +02:00
*
* @var string
*/
public $title = '';
/**
* Item url.
2014-05-20 20:20:27 +02:00
*
* @var string
*/
public $url = '';
/**
* Item author.
2014-05-20 20:20:27 +02:00
*
* @var string
*/
public $author = '';
2014-05-20 20:20:27 +02:00
/**
* Item date.
2014-05-20 20:20:27 +02:00
*
2015-03-01 19:56:11 +01:00
* @var \DateTime
2014-05-20 20:20:27 +02:00
*/
2015-03-01 19:56:11 +01:00
public $date = null;
2014-05-20 20:20:27 +02:00
/**
* Item content.
2014-05-20 20:20:27 +02:00
*
* @var string
*/
public $content = '';
/**
* Item enclosure url.
2014-05-20 20:20:27 +02:00
*
* @var string
*/
public $enclosure_url = '';
/**
* Item enclusure type.
2014-05-20 20:20:27 +02:00
*
* @var string
*/
public $enclosure_type = '';
/**
* Item language.
2014-05-20 20:20:27 +02:00
*
* @var string
*/
public $language = '';
2015-03-26 00:59:41 +01:00
/**
* Raw XML.
2015-03-26 00:59:41 +01:00
*
* @var \SimpleXMLElement
*/
public $xml;
/**
* List of namespaces.
2015-03-26 00:59:41 +01:00
*
* @var array
*/
public $namespaces = array();
/**
* Get specific XML tag or attribute value.
2015-03-26 00:59:41 +01:00
*
* @param string $tag Tag name (examples: guid, media:content)
* @param string $attribute Tag attribute
*
* @return array|false Tag values or error
2015-03-26 00:59:41 +01:00
*/
public function getTag($tag, $attribute = '')
{
2015-07-19 17:19:26 +02:00
// convert to xPath attribute query
if ($attribute !== '') {
$attribute = '/@'.$attribute;
2015-03-26 00:59:41 +01:00
}
2015-07-19 17:19:26 +02:00
// construct query
$query = './/'.$tag.$attribute;
$elements = XmlParser::getXPathResult($this->xml, $query, $this->namespaces);
if ($elements === false) { // xPath error
return false;
2015-03-26 00:59:41 +01:00
}
2015-07-19 17:19:26 +02:00
return array_map(function ($element) { return (string) $element;}, $elements);
2015-03-26 00:59:41 +01:00
}
2014-05-20 20:20:27 +02:00
/**
* Return item information.
2014-05-20 20:20:27 +02:00
*/
public function __toString()
{
$output = '';
2015-03-01 19:56:11 +01:00
foreach (array('id', 'title', 'url', 'language', 'author', 'enclosure_url', 'enclosure_type') as $property) {
2014-05-20 20:20:27 +02:00
$output .= 'Item::'.$property.' = '.$this->$property.PHP_EOL;
}
2015-03-01 19:56:11 +01:00
$output .= 'Item::date = '.$this->date->format(DATE_RFC822).PHP_EOL;
$output .= 'Item::isRTL() = '.($this->isRTL() ? 'true' : 'false').PHP_EOL;
2014-05-20 20:20:27 +02:00
$output .= 'Item::content = '.strlen($this->content).' bytes'.PHP_EOL;
return $output;
}
/**
* Get title.
2014-05-20 20:20:27 +02:00
*/
public function getTitle()
{
return $this->title;
}
/**
* Get url.
2014-05-20 20:20:27 +02:00
*/
public function getUrl()
{
return $this->url;
}
/**
* Get id.
2014-05-20 20:20:27 +02:00
*/
public function getId()
{
return $this->id;
}
/**
* Get date.
2014-05-20 20:20:27 +02:00
*/
public function getDate()
{
return $this->date;
}
/**
* Get content.
2014-05-20 20:20:27 +02:00
*/
public function getContent()
{
return $this->content;
}
/**
* Get enclosure url.
2014-05-20 20:20:27 +02:00
*/
public function getEnclosureUrl()
{
return $this->enclosure_url;
}
/**
* Get enclosure type.
2014-05-20 20:20:27 +02:00
*/
public function getEnclosureType()
{
return $this->enclosure_type;
}
/**
* Get language.
2014-05-20 20:20:27 +02:00
*/
public function getLanguage()
{
return $this->language;
}
/**
* Get author.
2014-05-20 20:20:27 +02:00
*/
public function getAuthor()
{
return $this->author;
}
/**
* Return true if the item is "Right to Left".
*
* @return bool
*/
public function isRTL()
{
return Parser::isLanguageRTL($this->language);
}
2014-05-20 20:20:27 +02:00
}