FeedHtmlField.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * A FeedHtmlField describes and generates
  4. * a feed, item or image html field (probably a description). Output is
  5. * generated based on $truncSize, $syndicateHtml properties.
  6. *
  7. * @author Pascal Van Hecke <feedcreator.class.php@vanhecke.info>
  8. * @version 1.6
  9. */
  10. class FeedHtmlField
  11. {
  12. /**
  13. * Mandatory attributes of a FeedHtmlField.
  14. */
  15. protected $rawFieldContent;
  16. /**
  17. * Optional attributes of a FeedHtmlField.
  18. */
  19. public $truncSize, $syndicateHtml;
  20. /**
  21. * Creates a new instance of FeedHtmlField.
  22. *
  23. * @param string $parFieldContent if given, sets the rawFieldContent property
  24. */
  25. public function __construct($parFieldContent)
  26. {
  27. if ($parFieldContent) {
  28. $this->rawFieldContent = $parFieldContent;
  29. }
  30. }
  31. /**
  32. * Creates the right output, depending on $truncSize, $syndicateHtml properties.
  33. *
  34. * @return string the formatted field
  35. */
  36. public function output()
  37. {
  38. // when field available and syndicated in html we assume
  39. // - valid html in $rawFieldContent and we enclose in CDATA tags
  40. // - no truncation (truncating risks producing invalid html)
  41. if (!$this->rawFieldContent) {
  42. $result = "";
  43. } elseif ($this->syndicateHtml) {
  44. $result = "<![CDATA[".$this->rawFieldContent."]]>";
  45. } else {
  46. if ($this->truncSize and is_int($this->truncSize)) {
  47. $result = FeedCreator::iTrunc(htmlspecialchars($this->rawFieldContent), $this->truncSize);
  48. } else {
  49. $result = htmlspecialchars($this->rawFieldContent);
  50. }
  51. }
  52. return $result;
  53. }
  54. }