-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MenuItem: Add support for common MenuItem interface.
- Loading branch information
1 parent
1436656
commit 76a45fb
Showing
6 changed files
with
104 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Baraja\AdminBar\Menu; | ||
|
||
|
||
use Baraja\AdminBar\Helpers; | ||
use Nette\Utils\Strings; | ||
|
||
final class MenuEvent extends MenuItem | ||
{ | ||
private string $label; | ||
|
||
private string $event; | ||
|
||
|
||
public function __construct(string $label, string $event) | ||
{ | ||
if ($event === '') { | ||
throw new \InvalidArgumentException('Event is required.'); | ||
} | ||
if (preg_match('/^[a-zA-Z0-9-.]+$/', $event) !== 1) { | ||
throw new \InvalidArgumentException( | ||
'Event format is not valid, because haystack "' . $event . '" given.', | ||
); | ||
} | ||
|
||
$this->label = Strings::firstUpper($label); | ||
$this->event = $event; | ||
} | ||
|
||
|
||
public function render(): string | ||
{ | ||
return '<a onclick="eventBus.$emit(\'' . Helpers::escapeHtml($this->getEvent()) . '\')">' | ||
. Helpers::escapeHtml($this->getLabel()) | ||
. '</a>'; | ||
} | ||
|
||
|
||
public function getLabel(): string | ||
{ | ||
return $this->label; | ||
} | ||
|
||
|
||
public function getEvent(): string | ||
{ | ||
return $this->event; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Baraja\AdminBar\Menu; | ||
|
||
|
||
abstract class MenuItem implements \Stringable | ||
{ | ||
final public function __toString(): string | ||
{ | ||
return $this->render(); | ||
} | ||
|
||
|
||
/** | ||
* Safe render of HTML content. | ||
*/ | ||
abstract public function render(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters