Repository navigation
Expand file tree
/
Copy pathSubRowKey.php
More file actions
43 lines (36 loc) · 1.44 KB
/
Copy pathSubRowKey.php
File metadata and controls
43 lines (36 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
declare(strict_types=1);
namespace NyonCode\WireTable\Support;
/**
* The key a sub-row action travels under: the parent row's key and the child's.
*
* A sub-row action runs through the same entry points as a row action
* (`openActionModal`, `executeTableAction`, the modal frame, the halt), and those
* carry one record key. A child's own key would be looked up among the PARENT
* rows — another model, so another record with the same id, or none — so a
* sub-row button sends both, and the host finds the child through the parent
* the table's own query returns. That is also what keeps a forged key from
* reaching a child of a row the viewer cannot see.
*
* The separator never occurs in an integer, UUID or ULID key and survives the
* single-quoted Livewire expression it is written into.
*/
final class SubRowKey
{
private const SEPARATOR = '~sub~';
public static function encode(int|string $parentKey, int|string $childKey): string
{
return $parentKey.self::SEPARATOR.$childKey;
}
/**
* @return array{0: string, 1: string}|null [parent key, child key], or null for a row key
*/
public static function decode(int|string|null $key): ?array
{
if (! is_string($key) || ! str_contains($key, self::SEPARATOR)) {
return null;
}
[$parent, $child] = explode(self::SEPARATOR, $key, 2);
return $parent === '' || $child === '' ? null : [$parent, $child];
}
}