|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Charcoal\Cms\Support; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Additional utilities for the HTML document. |
|
9
|
|
|
*/ |
|
10
|
|
|
trait DocumentTrait |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Parse the document title parts. |
|
14
|
|
|
* |
|
15
|
|
|
* @return string[] |
|
16
|
|
|
*/ |
|
17
|
|
|
protected function documentTitleParts() |
|
18
|
|
|
{ |
|
19
|
|
|
return [ |
|
20
|
|
|
'title' => $this->title(), |
|
21
|
|
|
'site' => $this->siteName(), |
|
22
|
|
|
]; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Retrieve the document title separator. |
|
27
|
|
|
* |
|
28
|
|
|
* @return string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function documentTitleSeparator() |
|
31
|
|
|
{ |
|
32
|
|
|
return '—'; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Parse the document title separator. |
|
37
|
|
|
* |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected function parseDocumentTitleSeparator() |
|
41
|
|
|
{ |
|
42
|
|
|
$delim = trim($this->documentTitleSeparator()); |
|
43
|
|
|
if (empty($delim)) { |
|
44
|
|
|
return ''; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return sprintf(' %s ', $delim); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Parse the document title. |
|
52
|
|
|
* |
|
53
|
|
|
* @param array $parts The document title parts. |
|
54
|
|
|
* @return string The concatenated title. |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function parseDocumentTitle(array $parts) |
|
57
|
|
|
{ |
|
58
|
|
|
$parts = $this->parseDocumentTitleParts($parts); |
|
59
|
|
|
$delim = $this->parseDocumentTitleSeparator(); |
|
60
|
|
|
$title = implode($delim, $parts); |
|
61
|
|
|
|
|
62
|
|
|
return $title; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Parse the document title segments. |
|
67
|
|
|
* |
|
68
|
|
|
* Iterates over each value in $parts passing them to |
|
69
|
|
|
* {@see DocumentTrait::filterDocumentTitlePart}. |
|
70
|
|
|
* If the method returns TRUE, the current value from $parts |
|
71
|
|
|
* is concatenated into the title. |
|
72
|
|
|
* |
|
73
|
|
|
* @param array $parts The document title parts. |
|
74
|
|
|
* @return array The parsed and filtered segments. |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function parseDocumentTitleParts(array $parts) |
|
77
|
|
|
{ |
|
78
|
|
|
$segments = []; |
|
79
|
|
|
foreach ($parts as $key => $value) { |
|
80
|
|
|
$value = $this->parseDocumentTitlePart($value, $key, $parts); |
|
81
|
|
|
if ($value === true) { |
|
82
|
|
|
$value = $parts[$key]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if (is_bool($value) || (empty($value) && !is_numeric($value))) { |
|
86
|
|
|
continue; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$segments[$key] = (string)$value; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $segments; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Parse the document title part. |
|
97
|
|
|
* |
|
98
|
|
|
* If you want to exclude the site name ("site") from the document title |
|
99
|
|
|
* if it is present in other parts, you can use the following snippet: |
|
100
|
|
|
* |
|
101
|
|
|
* ```php |
|
102
|
|
|
* if ($key === 'site') { |
|
103
|
|
|
* foreach ($parts as $k => $v) { |
|
104
|
|
|
* if ($k !== $key && strpos($v, $value) !== false) { |
|
105
|
|
|
* return null; |
|
106
|
|
|
* } |
|
107
|
|
|
* } |
|
108
|
|
|
* } |
|
109
|
|
|
* ``` |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $value The value of the current iteration. |
|
112
|
|
|
* @param string $key The key/index of the current iteration. |
|
113
|
|
|
* @param array $parts The document title parts. |
|
114
|
|
|
* @return mixed The mutated value of the current iteration. |
|
115
|
|
|
* If $value is equal to FALSE (converted to boolean; excluding "0"), it is excluded from the document title. |
|
116
|
|
|
* If the method returns TRUE, the original $value is included into the document title. |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function parseDocumentTitlePart($value, $key, array $parts) |
|
119
|
|
|
{ |
|
120
|
|
|
unset($key, $parts); |
|
121
|
|
|
return $value; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Retrieve the document title. |
|
126
|
|
|
* |
|
127
|
|
|
* @throws InvalidArgumentException If the document title structure is invalid. |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
|
|
final public function documentTitle() |
|
131
|
|
|
{ |
|
132
|
|
|
$parts = $this->documentTitleParts(); |
|
133
|
|
|
if (array_diff_key([ 'title' => true, 'site' => true ], $parts)) { |
|
134
|
|
|
throw new InvalidArgumentException( |
|
135
|
|
|
'The document title parts requires at least a "title" and a "site"' |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $this->parseDocumentTitle($parts); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Retrieve the site name. |
|
144
|
|
|
* |
|
145
|
|
|
* @return string|null |
|
146
|
|
|
*/ |
|
147
|
|
|
abstract public function siteName(); |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Retrieve the title of the page (from the context). |
|
151
|
|
|
* |
|
152
|
|
|
* @return string|null |
|
153
|
|
|
*/ |
|
154
|
|
|
abstract public function title(); |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Retrieve the current object relative to the context. |
|
158
|
|
|
* |
|
159
|
|
|
* @return \Charcoal\Model\ModelInterface |
|
160
|
|
|
*/ |
|
161
|
|
|
abstract public function contextObject(); |
|
162
|
|
|
} |
|
163
|
|
|
|