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
|
|
|
return $value; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Retrieve the document title. |
125
|
|
|
* |
126
|
|
|
* @throws InvalidArgumentException If the document title structure is invalid. |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
final public function documentTitle() |
130
|
|
|
{ |
131
|
|
|
$parts = $this->documentTitleParts(); |
132
|
|
|
if (array_diff_key([ 'title' => true, 'site' => true ], $parts)) { |
133
|
|
|
throw new InvalidArgumentException( |
134
|
|
|
'The document title parts requires at least a "title" and a "site"' |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $this->parseDocumentTitle($parts); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Retrieve the site name. |
143
|
|
|
* |
144
|
|
|
* @return string|null |
145
|
|
|
*/ |
146
|
|
|
abstract public function siteName(); |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Retrieve the title of the page (from the context). |
150
|
|
|
* |
151
|
|
|
* @return string|null |
152
|
|
|
*/ |
153
|
|
|
abstract public function title(); |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Retrieve the current object relative to the context. |
157
|
|
|
* |
158
|
|
|
* @return \Charcoal\Model\ModelInterface |
159
|
|
|
*/ |
160
|
|
|
abstract public function contextObject(); |
161
|
|
|
} |
162
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.