summaryrefslogtreecommitdiffstats
path: root/vendor/gipfl/icingaweb2/src/Img.php
blob: 3c68adb9de84b7f2618f928647b1920471c52f36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php

namespace gipfl\IcingaWeb2;

use Icinga\Web\Url as WebUrl;
use ipl\Html\Attribute;
use ipl\Html\BaseHtmlElement;

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);
        $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) {
                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()
    {
        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;
    }
}