Change to RssHelper use Xml class instead XmlHelper.

This commit is contained in:
Juan Basso 2010-07-28 17:47:02 -03:00
parent df12e5b4ab
commit 8f1bdd5378
2 changed files with 63 additions and 71 deletions

View file

@ -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 @@ class RssHelper extends XmlHelper {
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 @@ class RssHelper extends XmlHelper {
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;
}
}

View file

@ -37,9 +37,6 @@ class RssHelperTest extends CakeTestCase {
$this->Rss =& new RssHelper();
$this->Rss->Time =& new TimeHelper();
$this->Rss->beforeRender();
$manager =& XmlManager::getInstance();
$manager->namespaces = array();
}
/**
@ -53,52 +50,6 @@ class RssHelperTest extends CakeTestCase {
}
/**
* 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 @@ class RssHelperTest extends CakeTestCase {
'<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 @@ class RssHelperTest extends CakeTestCase {
);
$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',