diff options
Diffstat (limited to 'vendor/gipfl/icingaweb2/src/Link.php')
-rw-r--r-- | vendor/gipfl/icingaweb2/src/Link.php | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/vendor/gipfl/icingaweb2/src/Link.php b/vendor/gipfl/icingaweb2/src/Link.php new file mode 100644 index 0000000..e6e4de9 --- /dev/null +++ b/vendor/gipfl/icingaweb2/src/Link.php @@ -0,0 +1,85 @@ +<?php + +namespace gipfl\IcingaWeb2; + +use Icinga\Web\Url as WebUrl; +use ipl\Html\Attribute; +use ipl\Html\BaseHtmlElement; +use ipl\Html\ValidHtml; + +class Link extends BaseHtmlElement +{ + protected $tag = 'a'; + + /** @var Url */ + protected $url; + + /** + * Link constructor. + * @param $content + * @param $url + * @param null $urlParams + * @param array|null $attributes + */ + public function __construct($content, $url, $urlParams = null, array $attributes = null) + { + $this->setContent($content); + $this->setAttributes($attributes); + $this->getAttributes()->registerAttributeCallback('href', array($this, 'getHrefAttribute')); + $this->setUrl($url, $urlParams); + } + + /** + * @param ValidHtml|array|string $content + * @param Url|string $url + * @param array $urlParams + * @param mixed $attributes + * + * @return static + */ + public static function create($content, $url, $urlParams = null, array $attributes = null) + { + $link = new static($content, $url, $urlParams, $attributes); + return $link; + } + + /** + * @param $url + * @param $urlParams + */ + public function setUrl($url, $urlParams) + { + if ($url instanceof WebUrl) { // Hint: Url is also a WebUrl + if ($urlParams !== null) { + $url->addParams($urlParams); + } + + $this->url = $url; + } else { + if ($urlParams === null) { + $this->url = Url::fromPath($url); + } else { + $this->url = Url::fromPath($url, $urlParams); + } + } + + $this->url->getParams(); + } + + /** + * @return Attribute + */ + public function getHrefAttribute() + { + return new Attribute('href', $this->getUrl()->getAbsoluteUrl('&')); + } + + /** + * @return Url + */ + public function getUrl() + { + // TODO: What if null? #? + return $this->url; + } +} |