Newer
Older
<?php
namespace QUI\Menu\Independent\Items;
use function array_filter;
use function is_string;
use function json_decode;
*/
abstract class AbstractMenuItem
{
protected array $attributes = [];
/**
* @param array $attributes
*/
public function __construct(array $attributes = [])
{
$this->attributes = $attributes;
}
public function getTitle(Locale $Locale = null): string
if ($Locale === null) {
$Locale = QUI::getLocale();
}
$current = $Locale->getCurrent();
if (is_string($title)) {
$title = json_decode($title, true);
}
if (isset($title[$current])) {
return $title[$current];
}
return '';
public function getName(Locale $Locale = null): string
if ($Locale === null) {
$Locale = QUI::getLocale();
}
$current = $Locale->getCurrent();
if (is_array($data) && isset($data['name'])) {
if (is_string($data['name'])) {
$name = json_decode($data['name'], true);
} else {
$name = $data['name'];
}
if (isset($name[$current])) {
return $name[$current];
}
}
return $this->getTitle($Locale);
/**
* alias for name
* - can be use for the `a title=""` attribute
*
* @param Locale|null $Locale
* @return string
*/
public function getTitleAttribute(Locale $Locale = null): string
{
return $this->getName($Locale);
}
/**
* @return string
*/
public function getUrl(): string
{
return '';
}
if (isset($this->attributes['icon'])) {
return $this->attributes['icon'];
/**
* @return string
*/
public function getIdentifier(): string
if (isset($this->attributes['identifier'])) {
return $this->attributes['identifier'];
}
/**
* @return string
*/
public function getRel(): string
{
$data = $this->getCustomData();
if (is_array($data) && isset($data['rel'])) {
return $data['rel'];
}
return '';
}
* @return string
*/
public function getTarget(): string
{
$data = $this->getCustomData();
if (is_array($data) && isset($data['target'])) {
switch ($data['target']) {
case "_self":
case "frame":
case "popup":
case "_blank":
case "_top":
case "_parent":
return $data['target'];
}
}
return '';
}
/**
* return the menu typ for the children
*
* @return string
*/
public function getMenuType(): string
{
$data = $this->getCustomData();
if (is_array($data) && isset($data['menuType'])) {
return $data['menuType'];
}
return '';
}
* @return mixed
*/
public function getCustomData()
{
if (isset($this->attributes['data'])) {
return $this->attributes['data'];
/**
* @param Locale|null $Locale
* @return string
*/
public function getHTML(QUI\Locale $Locale = null): string
{
if ($Locale === null) {
$Locale = QUI::getLocale();
}
$url = $this->getUrl();
$title = $this->getTitle($Locale);
$name = $this->getName($Locale);
$rel = '';
if (!empty($relValue)) {
switch ($relValue) {
case 'alternate':
case 'author':
case 'bookmark':
case 'external':
case 'help':
case 'license':
case 'next':
case 'nofollow':
case 'noopener':
case 'noreferrer':
case 'prev':
case 'search':
case 'tag':
$rel = 'rel="' . $relValue . '"';
break;
}
}
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
$targetAttribute = '';
if (!empty($target)) {
$targetAttribute = $target;
}
// html link
return "<a href=\"$url\" title=\"$title\" $rel $targetAttribute>$name</a>";
}
//endregion
//region status
/**
* @return bool
*/
public function isActive(): bool
{
return $this->getStatus();
}
/**
* @return bool
*/
public function getStatus(): bool
{
$data = $this->getCustomData();
if (is_array($data) && isset($data['status'])) {
return !!$data['status'];
}
return true;
//endregion
//region type stuff
abstract public static function itemTitle();
/**
* @return string
*/
public static function itemIcon(): string
/**
* @return string
*/
public static function itemJsControl(): string
//endregion
//region children
/**
* Return the children of this item
*
* @param bool $onlyActive - if true, returns only the active children, if false, all children are returned
public function getChildren(bool $onlyActive = true): array
if ($onlyActive === false) {
return $this->children;
}
return array_filter($this->children, function ($Item) {
return $Item->isActive();
});
/**
* Add a child item
*
* @param AbstractMenuItem $Item
*/
public function appendChild(AbstractMenuItem $Item)