]> Raphaël G. Git Repositories - packbundle/blob - Util/SluggerUtil.php
Php 8.x constructor style
[packbundle] / Util / SluggerUtil.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys PackBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\PackBundle\Util;
13
14 /**
15 * Manages string conversions
16 */
17 class SluggerUtil {
18 /**
19 * The alpha array
20 */
21 protected array $alpha;
22
23 /**
24 * The rev array
25 */
26 protected array $rev;
27
28 /**
29 * The alpha array key number
30 */
31 protected int $count;
32
33 /**
34 * The offset reduced from secret
35 */
36 protected int $offset;
37
38 /**
39 * Construct slugger util
40 *
41 * TODO: use a recipe to generate in .env.local an env variable RAPSYSPACK_SECRET="ayl[...]z9w"
42 *
43 * @todo Add a command to generate alpha array or generate it on first run with cache storage ?
44 * @todo Use Cache like in calendar controller through FilesystemAdapter
45 *
46 * @param string $secret The secret string
47 */
48 public function __construct(protected string $secret) {
49 /**
50 * Pseudo-random alphabet
51 * @xxx use array flip and keys to workaround php "smart" that cast range('0', '9') as int instead of string
52 * @xxx The key count mismatch, count(alpha)>count(rev), resulted in a data corruption due to duplicate numeric values
53 * @todosee required range by json_encode result and short input (0->255 ???)
54 */
55 $this->alpha = array_keys(array_flip(array_merge(
56 range('^', '[', -1),
57 range('V', 'Z'),
58 range('9', '7', -1),
59 range('L', 'O'),
60 range('f', 'a', -1),
61 range('_', '`'),
62 range('3', '0', -1),
63 range('E', 'H'),
64 range('v', 'r', -1),
65 range('+', '/'),
66 range('K', 'I', -1),
67 range('g', 'j'),
68 range('=', ':', -1),
69 range('>', '@'),
70 range('m', 'k', -1),
71 range('4', '6'),
72 range('*', '%', -1),
73 range('n', 'q'),
74 range('U', 'P', -1),
75 range(' ', '$'),
76 range('D', 'A', -1),
77 range('w', 'z'),
78 range('~', '!', -1)
79 )));
80
81 //Init rev array
82 $this->count = count($rev = $this->rev = array_flip($this->alpha));
83
84 //Init split
85 $split = str_split($this->secret);
86
87 //Set offset
88 $this->offset = array_reduce($split, function ($res, $a) use ($rev) { return $res += $rev[$a]; }, count($split)) % $this->count;
89 }
90
91 /**
92 * Flatten recursively an array
93 *
94 * @param array|string $data The data tree
95 * @param string|null $current The current prefix
96 * @param string $sep The key separator
97 * @param string $prefix The key prefix
98 * @param string $suffix The key suffix
99 * @return array The flattened data
100 */
101 public function flatten($data, ?string $current = null, string $sep = '.', string $prefix = '', string $suffix = ''): array {
102 //Init result
103 $ret = [];
104
105 //Look for data array
106 if (is_array($data)) {
107 //Iteare on each pair
108 foreach($data as $k => $v) {
109 //Merge flattened value in return array
110 $ret += $this->flatten($v, empty($current) ? $k : $current.$sep.$k, $sep, $prefix, $suffix);
111 }
112 //Look flat data
113 } else {
114 //Store data in flattened key
115 $ret[$prefix.$current.$suffix] = $data;
116 }
117
118 //Return result
119 return $ret;
120 }
121
122 /**
123 * Crypt and base64uri encode string
124 *
125 * @param array|string $data The data string
126 * @return string The hashed data
127 */
128 public function hash(array|string $data): string {
129 //With array
130 if (is_array($data)) {
131 //Json encode array
132 $data = json_encode($data);
133 }
134
135 //Return hashed data
136 //XXX: we use hash_hmac with md5 hash
137 //XXX: crypt was dropped because it provided identical signature for string starting with same pattern
138 return str_replace(['+','/'], ['-','_'], base64_encode(hash_hmac('md5', $data, $this->secret, true)));
139 }
140
141 /**
142 * Serialize then short
143 *
144 * @param array $data The data array
145 * @return string The serialized and shorted data
146 */
147 public function serialize(array $data): string {
148 //Return shorted serialized data
149 //XXX: dropped serialize use to prevent short function from dropping utf-8 characters
150 return $this->short(json_encode($data));
151 }
152
153 /**
154 * Short
155 *
156 * @param string $data The data string
157 * @return string The shorted data
158 */
159 public function short(string $data): string {
160 //Return string
161 $ret = '';
162
163 //With data
164 if (!empty($data)) {
165 //Iterate on each character
166 foreach(str_split($data) as $k => $c) {
167 if (isset($this->rev[$c]) && isset($this->alpha[($this->rev[$c]+$this->offset)%$this->count])) {
168 //XXX: Remap char to an other one
169 $ret .= chr(($this->rev[$c] - $this->offset + $this->count) % $this->count);
170 } else {
171 throw new \RuntimeException(sprintf('Unable to retrieve character: %c', $c));
172 }
173 }
174 }
175
176 //Send result
177 return str_replace(['+','/'], ['-','_'], base64_encode($ret));
178 }
179
180 /**
181 * Convert string to safe slug
182 *
183 * @param string $data The data string
184 * @return ?string The slugged data
185 */
186 function slug(?string $data): ?string {
187 //With null
188 if ($data === null) {
189 //Return null
190 return $data;
191 }
192
193 //Use Transliterator if available
194 if (class_exists('Transliterator')) {
195 //Convert from any to latin, then to ascii and lowercase
196 $trans = \Transliterator::create('Any-Latin; Latin-ASCII; Lower()');
197 //Replace every non alphanumeric character by dash then trim dash
198 return trim(preg_replace('/[^a-zA-Z0-9]+/', '-', $trans->transliterate($data)), '-');
199 }
200
201 //Convert from utf-8 to ascii, replace quotes with space, remove non alphanumericseparator, replace separator with dash and trim dash
202 return trim(preg_replace('/[\/_|+ -]+/', '-', strtolower(preg_replace('/[^a-zA-Z0-9\/_|+ -]/', '', str_replace(['\'', '"'], ' ', iconv('UTF-8', 'ASCII//TRANSLIT', $data))))), '-');
203 }
204
205 /**
206 * Convert string to latin
207 *
208 * @param string $data The data string
209 * @return ?string The slugged data
210 */
211 function latin(?string $data): ?string {
212 //With null
213 if ($data === null) {
214 //Return null
215 return $data;
216 }
217
218 //Use Transliterator if available
219 if (class_exists('Transliterator')) {
220 //Convert from any to latin, then to ascii and lowercase
221 $trans = \Transliterator::create('Any-Latin; Latin-ASCII');
222 //Replace every non alphanumeric character by dash then trim dash
223 return trim($trans->transliterate($data));
224 }
225
226 //Convert from utf-8 to ascii
227 return trim(iconv('UTF-8', 'ASCII//TRANSLIT', $data));
228 }
229
230 /**
231 * Unshort then unserialize
232 *
233 * @param string $data The data string
234 * @return array The unshorted and unserialized data
235 */
236 public function unserialize(string $data): array {
237 //Return unshorted unserialized string
238 return json_decode($this->unshort($data), true);
239 }
240
241 /**
242 * Unshort
243 *
244 * @param string $data The data string
245 * @return string The unshorted data
246 */
247 public function unshort(string $data): string {
248 //Return string
249 $ret = '';
250
251 //Iterate on each character
252 foreach(str_split(base64_decode(str_replace(['-','_'], ['+','/'], $data))) as $c) {
253 //XXX: Reverse map char to an other one
254 $ret .= $this->alpha[(ord($c) + $this->offset) % $this->count];
255 }
256
257 //Send result
258 return $ret;
259 }
260 }