Skip to content
Permalink
Browse files

Change to RssHelper use Xml class instead XmlHelper.

  • Loading branch information...
jrbasso committed Jul 28, 2010
1 parent df12e5b commit 8f1bdd5378f268a93f466d3430045cc638958324
Showing with 63 additions and 71 deletions.
  1. +62 −6 cake/libs/view/helpers/rss.php
  2. +1 −65 cake/tests/cases/libs/view/helpers/rss.test.php
@@ -17,18 +17,16 @@
* @since CakePHP(tm) v 1.2
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::import('Helper', 'Xml');
App::import('Core', 'Xml');
/**
* XML Helper class for easy output of XML structures.
*
* XmlHelper encloses all methods needed while working with XML documents.
* RSS Helper class for easy output RSS structures.
*
* @package cake
* @subpackage cake.cake.libs.view.helpers
* @link http://book.cakephp.org/view/1460/RSS
*/
class RssHelper extends XmlHelper {
class RssHelper extends AppHelper {
/**
* Helpers used by RSS Helper
@@ -270,7 +268,7 @@ public function item($att = array(), $elements = array()) {
if (!empty($elements)) {
$content = implode('', $elements);
}
return $this->elem('item', $att, $content, !($content === null));
return $this->elem('item', (array)$att, $content, !($content === null));
}
/**
@@ -283,4 +281,62 @@ public function item($att = array(), $elements = array()) {
function time($time) {
return $this->Time->toRSS($time);
}
/**
* Generates an XML element
*
* @param string $name The name of the XML element
* @param array $attrib The attributes of the XML element
* @param mixed $content XML element content
* @param boolean $endTag Whether the end tag of the element should be printed
* @return string XML
*/
function elem($name, $attrib = array(), $content = null, $endTag = true) {
$namespace = null;
if (isset($attrib['namespace'])) {
$namespace = $attrib['namespace'];
unset($attrib['namespace']);
}
$cdata = false;
if (is_array($content) && isset($content['cdata'])) {
$cdata = true;
unset($content['cdata']);
}
if (is_array($content) && array_key_exists('value', $content)) {
$content = $content['value'];
}
$children = array();
if (is_array($content)) {
$children = $content;
$content = null;
}
$xml = '<' . $name;
if (!empty($namespace)) {
$xml .= ' xmlns:"' . $namespace . '"';
}
if (strpos($name, ':') !== false) {
list($prefix, ) = explode(':', $name, 2);
switch ($prefix) {
case 'atom':
$xml .= ' xmlns:atom="http://www.w3.org/2005/Atom"';
break;
}
}
if ($cdata && !empty($content)) {
$content = '<![CDATA[' . $content . ']]>';
}
$xml .= '>' . $content . '</' . $name. '>';
$elem = Xml::build($xml);
foreach ($attrib as $key => $value) {
$elem->addAttribute($key, $value);
}
foreach ($children as $child) {
$elem->addChild($child);
}
$xml = $elem->asXML();
$xml = trim(substr($xml, strpos($xml, '?>') + 2));
return $xml;
}
}
@@ -37,9 +37,6 @@ function setUp() {
$this->Rss =& new RssHelper();
$this->Rss->Time =& new TimeHelper();
$this->Rss->beforeRender();
$manager =& XmlManager::getInstance();
$manager->namespaces = array();
}
/**
@@ -53,52 +50,6 @@ function tearDown() {
}
/**
* testAddNamespace method
*
* @access public
* @return void
*/
function testAddNamespace() {
$this->Rss->addNs('custom', 'http://example.com/dtd.xml');
$manager =& XmlManager::getInstance();
$expected = array('custom' => 'http://example.com/dtd.xml');
$this->assertEqual($manager->namespaces, $expected);
$this->Rss->removeNs('custom');
$this->Rss->addNs('dummy', 'http://dummy.com/1.0/');
$result = $this->Rss->document();
$expected = array(
'rss' => array(
'xmlns:dummy' => 'http://dummy.com/1.0/',
'version' => '2.0'
)
);
$this->assertTags($result, $expected);
$this->Rss->removeNs('dummy');
}
/**
* testRemoveNamespace method
*
* @access public
* @return void
*/
function testRemoveNamespace() {
$this->Rss->addNs('custom', 'http://example.com/dtd.xml');
$this->Rss->addNs('custom2', 'http://example.com/dtd2.xml');
$manager =& XmlManager::getInstance();
$expected = array('custom' => 'http://example.com/dtd.xml', 'custom2' => 'http://example.com/dtd2.xml');
$this->assertEqual($manager->namespaces, $expected);
$this->Rss->removeNs('custom');
$expected = array('custom2' => 'http://example.com/dtd2.xml');
$this->assertEqual($manager->namespaces, $expected);
}
/**
* testDocument method
*
* @access public
@@ -252,6 +203,7 @@ function testChannelElementAttributes() {
'<link', 'http://example.com', '/link',
'/image',
'atom:link' => array(
'xmlns:atom' => 'http://www.w3.org/2005/Atom',
'href' => "http://www.example.com/rss.xml",
'rel' => "self",
'type' =>"application/rss+xml"
@@ -386,22 +338,6 @@ function testItem() {
);
$this->assertTags($result, $expected);
$item = array(
'title' => array(
'value' => 'My Title & more',
'convertEntities' => false
)
);
$result = $this->Rss->item(null, $item);
$expected = array(
'<item',
'<title',
'My Title & more',
'/title',
'/item'
);
$this->assertTags($result, $expected);
$item = array(
'title' => array(
'value' => 'My Title & more',

0 comments on commit 8f1bdd5

Please sign in to comment.
You can’t perform that action at this time.