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