Item.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. <?php
  2. namespace PicoFeed\Parser;
  3. /**
  4. * Feed Item.
  5. *
  6. * @package PicoFeed\Parser
  7. * @author Frederic Guillot
  8. */
  9. class Item
  10. {
  11. /**
  12. * List of known RTL languages.
  13. *
  14. * @var string[]
  15. */
  16. public $rtl = array(
  17. 'ar', // Arabic (ar-**)
  18. 'fa', // Farsi (fa-**)
  19. 'ur', // Urdu (ur-**)
  20. 'ps', // Pashtu (ps-**)
  21. 'syr', // Syriac (syr-**)
  22. 'dv', // Divehi (dv-**)
  23. 'he', // Hebrew (he-**)
  24. 'yi', // Yiddish (yi-**)
  25. );
  26. /**
  27. * Item id.
  28. *
  29. * @var string
  30. */
  31. public $id = '';
  32. /**
  33. * Item title.
  34. *
  35. * @var string
  36. */
  37. public $title = '';
  38. /**
  39. * Item url.
  40. *
  41. * @var string
  42. */
  43. public $url = '';
  44. /**
  45. * Item author.
  46. *
  47. * @var string
  48. */
  49. public $author = '';
  50. /**
  51. * Item date.
  52. *
  53. * @var \DateTime
  54. */
  55. public $date = null;
  56. /**
  57. * Item published date.
  58. *
  59. * @var \DateTime
  60. */
  61. public $publishedDate = null;
  62. /**
  63. * Item updated date.
  64. *
  65. * @var \DateTime
  66. */
  67. public $updatedDate = null;
  68. /**
  69. * Item content.
  70. *
  71. * @var string
  72. */
  73. public $content = '';
  74. /**
  75. * Item enclosure url.
  76. *
  77. * @var string
  78. */
  79. public $enclosureUrl = '';
  80. /**
  81. * Item enclusure type.
  82. *
  83. * @var string
  84. */
  85. public $enclosureType = '';
  86. /**
  87. * Item language.
  88. *
  89. * @var string
  90. */
  91. public $language = '';
  92. /**
  93. * Item categories.
  94. *
  95. * @var array
  96. */
  97. public $categories = array();
  98. /**
  99. * Raw XML.
  100. *
  101. * @var \SimpleXMLElement
  102. */
  103. public $xml;
  104. /**
  105. * List of namespaces.
  106. *
  107. * @var array
  108. */
  109. public $namespaces = array();
  110. /**
  111. * Check if a XML namespace exists
  112. *
  113. * @access public
  114. * @param string $namespace
  115. * @return bool
  116. */
  117. public function hasNamespace($namespace)
  118. {
  119. return array_key_exists($namespace, $this->namespaces);
  120. }
  121. /**
  122. * Get specific XML tag or attribute value.
  123. *
  124. * @param string $tag Tag name (examples: guid, media:content)
  125. * @param string $attribute Tag attribute
  126. *
  127. * @return array|false Tag values or error
  128. */
  129. public function getTag($tag, $attribute = '')
  130. {
  131. if ($attribute !== '') {
  132. $attribute = '/@'.$attribute;
  133. }
  134. $query = './/'.$tag.$attribute;
  135. $elements = XmlParser::getXPathResult($this->xml, $query, $this->namespaces);
  136. if ($elements === false) { // xPath error
  137. return false;
  138. }
  139. return array_map(function ($element) { return (string) $element;}, $elements);
  140. }
  141. /**
  142. * Return item information.
  143. *
  144. * @return string
  145. */
  146. public function __toString()
  147. {
  148. $output = '';
  149. foreach (array('id', 'title', 'url', 'language', 'author', 'enclosureUrl', 'enclosureType') as $property) {
  150. $output .= 'Item::'.$property.' = '.$this->$property.PHP_EOL;
  151. }
  152. $publishedDate = $this->publishedDate != null ? $this->publishedDate->format(DATE_RFC822) : null;
  153. $updatedDate = $this->updatedDate != null ? $this->updatedDate->format(DATE_RFC822) : null;
  154. $categoryString = $this->categories != null ? implode(',', $this->categories) : null;
  155. $output .= 'Item::date = '.$this->date->format(DATE_RFC822).PHP_EOL;
  156. $output .= 'Item::publishedDate = '.$publishedDate.PHP_EOL;
  157. $output .= 'Item::updatedDate = '.$updatedDate.PHP_EOL;
  158. $output .= 'Item::isRTL() = '.($this->isRTL() ? 'true' : 'false').PHP_EOL;
  159. $output .= 'Item::categories = ['.$categoryString.']'.PHP_EOL;
  160. $output .= 'Item::content = '.strlen($this->content).' bytes'.PHP_EOL;
  161. return $output;
  162. }
  163. /**
  164. * Get title.
  165. *
  166. * @return string
  167. */
  168. public function getTitle()
  169. {
  170. return $this->title;
  171. }
  172. /**
  173. * Get URL
  174. *
  175. * @access public
  176. * @return string
  177. */
  178. public function getUrl()
  179. {
  180. return $this->url;
  181. }
  182. /**
  183. * Set URL
  184. *
  185. * @access public
  186. * @param string $url
  187. * @return Item
  188. */
  189. public function setUrl($url)
  190. {
  191. $this->url = $url;
  192. return $this;
  193. }
  194. /**
  195. * Get id.
  196. *
  197. * @return string
  198. */
  199. public function getId()
  200. {
  201. return $this->id;
  202. }
  203. /**
  204. * Get date.
  205. *
  206. * @return \DateTime
  207. */
  208. public function getDate()
  209. {
  210. return $this->date;
  211. }
  212. /**
  213. * Get published date.
  214. *
  215. * @return \DateTime
  216. */
  217. public function getPublishedDate()
  218. {
  219. return $this->publishedDate;
  220. }
  221. /**
  222. * Get updated date.
  223. *
  224. * @return \DateTime
  225. */
  226. public function getUpdatedDate()
  227. {
  228. return $this->updatedDate;
  229. }
  230. /**
  231. * Get content.
  232. *
  233. * @return string
  234. */
  235. public function getContent()
  236. {
  237. return $this->content;
  238. }
  239. /**
  240. * Set content
  241. *
  242. * @access public
  243. * @param string $value
  244. * @return Item
  245. */
  246. public function setContent($value)
  247. {
  248. $this->content = $value;
  249. return $this;
  250. }
  251. /**
  252. * Get enclosure url.
  253. *
  254. * @return string
  255. */
  256. public function getEnclosureUrl()
  257. {
  258. return $this->enclosureUrl;
  259. }
  260. /**
  261. * Get enclosure type.
  262. *
  263. * @return string
  264. */
  265. public function getEnclosureType()
  266. {
  267. return $this->enclosureType;
  268. }
  269. /**
  270. * Get language.
  271. *
  272. * @return string
  273. */
  274. public function getLanguage()
  275. {
  276. return $this->language;
  277. }
  278. /**
  279. * Get categories.
  280. *
  281. * @return array
  282. */
  283. public function getCategories()
  284. {
  285. return $this->categories;
  286. }
  287. /**
  288. * Get author.
  289. *
  290. * @return string
  291. */
  292. public function getAuthor()
  293. {
  294. return $this->author;
  295. }
  296. /**
  297. * Return true if the item is "Right to Left".
  298. *
  299. * @return bool
  300. */
  301. public function isRTL()
  302. {
  303. return Parser::isLanguageRTL($this->language);
  304. }
  305. /**
  306. * Set item id.
  307. *
  308. * @param string $id
  309. * @return Item
  310. */
  311. public function setId($id)
  312. {
  313. $this->id = $id;
  314. return $this;
  315. }
  316. /**
  317. * Set item title.
  318. *
  319. * @param string $title
  320. * @return Item
  321. */
  322. public function setTitle($title)
  323. {
  324. $this->title = $title;
  325. return $this;
  326. }
  327. /**
  328. * Set author.
  329. *
  330. * @param string $author
  331. * @return Item
  332. */
  333. public function setAuthor($author)
  334. {
  335. $this->author = $author;
  336. return $this;
  337. }
  338. /**
  339. * Set item date.
  340. *
  341. * @param \DateTime $date
  342. * @return Item
  343. */
  344. public function setDate($date)
  345. {
  346. $this->date = $date;
  347. return $this;
  348. }
  349. /**
  350. * Set item published date.
  351. *
  352. * @param \DateTime $publishedDate
  353. * @return Item
  354. */
  355. public function setPublishedDate($publishedDate)
  356. {
  357. $this->publishedDate = $publishedDate;
  358. return $this;
  359. }
  360. /**
  361. * Set item updated date.
  362. *
  363. * @param \DateTime $updatedDate
  364. * @return Item
  365. */
  366. public function setUpdatedDate($updatedDate)
  367. {
  368. $this->updatedDate = $updatedDate;
  369. return $this;
  370. }
  371. /**
  372. * Set enclosure url.
  373. *
  374. * @param string $enclosureUrl
  375. * @return Item
  376. */
  377. public function setEnclosureUrl($enclosureUrl)
  378. {
  379. $this->enclosureUrl = $enclosureUrl;
  380. return $this;
  381. }
  382. /**
  383. * Set enclosure type.
  384. *
  385. * @param string $enclosureType
  386. * @return Item
  387. */
  388. public function setEnclosureType($enclosureType)
  389. {
  390. $this->enclosureType = $enclosureType;
  391. return $this;
  392. }
  393. /**
  394. * Set item language.
  395. *
  396. * @param string $language
  397. * @return Item
  398. */
  399. public function setLanguage($language)
  400. {
  401. $this->language = $language;
  402. return $this;
  403. }
  404. /**
  405. * Set item categories.
  406. *
  407. * @param array $categories
  408. * @return Item
  409. */
  410. public function setCategories($categories)
  411. {
  412. $this->categories = $categories;
  413. return $this;
  414. }
  415. /**
  416. * Set item categories from xml.
  417. *
  418. * @param |SimpleXMLElement[] $categories
  419. * @return Item
  420. */
  421. public function setCategoriesFromXml($categories)
  422. {
  423. if ($categories !== false) {
  424. $this->setCategories(
  425. array_map(
  426. function ($element) {
  427. return trim((string) $element);
  428. },
  429. $categories
  430. )
  431. );
  432. }
  433. return $this;
  434. }
  435. /**
  436. * Set raw XML.
  437. *
  438. * @param \SimpleXMLElement $xml
  439. * @return Item
  440. */
  441. public function setXml($xml)
  442. {
  443. $this->xml = $xml;
  444. return $this;
  445. }
  446. /**
  447. * Get raw XML.
  448. *
  449. * @return \SimpleXMLElement
  450. */
  451. public function getXml()
  452. {
  453. return $this->xml;
  454. }
  455. /**
  456. * Set XML namespaces.
  457. *
  458. * @param array $namespaces
  459. * @return Item
  460. */
  461. public function setNamespaces($namespaces)
  462. {
  463. $this->namespaces = $namespaces;
  464. return $this;
  465. }
  466. /**
  467. * Get XML namespaces.
  468. *
  469. * @return array
  470. */
  471. public function getNamespaces()
  472. {
  473. return $this->namespaces;
  474. }
  475. }