1
<?php
2

3
namespace Nuwave\Lighthouse\GlobalId;
4

5
use Nuwave\Lighthouse\Support\Contracts\GlobalId as GlobalIdContract;
6

7
/**
8
 * The default encoding of global IDs in Lighthouse.
9
 *
10
 * The way that IDs are generated basically works like this:
11
 *
12
 * 1. Take the name of a type, e.g. "User" and an ID, e.g. 123
13
 * 2. Glue them together, separated by a colon, e.g. "User:123"
14
 * 3. base64_encode the result
15
 *
16
 * This can then be reversed to uniquely identify an entity in our
17
 * schema, just by looking at a single ID.
18
 */
19
class GlobalId implements GlobalIdContract
20
{
21 1
    public function encode(string $type, $id): string
22
    {
23 1
        return base64_encode($type.':'.$id);
24
    }
25

26 1
    public function decode(string $globalID): array
27
    {
28 1
        return explode(':', \Safe\base64_decode($globalID));
29
    }
30

31 1
    public function decodeID(string $globalID): string
32
    {
33 1
        [$type, $id] = self::decode($globalID);
34

35 1
        return trim($id);
36
    }
37

38 1
    public function decodeType(string $globalID): string
39
    {
40 1
        [$type, $id] = self::decode($globalID);
41

42 1
        return trim($type);
43
    }
44
}

Read our documentation on viewing source code .

Loading