-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSet.php
More file actions
48 lines (40 loc) · 789 Bytes
/
Copy pathSet.php
File metadata and controls
48 lines (40 loc) · 789 Bytes
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
44
45
46
47
48
<?php declare(strict_types=1);
namespace h4kuna\DataType\Basic;
use h4kuna\DataType;
use Nette\StaticClass;
/**
* Transform set from string to array and vice versa
* MySQL data type SET to checkboxlist
* @example
* [
* foo => TRUE,
* bar => TRUE,
* joe => FALSE
* ]
* string: foo,bar
* array: [foo => TRUE, bar => TRUE]
*/
final class Set
{
use StaticClass;
/**
* @return array<string, true>
*/
public static function fromString(string $value): array
{
return array_fill_keys(explode(',', $value), true);
}
/**
* @param array<string, bool|null> $set
*/
public static function toString(array $set): string
{
$out = [];
foreach ($set as $k => $v) {
if ($v !== null && $v !== false) {
$out[] = $k;
}
}
return implode(',', $out);
}
}