|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rephlux\PageTitle; |
|
4
|
|
|
|
|
5
|
|
|
use Countable; |
|
6
|
|
|
|
|
7
|
|
|
class PageTitle implements Countable |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Collection of page title parts |
|
11
|
|
|
* |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $collection = []; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Delimeter string for seperate the page title parts |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
private $delimeter; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The page name to append or prepend to the page title parts |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $page_name; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The default text when no page title is set |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
private $default; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $delimeter |
|
39
|
|
|
* @param string $page_name |
|
40
|
|
|
* @param string $default |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct($delimeter = ' | ', $page_name = '', $default = '') |
|
43
|
|
|
{ |
|
44
|
|
|
$this->delimeter = $delimeter; |
|
45
|
|
|
$this->page_name = $page_name; |
|
46
|
|
|
$this->default = $default; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Add an item to the collection |
|
51
|
|
|
* |
|
52
|
|
|
* @param $item |
|
53
|
|
|
* @return mixed |
|
54
|
|
|
*/ |
|
55
|
|
|
public function add($item) |
|
56
|
|
|
{ |
|
57
|
|
|
if (is_array($item)) { |
|
58
|
|
|
return array_map([$this, 'add'], $item); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (!$item | strlen(trim($item)) === 0) { |
|
62
|
|
|
return false; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$this->collection[] = trim(strip_tags($item)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Count the collection |
|
70
|
|
|
* |
|
71
|
|
|
* @return int |
|
72
|
|
|
*/ |
|
73
|
|
|
public function count() |
|
74
|
|
|
{ |
|
75
|
|
|
return count($this->collection); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Get the page title |
|
80
|
|
|
* |
|
81
|
|
|
* @param bool|string $direction |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
|
|
public function get($direction = 'regular') |
|
85
|
|
|
{ |
|
86
|
|
|
if ($this->count() == 0) { |
|
87
|
|
|
return $this->default; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if ($direction === 'downward') { |
|
91
|
|
|
$this->collection = array_reverse($this->collection); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$this->addPageName(); |
|
95
|
|
|
|
|
96
|
|
|
if ($direction === 'reverse') { |
|
97
|
|
|
$this->collection = array_reverse($this->collection); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return implode($this->delimeter, $this->collection); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Add the page name to the collection |
|
105
|
|
|
* |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function addPageName() |
|
108
|
|
|
{ |
|
109
|
|
|
if (isset($this->page_name) && !in_array($this->page_name, $this->collection)) { |
|
110
|
|
|
$this->add($this->page_name); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |