Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ProductTokenizer.php
1<?php
2
4
7
8final class ProductTokenizer
9{
10 private const PRODUCT_TOKEN = 'productToken';
11
12 public static function encode(array $productIds): string
13 {
14 $productIds = array_unique(array_filter(array_map('intval', $productIds)));
15 if (empty($productIds))
16 {
17 return '';
18 }
19
20 return self::encodeBase64Url(implode(',', $productIds));
21 }
22
23 public static function mixIntoUri(Uri $uri, array $productIds): Uri
24 {
25 $token = self::encode($productIds);
26 if ($token === '')
27 {
28 return $uri;
29 }
30
31 return $uri->addParams([
32 self::PRODUCT_TOKEN => self::encode($productIds),
33 ]);
34 }
35
36 public static function decode(string $token): array
37 {
38 if ($token === '')
39 {
40 return [];
41 }
42
43 $productIds = explode(',', self::decodeBase64Url($token));
44 if (!is_array($productIds) || empty($productIds))
45 {
46 return [];
47 }
48
49 return array_unique(array_filter(array_map('intval', $productIds)));
50 }
51
52 public static function decodeFromRequest(Request $request): ?array
53 {
54 $token = $request->get(self::PRODUCT_TOKEN);
55 if ($token !== null && is_string($token))
56 {
57 return self::decode($token);
58 }
59
60 return null;
61 }
62
63 public static function encodeBase64Url(string $data): string
64 {
65 return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
66 }
67
68 public static function decodeBase64Url(string $data): string
69 {
70 return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
71 }
72}
addParams(array $params, $preserveDots=false)
Definition uri.php:307