icingaweb2-module-director/library/vendor/ipl/Html/Img.php

83 lines
1.9 KiB
PHP
Raw Normal View History

<?php
2017-10-09 15:23:27 +02:00
namespace dipl\Html;
2017-10-09 15:23:27 +02:00
use dipl\Web\Url;
use Icinga\Web\Url as WebUrl;
2018-05-05 01:11:55 +02:00
class Img extends BaseHtmlElement
{
protected $tag = 'img';
/** @var Url */
protected $url;
protected $defaultAttributes = array('alt' => '');
protected function __construct()
{
}
/**
* @param Url|string $url
* @param array $urlParams
* @param array $attributes
*
* @return static
*/
public static function create($url, $urlParams = null, array $attributes = null)
{
/** @var Img $img */
$img = new static();
$img->setAttributes($attributes);
2018-05-05 00:18:45 +02:00
$img->getAttributes()->registerAttributeCallback('src', array($img, 'getSrcAttribute'));
$img->setUrl($url, $urlParams);
return $img;
}
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) {
2018-02-20 14:39:58 +01:00
if (is_string($url) && substr($url, 0, 5) === 'data:') {
$this->url = $url;
return;
} else {
$this->url = Url::fromPath($url);
}
} else {
$this->url = Url::fromPath($url, $urlParams);
}
}
$this->url->getParams();
}
/**
* @return Attribute
*/
public function getSrcAttribute()
{
2018-02-20 14:39:58 +01:00
if (is_string($this->url)) {
return new Attribute('src', $this->url);
} else {
return new Attribute('src', $this->getUrl()->getAbsoluteUrl('&'));
}
}
/**
* @return Url
*/
public function getUrl()
{
// TODO: What if null? #?
return $this->url;
}
}