|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rephlux\PageTitle; |
|
4
|
|
|
|
|
5
|
|
|
use Countable; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class PageTitle. |
|
9
|
|
|
* |
|
10
|
|
|
* @author Chris van Daele <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class PageTitle implements Countable |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Collection of page title parts. |
|
16
|
|
|
* |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $collection = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Delimeter string for seperate the page title parts. |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $delimeter; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The page name to append or prepend to the page title parts. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $page_name; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The default text when no page title is set. |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $default; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $delimeter |
|
44
|
|
|
* @param string $page_name |
|
45
|
|
|
* @param string $default |
|
46
|
|
|
*/ |
|
47
|
30 |
|
public function __construct($delimeter = ' | ', $page_name = '', $default = '') |
|
48
|
|
|
{ |
|
49
|
30 |
|
$this->delimeter = $delimeter; |
|
50
|
30 |
|
$this->page_name = $page_name; |
|
51
|
30 |
|
$this->default = $default; |
|
52
|
30 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
18 |
|
public function getDelimeter() |
|
58
|
|
|
{ |
|
59
|
18 |
|
return $this->delimeter; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $delimeter |
|
64
|
|
|
* |
|
65
|
|
|
* @return $this |
|
66
|
|
|
*/ |
|
67
|
3 |
|
public function setDelimeter($delimeter) |
|
68
|
|
|
{ |
|
69
|
3 |
|
$this->delimeter = $delimeter; |
|
70
|
|
|
|
|
71
|
3 |
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
18 |
|
public function getPageName() |
|
78
|
|
|
{ |
|
79
|
18 |
|
return $this->page_name; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param string $page_name |
|
84
|
|
|
* |
|
85
|
|
|
* @return $this |
|
86
|
|
|
*/ |
|
87
|
3 |
|
public function setPageName($page_name) |
|
88
|
|
|
{ |
|
89
|
3 |
|
$this->page_name = $page_name; |
|
90
|
|
|
|
|
91
|
3 |
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
3 |
|
public function getDefault() |
|
98
|
|
|
{ |
|
99
|
3 |
|
return $this->default; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param string $default |
|
104
|
|
|
* |
|
105
|
|
|
* @return $this |
|
106
|
|
|
*/ |
|
107
|
3 |
|
public function setDefault($default) |
|
108
|
|
|
{ |
|
109
|
3 |
|
$this->default = $default; |
|
110
|
|
|
|
|
111
|
3 |
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Add an item to the collection. |
|
116
|
|
|
* |
|
117
|
|
|
* @param $item |
|
118
|
|
|
* |
|
119
|
|
|
* @return mixed |
|
120
|
|
|
*/ |
|
121
|
18 |
|
public function add($item) |
|
122
|
|
|
{ |
|
123
|
18 |
|
if (is_array($item)) { |
|
124
|
9 |
|
return array_map([$this, 'add'], $item); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
18 |
|
if (! $item | strlen(trim($item)) === 0) { |
|
128
|
3 |
|
return false; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
15 |
|
$this->collection[] = trim(strip_tags($item)); |
|
132
|
15 |
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Count the collection. |
|
136
|
|
|
* |
|
137
|
|
|
* @return int |
|
138
|
|
|
*/ |
|
139
|
21 |
|
public function count() |
|
140
|
|
|
{ |
|
141
|
21 |
|
return count($this->collection); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Get the page title. |
|
146
|
|
|
* |
|
147
|
|
|
* @param bool|string $direction |
|
148
|
|
|
* |
|
149
|
|
|
* @return string |
|
150
|
|
|
*/ |
|
151
|
18 |
|
public function get($direction = 'regular') |
|
152
|
|
|
{ |
|
153
|
18 |
|
if ($this->count() == 0) { |
|
154
|
3 |
|
return $this->default; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
15 |
|
if ($direction === 'downward') { |
|
158
|
3 |
|
$this->collection = array_reverse($this->collection); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
15 |
|
$this->addPageName(); |
|
162
|
|
|
|
|
163
|
15 |
|
if ($direction === 'reverse') { |
|
164
|
3 |
|
$this->collection = array_reverse($this->collection); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
15 |
|
return implode($this->getDelimeter(), $this->collection); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Add the page name to the collection. |
|
172
|
|
|
*/ |
|
173
|
15 |
|
protected function addPageName() |
|
174
|
|
|
{ |
|
175
|
15 |
|
if (! empty($this->getPageName()) && ! in_array($this->getPageName(), $this->collection)) { |
|
176
|
15 |
|
$this->add($this->getPageName()); |
|
177
|
|
|
} |
|
178
|
15 |
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|