Iframe::renderAttributes()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 9.2
cc 4
eloc 5
nc 6
nop 0
crap 4
1
<?php namespace Arcanedev\EmbedVideo\Entities;
2
3
use Illuminate\Contracts\Support\Htmlable;
4
use Illuminate\Support\HtmlString;
5
6
/**
7
 * Class     Iframe
8
 *
9
 * @package  Arcanedev\EmbedVideo\Entities
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class Iframe implements Htmlable
13
{
14
    /* -----------------------------------------------------------------
15
     |  Properties
16
     | -----------------------------------------------------------------
17
     */
18
    /**
19
     * @var string
20
     */
21
    protected $pattern;
22
23
    /**
24
     * @var array
25
     */
26
    protected $queries;
27
28
    /**
29
     * @var array
30
     */
31
    protected $replacer;
32
33
    /**
34
     * @var array
35
     */
36
    protected $attributes;
37
38
    /* -----------------------------------------------------------------
39
     |  Constructor
40
     | -----------------------------------------------------------------
41
     */
42
    /**
43
     * Iframe constructor.
44
     *
45
     * @param  string  $pattern
46
     * @param  array   $replacer
47
     * @param  array   $queries
48
     * @param  array   $attributes
49
     */
50 30
    public function __construct($pattern, array $replacer = [], array $queries = [], array $attributes = [])
51
    {
52 30
        $this->pattern    = $pattern;
53 30
        $this->replacer   = $replacer;
54 30
        $this->queries    = $queries;
55 30
        $this->attributes = $attributes;
56 30
    }
57
58
    /* -----------------------------------------------------------------
59
     |  Getters & Setters
60
     | -----------------------------------------------------------------
61
     */
62
    /**
63
     * Get iframe src value.
64
     *
65
     * @return string
66
     */
67 30
    public function src()
68
    {
69 30
        return $this->renderUrl().$this->renderQueries();
70
    }
71
72
    /* -----------------------------------------------------------------
73
     |  Main Methods
74
     | -----------------------------------------------------------------
75
     */
76
    /**
77
     * Render the iframe.
78
     *
79
     * @return \Illuminate\Support\HtmlString
80
     */
81 30
    public function render()
82
    {
83 30
        return new HtmlString(
84 30
            '<iframe src="'.$this->src().'"'.$this->renderAttributes().'></iframe>'
85 10
        );
86
    }
87
88
    /**
89
     * Get content as a string of HTML.
90
     *
91
     * @return string
92
     */
93 9
    public function toHtml()
94
    {
95 9
        return $this->render()->toHtml();
96
    }
97
98
    /**
99
     * Convert the object to string.
100
     *
101
     * @return string
102
     */
103
    public function __toString()
104
    {
105
        return $this->toHtml();
106
    }
107
108
    /* -----------------------------------------------------------------
109
     |  Other Methods
110
     | -----------------------------------------------------------------
111
     */
112
    /**
113
     * Render the base URL.
114
     *
115
     * @return string
116
     */
117 30
    protected function renderUrl()
118
    {
119 30
        return str_replace(
120 30
            array_keys($this->replacer),
121 30
            array_values($this->replacer),
122 30
            $this->pattern
123 10
        );
124
    }
125
126
    /**
127
     * Render the URL queries.
128
     *
129
     * @return string
130
     */
131 30
    protected function renderQueries()
132
    {
133 30
        if (empty($this->queries)) return '';
134
135 27
        $queries = [];
136
137 27
        foreach ($this->queries as $key => $value) {
138 27
            switch (gettype($value)) {
139 27
                case 'boolean':
140 3
                    $queries[$key] = $value ? 'true' : 'false';
141 3
                    continue;
142
143 9
                default:
144 27
                    $queries[$key] = $value;
145 27
                    continue;
146 9
            }
147 9
        }
148
149 27
        return '?'.http_build_query($queries);
150
    }
151
152
    /**
153
     * Render the attributes.
154
     *
155
     * @return string
156
     */
157 30
    protected function renderAttributes()
158
    {
159 30
        $output = [];
160
161 30
        foreach ($this->attributes as $key => $value) {
162 9
            $output[] = is_int($key) ? $value : $key.'="'.e($value).'"';
163 10
        };
164
165 30
        return empty($output) ? '' : ' '.implode(' ', $output);
166
    }
167
}
168