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