|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Charcoal\Object; |
|
4
|
|
|
|
|
5
|
|
|
use \Charcoal\View\ViewableInterface; |
|
6
|
|
|
use \Charcoal\Translation\TranslationString; |
|
7
|
|
|
use \Charcoal\Translation\TranslationConfig; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Full implementation, as Trait, of the `RoutableInterface`. |
|
11
|
|
|
*/ |
|
12
|
|
|
trait RoutableTrait |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
private $slugPattern = ''; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string $slug |
|
21
|
|
|
*/ |
|
22
|
|
|
private $slug; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param mixed $pattern The slug pattern. |
|
26
|
|
|
* @return RoutableInterface Chainable |
|
27
|
|
|
*/ |
|
28
|
|
|
public function setSlugPattern($pattern) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->slugPattern = new TranslationString($pattern); |
|
|
|
|
|
|
31
|
|
|
return $this; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return TranslationString |
|
36
|
|
|
*/ |
|
37
|
|
|
public function slugPattern() |
|
38
|
|
|
{ |
|
39
|
|
|
if (!$this->slugPattern) { |
|
40
|
|
|
$metadata = $this->metadata(); |
|
|
|
|
|
|
41
|
|
|
$slugPattern = isset($metadata['slug_pattern']) ? $metadata['slug_pattern'] : ''; |
|
42
|
|
|
$this->setSlugPattern($slugPattern); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $this->slugPattern; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param mixed $slug The slug. |
|
50
|
|
|
* @return RoutableInterface Chainable |
|
51
|
|
|
*/ |
|
52
|
|
|
public function setSlug($slug) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->slug = new TranslationString($slug); |
|
|
|
|
|
|
55
|
|
|
$patterns = $this->slug->all(); |
|
56
|
|
|
foreach ($patterns as $lang => $pattern) { |
|
57
|
|
|
$this->slug[$lang] = $this->slugify($this->slug[$lang]); |
|
58
|
|
|
} |
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return TranslationString |
|
64
|
|
|
*/ |
|
65
|
|
|
public function slug() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->slug; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Generate a URL slug from the object's URL slug pattern. |
|
72
|
|
|
* |
|
73
|
|
|
* @return TranslationString |
|
74
|
|
|
*/ |
|
75
|
|
|
public function generateSlug() |
|
76
|
|
|
{ |
|
77
|
|
|
$patterns = $this->slugPattern(); |
|
78
|
|
|
$patterns = $patterns->all(); |
|
79
|
|
|
$slug = new TranslationString(); |
|
80
|
|
|
|
|
81
|
|
|
$translator = TranslationConfig::instance(); |
|
82
|
|
|
|
|
83
|
|
|
$origLang = $translator->currentLanguage(); |
|
84
|
|
|
foreach ($patterns as $lang => $pattern) { |
|
85
|
|
|
$translator->setCurrentLanguage($lang); |
|
86
|
|
|
if ($this instanceof ViewableInterface && $this->view() !== null) { |
|
87
|
|
|
$slug[$lang] = $this->view()->render($pattern, $this->viewController()); |
|
88
|
|
|
} else { |
|
89
|
|
|
$obj = $this; |
|
90
|
|
|
|
|
91
|
|
|
$cb = function ($matches) use ($obj) { |
|
92
|
|
|
$method = trim($matches[1]); |
|
93
|
|
|
if (method_exists($obj, $method)) { |
|
94
|
|
|
return call_user_func([$obj, $method]); |
|
95
|
|
|
} elseif (isset($obj[$method])) { |
|
96
|
|
|
return $obj[$method]; |
|
97
|
|
|
} else { |
|
98
|
|
|
return ''; |
|
99
|
|
|
} |
|
100
|
|
|
}; |
|
101
|
|
|
$slug[$lang] = preg_replace_callback('~{{(.*?)}}~i', $cb, $pattern); |
|
102
|
|
|
} |
|
103
|
|
|
$slug[$lang] = $this->slugify($slug[$lang]); |
|
104
|
|
|
} |
|
105
|
|
|
$translator->setCurrentLanguage($origLang); |
|
106
|
|
|
|
|
107
|
|
|
return $slug; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
|
|
public function url() |
|
114
|
|
|
{ |
|
115
|
|
|
return (string)$this->slug(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param string $str The string to slugify. |
|
120
|
|
|
* @return string The slugified string. |
|
121
|
|
|
*/ |
|
122
|
|
|
public function slugify($str) |
|
123
|
|
|
{ |
|
124
|
|
|
// Character options |
|
125
|
|
|
$separator = '_'; |
|
126
|
|
|
$punctuation_modifier = $separator; |
|
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
// Punctuation |
|
129
|
|
|
$punctuation_characters = ['&', '%', '?', ')', '(', '\\', '"', "'", ':', '#', '.', ',', ';', '!']; |
|
130
|
|
|
|
|
131
|
|
|
// Unescaped HTML characters string |
|
132
|
|
|
$unescaped_html_characters = '/&(raquo|laquo|rsaquo|lsaquo|rdquo|ldquo|rsquo|lsquo|hellip|amp|nbsp|quot|ordf|ordm);/'; |
|
133
|
|
|
|
|
134
|
|
|
$separator = '-'; |
|
135
|
|
|
|
|
136
|
|
|
$slug = preg_replace('/[^\p{L}\s]/u', '-', $str); |
|
137
|
|
|
|
|
138
|
|
|
$slug = mb_strtolower($slug, 'UTF-8'); |
|
139
|
|
|
|
|
140
|
|
|
// Strip HTML |
|
141
|
|
|
$slug = strip_tags($slug); |
|
142
|
|
|
|
|
143
|
|
|
// Strip Whitespace |
|
144
|
|
|
$slug = trim($slug); |
|
145
|
|
|
|
|
146
|
|
|
// Remove diacritics |
|
147
|
|
|
$slug = preg_replace('/&([a-zA-Z])(uml|acute|grave|circ|tilde|cedil|ring);/', '$1', htmlentities($slug, ENT_COMPAT, 'UTF-8')); |
|
148
|
|
|
|
|
149
|
|
|
// Remove unescaped HTML characters |
|
150
|
|
|
$slug = preg_replace($unescaped_html_characters, '', $slug); |
|
151
|
|
|
|
|
152
|
|
|
// Get rid of punctuation |
|
153
|
|
|
$slug = str_replace($punctuation_characters, $separator, $slug); |
|
154
|
|
|
|
|
155
|
|
|
// Post-cleanup, get rid of spaces, repeating dash symbols symbols and surround whitespace/separators |
|
156
|
|
|
$slug = trim($slug); |
|
157
|
|
|
|
|
158
|
|
|
// Replace whitespace by seperator |
|
159
|
|
|
$slug = preg_replace('/\s+/', $separator, $slug); |
|
160
|
|
|
|
|
161
|
|
|
// Squeeze multiple dashes or underscores |
|
162
|
|
|
$slug = preg_replace('/[-_]{2,}/', '-', $slug); |
|
163
|
|
|
|
|
164
|
|
|
// Strip leading and trailing dashes or underscores |
|
165
|
|
|
$slug = trim($slug, '-_'); |
|
166
|
|
|
|
|
167
|
|
|
// Finally, remove all whitespace |
|
168
|
|
|
$slug = preg_replace('/[_]+/', $separator, $slug); |
|
169
|
|
|
//$slug = str_replace('_', $separator, $slug); |
|
|
|
|
|
|
170
|
|
|
|
|
171
|
|
|
return $slug; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..