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
|
7 |
|
public function __construct($delimeter = ' | ', $page_name = '', $default = '') |
43
|
|
|
{ |
44
|
7 |
|
$this->delimeter = $delimeter; |
45
|
7 |
|
$this->page_name = $page_name; |
46
|
7 |
|
$this->default = $default; |
47
|
7 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Add an item to the collection |
51
|
|
|
* |
52
|
|
|
* @param $item |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
6 |
|
public function add($item) |
56
|
|
|
{ |
57
|
6 |
|
if (is_array($item)) { |
58
|
3 |
|
return array_map([$this, 'add'], $item); |
59
|
|
|
} |
60
|
|
|
|
61
|
6 |
|
if (!$item | strlen(trim($item)) === 0) { |
62
|
1 |
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
5 |
|
$this->collection[] = trim(strip_tags($item)); |
66
|
5 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Count the collection |
70
|
|
|
* |
71
|
|
|
* @return int |
72
|
|
|
*/ |
73
|
7 |
|
public function count() |
74
|
|
|
{ |
75
|
7 |
|
return count($this->collection); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the page title |
80
|
|
|
* |
81
|
|
|
* @param bool|string $direction |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
6 |
|
public function get($direction = 'regular') |
85
|
|
|
{ |
86
|
6 |
|
if ($this->count() == 0) { |
87
|
1 |
|
return $this->default; |
88
|
|
|
} |
89
|
|
|
|
90
|
5 |
|
if ($direction === 'downward') { |
91
|
1 |
|
$this->collection = array_reverse($this->collection); |
92
|
1 |
|
} |
93
|
|
|
|
94
|
5 |
|
$this->addPageName(); |
95
|
|
|
|
96
|
5 |
|
if ($direction === 'reverse') { |
97
|
1 |
|
$this->collection = array_reverse($this->collection); |
98
|
1 |
|
} |
99
|
|
|
|
100
|
5 |
|
return implode($this->delimeter, $this->collection); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Add the page name to the collection |
105
|
|
|
* |
106
|
|
|
*/ |
107
|
5 |
|
protected function addPageName() |
108
|
|
|
{ |
109
|
5 |
|
if (isset($this->page_name) && !in_array($this->page_name, $this->collection)) { |
110
|
5 |
|
$this->add($this->page_name); |
111
|
5 |
|
} |
112
|
|
|
} |
113
|
|
|
} |