Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breadcrumb renderer #257

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 160 additions & 0 deletions src/Knp/Menu/Renderer/BreadcrumbRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php

namespace Knp\Menu\Renderer;

use Knp\Menu\ItemInterface;

/**
* Renders MenuItem tree as a breadcrumb
*/
class BreadcrumbRenderer extends Renderer implements RendererInterface
{
private $defaultOptions;

/**
* @param array $defaultOptions
* @param string $charset
*/
public function __construct(array $defaultOptions = array(), $charset = null)
{
$this->defaultOptions = array_merge(array(
'current_as_link' => true,
'current_class' => 'current',
'additional_path' => null,
'compressed' => false,
'allow_safe_labels' => false,
'root_attributes' => array(),
), $defaultOptions);

parent::__construct($charset);
}

/**
* Renders a menu with the specified renderer.
*
* @param \Knp\Menu\ItemInterface $item
* @param array $options
* @return string
*/
public function render(ItemInterface $item, array $options = array())
{
$options = array_merge($this->defaultOptions, $options);

$breadcrumb = $item->getBreadcrumbsArray($options['additional_path']);

if (empty($breadcrumb)) {
return '';
}

return $this->renderBreadcrumb($breadcrumb, $options);
}

/**
* Renders the breadcrumb
*
* @param array $breadcrumb
* @param array $options
* @return string
*/
protected function renderBreadcrumb(array $breadcrumb, array $options)
{
$html = $this->format('<ul'.$this->renderHtmlAttributes($options['root_attributes']).'>', 'ul', 0, $options);
$html .= $this->renderList($breadcrumb, $options);
$html .= $this->format('</ul>', 'ul', 0, $options);

return $html;
}

/**
* Renders the breadcrumb list
*
* @param array $breadcrumb
* @param array $options
* @return string
*/
protected function renderList(array $breadcrumb, array $options)
{
$html = '';
foreach ($breadcrumb as $element) {
$element = array_replace(array('label' => null, 'uri' => null, 'item' => null), $element);
$html .= $this->renderItem($element['label'], $element['uri'], $options, $element['item']);
}

return $html;
}

/**
* @param string $label
* @param string $uri
* @param array $options
* @param \Knp\Menu\ItemInterface|null $item
* @return string
*/
protected function renderItem($label, $uri, array $options, ItemInterface $item = null)
{
$isCurrent = null !== $item && $item->isCurrent();
$attributes = $isCurrent ? array('class' => $options['current_class']) : array();

// opening li tag
$html = $this->format('<li'.$this->renderHtmlAttributes($attributes).'>', 'li', 1, $options);

// render the text/link inside the li tag
if (null === $uri || ($isCurrent && !$options['current_as_link'])) {
$content = $this->renderLabel($label, $options, $item);
} else {
$content = sprintf('<a href="%s">%s</a>', $this->escape($uri), $this->renderLabel($label, $options, $item));
}
$html .= $this->format($content, 'link', 1, $options);

// closing li tag
$html .= $this->format('</li>', 'li', 1, $options);

return $html;
}

/**
* @param string $label
* @param array $options
* @param \Knp\Menu\ItemInterface|null $item
* @return string
*/
protected function renderLabel($label, array $options, ItemInterface $item = null)
{
if ($options['allow_safe_labels'] && null !== $item && $item->getExtra('safe_label', false)) {
return $item->getLabel();
}

return $this->escape($label);
}

/**
* If $this->renderCompressed is on, this will apply the necessary
* spacing and line-breaking so that the particular thing being rendered
* makes up its part in a fully-rendered and spaced menu.
*
* @param string $html The html to render in an (un)formatted way
* @param string $type The type [ul,link,li] of thing being rendered
* @param integer $level
* @param array $options
* @return string
*/
protected function format($html, $type, $level, array $options)
{
if ($options['compressed']) {
return $html;
}

switch ($type) {
case 'ul':
case 'link':
$spacing = $level * 4;
break;

case 'li':
$spacing = $level * 4 - 2;
break;
}

return str_repeat(' ', $spacing).$html."\n";
}
}
115 changes: 115 additions & 0 deletions tests/Knp/Menu/Tests/Renderer/BreadcrumbRendererTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace Knp\Menu\Tests\Renderer;

use Knp\Menu\Renderer\BreadcrumbRenderer;
use Knp\Menu\Tests\TestCase;

class BreadcrumbRendererTest extends TestCase
{
public function testBreadcrumbRendering()
{
$renderer = new BreadcrumbRenderer(array('compressed' => true));

$expected = '<ul><li>Root li</li><li>Parent 1</li><li>Child 1</li></ul>';

$this->assertEquals($expected, $renderer->render($this->ch1));
}

public function testAdditionalPath()
{
$renderer = new BreadcrumbRenderer(array('compressed' => true));

$expected = '<ul><li>Root li</li><li>Parent 1</li><li>Child 1</li><li><a href="http://example.com">Foo</a></li></ul>';

$this->assertEquals($expected, $renderer->render($this->ch1, array('additional_path' => array('Foo' => 'http://example.com'))));
}

public function testCurrentLink()
{
$this->pt1->setUri('foobar')->setCurrent(true);

$renderer = new BreadcrumbRenderer(array('compressed' => true));

$expected = '<ul><li>Root li</li><li class="current"><a href="foobar">Parent 1</a></li><li>Child 1</li></ul>';

$this->assertEquals($expected, $renderer->render($this->ch1));
}

public function testCurrentNoLink()
{
$this->pt1->setUri('foobar')->setCurrent(true);

$renderer = new BreadcrumbRenderer(array('compressed' => true, 'current_as_link' => false));

$expected = '<ul><li>Root li</li><li class="current">Parent 1</li><li>Child 1</li></ul>';

$this->assertEquals($expected, $renderer->render($this->ch1));
}

public function testCurrentCustomClass()
{
$this->pt1->setCurrent(true);

$renderer = new BreadcrumbRenderer(array('compressed' => true, 'current_class' => 'foo'));

$expected = '<ul><li>Root li</li><li class="foo">Parent 1</li><li>Child 1</li></ul>';

$this->assertEquals($expected, $renderer->render($this->ch1));
}

public function testEscapedLabel()
{
$this->pt1->setExtra('safe_label', true)->setLabel('<strong>Foo</strong>');

$renderer = new BreadcrumbRenderer(array('compressed' => true));

$expected = '<ul><li>Root li</li><li>&lt;strong&gt;Foo&lt;/strong&gt;</li><li>Child 1</li></ul>';

$this->assertEquals($expected, $renderer->render($this->ch1));
}

public function testUnsafeLabel()
{
$this->pt1->setLabel('<strong>Foo</strong>');

$renderer = new BreadcrumbRenderer(array('compressed' => true, 'allow_safe_labels' => true));

$expected = '<ul><li>Root li</li><li>&lt;strong&gt;Foo&lt;/strong&gt;</li><li>Child 1</li></ul>';

$this->assertEquals($expected, $renderer->render($this->ch1));
}

public function testSafeLabel()
{
$this->pt1->setExtra('safe_label', true)->setLabel('<strong>Foo</strong>');

$renderer = new BreadcrumbRenderer(array('compressed' => true, 'allow_safe_labels' => true));

$expected = '<ul><li>Root li</li><li><strong>Foo</strong></li><li>Child 1</li></ul>';

$this->assertEquals($expected, $renderer->render($this->ch1));
}

public function testPrettyRendering()
{
$renderer = new BreadcrumbRenderer();

$rendered = <<<HTML
<ul>
<li>
Root li
</li>
<li>
Parent 1
</li>
<li>
Child 1
</li>
</ul>

HTML;

$this->assertEquals($rendered, $renderer->render($this->ch1));
}
}