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