2023-03-11 12:04:29 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Zend Framework (http://framework.zend.com/)
|
|
|
|
*
|
|
|
|
* @link http://github.com/zendframework/zf2 for the canonical source repository
|
2023-04-01 09:03:34 +03:00
|
|
|
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
|
2023-03-11 12:04:29 +03:00
|
|
|
* @license http://framework.zend.com/license/new-bsd New BSD License
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Zend\View\Helper;
|
|
|
|
|
2023-04-01 09:03:34 +03:00
|
|
|
use Zend\View\Exception;
|
|
|
|
|
2023-03-11 12:04:29 +03:00
|
|
|
/**
|
|
|
|
* Helper for ordered and unordered lists
|
|
|
|
*/
|
|
|
|
class HtmlList extends AbstractHtmlElement
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Generates a 'List' element.
|
|
|
|
*
|
|
|
|
* @param array $items Array with the elements of the list
|
|
|
|
* @param bool $ordered Specifies ordered/unordered list; default unordered
|
|
|
|
* @param array $attribs Attributes for the ol/ul tag.
|
|
|
|
* @param bool $escape Escape the items.
|
2023-04-01 09:03:34 +03:00
|
|
|
* @throws Exception\InvalidArgumentException
|
2023-03-11 12:04:29 +03:00
|
|
|
* @return string The list XHTML.
|
|
|
|
*/
|
|
|
|
public function __invoke(array $items, $ordered = false, $attribs = false, $escape = true)
|
|
|
|
{
|
2023-04-01 09:03:34 +03:00
|
|
|
if (empty($items)) {
|
|
|
|
throw new Exception\InvalidArgumentException(sprintf(
|
|
|
|
'$items array can not be empty in %s',
|
|
|
|
__METHOD__
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2023-03-11 12:04:29 +03:00
|
|
|
$list = '';
|
|
|
|
|
|
|
|
foreach ($items as $item) {
|
|
|
|
if (!is_array($item)) {
|
|
|
|
if ($escape) {
|
|
|
|
$escaper = $this->getView()->plugin('escapeHtml');
|
|
|
|
$item = $escaper($item);
|
|
|
|
}
|
2023-04-01 09:03:34 +03:00
|
|
|
$list .= '<li>' . $item . '</li>' . PHP_EOL;
|
2023-03-11 12:04:29 +03:00
|
|
|
} else {
|
2023-04-01 09:03:34 +03:00
|
|
|
$itemLength = 5 + strlen(PHP_EOL);
|
2023-03-11 12:04:29 +03:00
|
|
|
if ($itemLength < strlen($list)) {
|
|
|
|
$list = substr($list, 0, strlen($list) - $itemLength)
|
2023-04-01 09:03:34 +03:00
|
|
|
. $this($item, $ordered, $attribs, $escape) . '</li>' . PHP_EOL;
|
2023-03-11 12:04:29 +03:00
|
|
|
} else {
|
2023-04-01 09:03:34 +03:00
|
|
|
$list .= '<li>' . $this($item, $ordered, $attribs, $escape) . '</li>' . PHP_EOL;
|
2023-03-11 12:04:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($attribs) {
|
|
|
|
$attribs = $this->htmlAttribs($attribs);
|
|
|
|
} else {
|
|
|
|
$attribs = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$tag = ($ordered) ? 'ol' : 'ul';
|
|
|
|
|
2023-04-01 09:03:34 +03:00
|
|
|
return '<' . $tag . $attribs . '>' . PHP_EOL . $list . '</' . $tag . '>' . PHP_EOL;
|
2023-03-11 12:04:29 +03:00
|
|
|
}
|
|
|
|
}
|