Completed
Pull Request — 2.x (#43)
by jake
02:49 queued 50s
created

Title::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Html\Helper;
10
11
/**
12
 *
13
 * Helper to generate a `<title>` tag.
14
 *
15
 * @package Aura.Html
16
 *
17
 */
18
class Title extends AbstractHelper
19
{
20
    /**
21
     *
22
     * The title value.
23
     *
24
     * @var string
25
     *
26
     */
27
    protected $title = null;
28
29
    /**
30
     *
31
     * Returns the helper so you can call methods on it; alternatively, if you
32
     * pass a title string, it will be as you called `append()`.
33
     *
34
     * @param string $text Text to append to the existing title.
35
     *
36
     * @return self
37
     *
38
     */
39 1
    public function __invoke($text = null)
40
    {
41
        if ($text) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $text of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
42
            $this->append($text);
43
        }
44
        return $this;
45 1
    }
46
47
    /**
48
     *
49
     * Returns the current title string.
50
     *
51
     * @return string The current title string.
52
     *
53
     */
54 4
    public function __toString()
55
    {
56
        $title = $this->indent(1, "<title>{$this->title}</title>");
57 4
        $this->title = null;
58 4
        return $title;
59
    }
60
61
    /**
62
     *
63
     * Escapes and sets text as the <title> string.
64
     *
65
     * @param string $text The title string.
66
     *
67
     * @return self
68
     *
69
     */
70 3
    public function set($text)
71
    {
72
        $this->title = $this->escaper->html($text);
73 3
        return $this;
74
    }
75
76
    /**
77
     *
78
     * Sets raw text as the <title> string.
79
     *
80
     * @param string $text The title string.
81
     *
82
     * @return self
83
     *
84
     */
85 1
    public function setRaw($text)
86
    {
87 1
        $this->title = $text;
88 1
        return $this;
89
    }
90
91
    /**
92
     *
93
     * Gets the currently set title string without HTML tag
94
     *
95
     * @return string
96
     *
97
     */
98 1
    public function get()
99
    {
100 1
        return $this->title;
101
    }
102
103
    /**
104
     *
105
     * Escapes and appends text to the end of the current <title> string.
106
     *
107
     * @param string $text The string to be appended to the title.
108
     *
109
     * @return self
110
     *
111
     */
112 1
    public function append($text)
113
    {
114
        $this->title .= $this->escaper->html($text);
115 1
        return $this;
116
    }
117
118
    /**
119
     *
120
     * Appends raw text to the end of the current <title> string.
121
     *
122
     * @param string $text The string to be appended to the title.
123
     *
124
     * @return self
125
     *
126
     */
127
    public function appendRaw($text)
128
    {
129
        $this->title .= $text;
130
        return $this;
131
    }
132
133
    /**
134
     *
135
     * Escapes and prepends text to the beginning of the current <title> string.
136
     *
137
     * @param string $text The string to be appended to the title.
138
     *
139
     * @return self
140
     *
141
     */
142 1
    public function prepend($text)
143
    {
144
        $this->title = $this->escaper->html($text) . $this->title;
145 1
        return $this;
146
    }
147
148
    /**
149
     *
150
     * Prepends raw text to the beginning of the current <title> string.
151
     *
152
     * @param string $text The string to be appended to the title.
153
     *
154
     * @return self
155
     *
156
     */
157
    public function prependRaw($text)
158
    {
159
        $this->title = $text . $this->title;
160
        return $this;
161
    }
162
}
163