CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • BreadcrumbsHelper
  • FlashHelper
  • FormHelper
  • HtmlHelper
  • NumberHelper
  • PaginatorHelper
  • RssHelper
  • SessionHelper
  • TextHelper
  • TimeHelper
  • UrlHelper

Traits

  • IdGeneratorTrait
  • SecureFieldTokenTrait
  1: <?php
  2: /**
  3:  * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4:  * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5:  *
  6:  * Licensed under The MIT License
  7:  * For full copyright and license information, please see the LICENSE.txt
  8:  * Redistributions of files must retain the above copyright notice.
  9:  *
 10:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 11:  * @link          https://cakephp.org CakePHP(tm) Project
 12:  * @since         1.2.0
 13:  * @license       https://opensource.org/licenses/mit-license.php MIT License
 14:  */
 15: namespace Cake\View\Helper;
 16: 
 17: use Cake\Utility\Xml;
 18: use Cake\View\Helper;
 19: use Cake\View\View;
 20: 
 21: /**
 22:  * RSS Helper class for easy output RSS structures.
 23:  *
 24:  * @property \Cake\View\Helper\UrlHelper $Url
 25:  * @property \Cake\View\Helper\TimeHelper $Time
 26:  * @link https://book.cakephp.org/3.0/en/views/helpers/rss.html
 27:  * @deprecated 3.5.0 RssHelper is deprecated and will be removed in 4.0.0
 28:  */
 29: class RssHelper extends Helper
 30: {
 31:     /**
 32:      * Helpers used by RSS Helper
 33:      *
 34:      * @var array
 35:      */
 36:     public $helpers = ['Url', 'Time'];
 37: 
 38:     /**
 39:      * Base URL
 40:      *
 41:      * @var string
 42:      */
 43:     public $base;
 44: 
 45:     /**
 46:      * URL to current action.
 47:      *
 48:      * @var string
 49:      */
 50:     public $here;
 51: 
 52:     /**
 53:      * Parameter array.
 54:      *
 55:      * @var array
 56:      */
 57:     public $params = [];
 58: 
 59:     /**
 60:      * Current action.
 61:      *
 62:      * @var string
 63:      */
 64:     public $action;
 65: 
 66:     /**
 67:      * POSTed model data
 68:      *
 69:      * @var array
 70:      */
 71:     public $data;
 72: 
 73:     /**
 74:      * Name of the current model
 75:      *
 76:      * @var string
 77:      */
 78:     public $model;
 79: 
 80:     /**
 81:      * Name of the current field
 82:      *
 83:      * @var string
 84:      */
 85:     public $field;
 86: 
 87:     /**
 88:      * Default spec version of generated RSS
 89:      *
 90:      * @var string
 91:      */
 92:     public $version = '2.0';
 93: 
 94:     /**
 95:      * {@inheritDoc}
 96:      */
 97:     public function __construct(View $view, array $settings = [])
 98:     {
 99:         deprecationWarning('RssHelper is deprecated and will be removed in 4.0.0');
100:         parent::__construct($view, $settings);
101:     }
102: 
103:     /**
104:      * Returns an RSS document wrapped in `<rss />` tags
105:      *
106:      * @param array $attrib `<rss />` tag attributes
107:      * @param string|null $content Tag content.
108:      * @return string An RSS document
109:      */
110:     public function document($attrib = [], $content = null)
111:     {
112:         if ($content === null) {
113:             $content = $attrib;
114:             $attrib = [];
115:         }
116:         if (!isset($attrib['version']) || empty($attrib['version'])) {
117:             $attrib['version'] = $this->version;
118:         }
119: 
120:         return $this->elem('rss', $attrib, $content);
121:     }
122: 
123:     /**
124:      * Returns an RSS `<channel />` element
125:      *
126:      * @param array $attrib `<channel />` tag attributes
127:      * @param array $elements Named array elements which are converted to tags
128:      * @param string|null $content Content (`<item />`'s belonging to this channel
129:      * @return string An RSS `<channel />`
130:      */
131:     public function channel($attrib = [], $elements = [], $content = null)
132:     {
133:         if (!isset($elements['link'])) {
134:             $elements['link'] = '/';
135:         }
136:         if (!isset($elements['title'])) {
137:             $elements['title'] = '';
138:         }
139:         if (!isset($elements['description'])) {
140:             $elements['description'] = '';
141:         }
142:         $elements['link'] = $this->Url->build($elements['link'], true);
143: 
144:         $elems = '';
145:         foreach ($elements as $elem => $data) {
146:             $attributes = [];
147:             if (is_array($data)) {
148:                 if (strtolower($elem) === 'cloud') {
149:                     $attributes = $data;
150:                     $data = [];
151:                 } elseif (isset($data['attrib']) && is_array($data['attrib'])) {
152:                     $attributes = $data['attrib'];
153:                     unset($data['attrib']);
154:                 } else {
155:                     $innerElements = '';
156:                     foreach ($data as $subElement => $value) {
157:                         $innerElements .= $this->elem($subElement, [], $value);
158:                     }
159:                     $data = $innerElements;
160:                 }
161:             }
162:             $elems .= $this->elem($elem, $attributes, $data);
163:         }
164: 
165:         return $this->elem('channel', $attrib, $elems . $content, !($content === null));
166:     }
167: 
168:     /**
169:      * Transforms an array of data using an optional callback, and maps it to a set
170:      * of `<item />` tags
171:      *
172:      * @param array $items The list of items to be mapped
173:      * @param string|array|null $callback A string function name, or array containing an object
174:      *     and a string method name
175:      * @return string A set of RSS `<item />` elements
176:      */
177:     public function items($items, $callback = null)
178:     {
179:         if ($callback) {
180:             $items = array_map($callback, $items);
181:         }
182: 
183:         $out = '';
184:         $c = count($items);
185: 
186:         for ($i = 0; $i < $c; $i++) {
187:             $out .= $this->item([], $items[$i]);
188:         }
189: 
190:         return $out;
191:     }
192: 
193:     /**
194:      * Converts an array into an `<item />` element and its contents
195:      *
196:      * @param array $att The attributes of the `<item />` element
197:      * @param array $elements The list of elements contained in this `<item />`
198:      * @return string An RSS `<item />` element
199:      */
200:     public function item($att = [], $elements = [])
201:     {
202:         $content = null;
203: 
204:         if (isset($elements['link']) && !isset($elements['guid'])) {
205:             $elements['guid'] = $elements['link'];
206:         }
207: 
208:         foreach ($elements as $key => $val) {
209:             $attrib = [];
210: 
211:             $escape = true;
212:             if (is_array($val) && isset($val['convertEntities'])) {
213:                 $escape = $val['convertEntities'];
214:                 unset($val['convertEntities']);
215:             }
216: 
217:             switch ($key) {
218:                 case 'pubDate':
219:                     $val = $this->time($val);
220:                     break;
221:                 case 'category':
222:                     if (is_array($val) && !empty($val[0])) {
223:                         $categories = [];
224:                         foreach ($val as $category) {
225:                             $attrib = [];
226:                             if (is_array($category) && isset($category['domain'])) {
227:                                 $attrib['domain'] = $category['domain'];
228:                                 unset($category['domain']);
229:                             }
230:                             $categories[] = $this->elem($key, $attrib, $category);
231:                         }
232:                         $elements[$key] = implode('', $categories);
233:                         continue 2;
234:                     }
235:                     if (is_array($val) && isset($val['domain'])) {
236:                         $attrib['domain'] = $val['domain'];
237:                     }
238:                     break;
239:                 case 'link':
240:                 case 'guid':
241:                 case 'comments':
242:                     if (is_array($val) && isset($val['url'])) {
243:                         $attrib = $val;
244:                         unset($attrib['url']);
245:                         $val = $val['url'];
246:                     }
247:                     $val = $this->Url->build($val, true);
248:                     break;
249:                 case 'source':
250:                     if (is_array($val) && isset($val['url'])) {
251:                         $attrib['url'] = $this->Url->build($val['url'], true);
252:                         $val = $val['title'];
253:                     } elseif (is_array($val)) {
254:                         $attrib['url'] = $this->Url->build($val[0], true);
255:                         $val = $val[1];
256:                     }
257:                     break;
258:                 case 'enclosure':
259:                     if (is_string($val['url']) && is_file(WWW_ROOT . $val['url']) && file_exists(WWW_ROOT . $val['url'])) {
260:                         if (!isset($val['length']) && strpos($val['url'], '://') === false) {
261:                             $val['length'] = sprintf('%u', filesize(WWW_ROOT . $val['url']));
262:                         }
263:                         if (!isset($val['type']) && function_exists('mime_content_type')) {
264:                             $val['type'] = mime_content_type(WWW_ROOT . $val['url']);
265:                         }
266:                     }
267:                     $val['url'] = $this->Url->build($val['url'], true);
268:                     $attrib = $val;
269:                     $val = null;
270:                     break;
271:                 default:
272:                     $attrib = $att;
273:             }
274:             if ($val !== null && $escape) {
275:                 $val = h($val);
276:             }
277:             $elements[$key] = $this->elem($key, $attrib, $val);
278:         }
279:         if (!empty($elements)) {
280:             $content = implode('', $elements);
281:         }
282: 
283:         return $this->elem('item', (array)$att, $content, !($content === null));
284:     }
285: 
286:     /**
287:      * Converts a time in any format to an RSS time
288:      *
289:      * @param int|string|\DateTime $time UNIX timestamp or valid time string or DateTime object.
290:      * @return string An RSS-formatted timestamp
291:      * @see \Cake\View\Helper\TimeHelper::toRSS
292:      */
293:     public function time($time)
294:     {
295:         return $this->Time->toRss($time);
296:     }
297: 
298:     /**
299:      * Generates an XML element
300:      *
301:      * @param string $name The name of the XML element
302:      * @param array $attrib The attributes of the XML element
303:      * @param string|array|null $content XML element content
304:      * @param bool $endTag Whether the end tag of the element should be printed
305:      * @return string XML
306:      */
307:     public function elem($name, $attrib = [], $content = null, $endTag = true)
308:     {
309:         $namespace = null;
310:         if (isset($attrib['namespace'])) {
311:             $namespace = $attrib['namespace'];
312:             unset($attrib['namespace']);
313:         }
314:         $cdata = false;
315:         if (is_array($content) && isset($content['cdata'])) {
316:             $cdata = true;
317:             unset($content['cdata']);
318:         }
319:         if (is_array($content) && array_key_exists('value', $content)) {
320:             $content = $content['value'];
321:         }
322:         $children = [];
323:         if (is_array($content)) {
324:             $children = $content;
325:             $content = null;
326:         }
327: 
328:         $xml = '<' . $name;
329:         if (!empty($namespace)) {
330:             $xml .= ' xmlns';
331:             if (is_array($namespace)) {
332:                 $xml .= ':' . $namespace['prefix'];
333:                 $namespace = $namespace['url'];
334:             }
335:             $xml .= '="' . $namespace . '"';
336:         }
337:         $bareName = $name;
338:         if (strpos($name, ':') !== false) {
339:             list($prefix, $bareName) = explode(':', $name, 2);
340:             switch ($prefix) {
341:                 case 'atom':
342:                     $xml .= ' xmlns:atom="http://www.w3.org/2005/Atom"';
343:                     break;
344:             }
345:         }
346:         if ($cdata && !empty($content)) {
347:             $content = '<![CDATA[' . $content . ']]>';
348:         }
349:         $xml .= '>' . $content . '</' . $name . '>';
350:         $elem = Xml::build($xml, ['return' => 'domdocument']);
351:         $nodes = $elem->getElementsByTagName($bareName);
352:         if ($attrib) {
353:             foreach ($attrib as $key => $value) {
354:                 $nodes->item(0)->setAttribute($key, $value);
355:             }
356:         }
357:         foreach ($children as $child) {
358:             $child = $elem->createElement($name, $child);
359:             $nodes->item(0)->appendChild($child);
360:         }
361: 
362:         $xml = $elem->saveXml();
363:         $xml = trim(substr($xml, strpos($xml, '?>') + 2));
364: 
365:         return $xml;
366:     }
367: 
368:     /**
369:      * Event listeners.
370:      *
371:      * @return array
372:      */
373:     public function implementedEvents()
374:     {
375:         return [];
376:     }
377: }
378: 
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs