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();
$title = $this->attributes['title'];
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();
}
$data = $this->getCustomData();
$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
/**
* @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 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);
$relValue = $this->getRel();
$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;
}
}
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// target attribute
$target = $this->getTarget();
$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)