1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This file is part of Scout Extended. |
7
|
|
|
* |
8
|
|
|
* (c) Algolia Team <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Algolia\ScoutExtended\Searchable; |
15
|
|
|
|
16
|
|
|
use function get_class; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @internal |
20
|
|
|
*/ |
21
|
|
|
final class ObjectIdEncrypter |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Holds the metadata separator. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private static $separator = '::'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Encrypt the given searchable. |
32
|
|
|
* |
33
|
|
|
* @param mixed $searchable |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
16 |
|
public static function encrypt($searchable, int $part = null): string |
38
|
|
|
{ |
39
|
16 |
|
$scoutKey = method_exists($searchable, 'getScoutKey') ? $searchable->getScoutKey() : $searchable->getKey(); |
40
|
|
|
|
41
|
16 |
|
$meta = [get_class($searchable->getModel()), $scoutKey]; |
42
|
|
|
|
43
|
16 |
|
if ($part !== null) { |
44
|
5 |
|
$meta[] = $part; |
45
|
|
|
} |
46
|
|
|
|
47
|
16 |
|
return implode(self::$separator, $meta); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $objectId |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
3 |
|
public static function withoutPart(string $objectId): string |
56
|
|
|
{ |
57
|
3 |
|
return implode(self::$separator, [self::decryptSearchable($objectId), self::decryptSearchableKey($objectId)]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $objectId |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
3 |
|
public static function decryptSearchable(string $objectId): string |
66
|
|
|
{ |
67
|
3 |
|
return (string) explode(self::$separator, $objectId)[0]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $objectId |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
3 |
|
public static function decryptSearchableKey(string $objectId): string |
76
|
|
|
{ |
77
|
3 |
|
return (string) explode(self::$separator, $objectId)[1]; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|