Item.php 9.9 KB

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