summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/web/src/Widget/Link.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/ipl/web/src/Widget/Link.php')
-rw-r--r--vendor/ipl/web/src/Widget/Link.php97
1 files changed, 97 insertions, 0 deletions
diff --git a/vendor/ipl/web/src/Widget/Link.php b/vendor/ipl/web/src/Widget/Link.php
new file mode 100644
index 0000000..cbae3b9
--- /dev/null
+++ b/vendor/ipl/web/src/Widget/Link.php
@@ -0,0 +1,97 @@
+<?php
+
+namespace ipl\Web\Widget;
+
+use ipl\Html\Attribute;
+use ipl\Html\Attributes;
+use ipl\Html\BaseHtmlElement;
+use ipl\Web\Common\BaseTarget;
+use ipl\Web\Url;
+
+/**
+ * Link element, i.e. <a href="...
+ */
+class Link extends BaseHtmlElement
+{
+ use BaseTarget;
+
+ /** @var Url */
+ protected $url;
+
+ protected $tag = 'a';
+
+ /**
+ * Create a link element
+ *
+ * @param mixed $content
+ * @param Url|string $url
+ * @param Attributes|array $attributes
+ */
+ public function __construct($content, $url, $attributes = null)
+ {
+ $this
+ ->setContent($content)
+ ->setUrl($url)
+ ->getAttributes()
+ ->add($attributes)
+ ->registerAttributeCallback('href', [$this, 'createHrefAttribute']);
+ }
+
+ /**
+ * Get the URL of the link
+ *
+ * @return Url
+ */
+ public function getUrl()
+ {
+ return $this->url;
+ }
+
+ /**
+ * Set the URL of the link
+ *
+ * @param Url|string $url
+ *
+ * @return $this
+ */
+ public function setUrl($url)
+ {
+ if (! $url instanceof Url) {
+ try {
+ $url = Url::fromPath($url);
+ } catch (\Exception $e) {
+ $url = 'invalid';
+ }
+ }
+
+ $this->url = $url;
+
+ return $this;
+ }
+
+ /**
+ * Create and return the href attribute
+ *
+ * Used as attribute callback for the href attribute.
+ *
+ * @return Attribute
+ */
+ public function createHrefAttribute()
+ {
+ return new Attribute('href', (string) $this->getUrl());
+ }
+
+ /**
+ * Open this link in a modal
+ *
+ * @return $this
+ */
+ public function openInModal(): self
+ {
+ $this->getAttributes()
+ ->set('data-icinga-modal', true)
+ ->set('data-no-icinga-ajax', true);
+
+ return $this;
+ }
+}