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.7 Red Velvet API

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