-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Select.php
85 lines (70 loc) · 2.06 KB
/
Select.php
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace Notion\Databases\Properties;
/**
* @psalm-import-type SelectOptionJson from SelectOption
*
* @psalm-type SelectJson = array{
* id: string,
* name: string,
* type: "select",
* select: array{ options: SelectOptionJson[] },
* }
*/
class Select implements PropertyInterface
{
private const TYPE = Property::TYPE_SELECT;
private Property $property;
/** @var SelectOption[] */
private array $options;
/** @param SelectOption[] $options */
private function __construct(Property $property, array $options)
{
$this->property = $property;
$this->options = $options;
}
/** @param SelectOption[] $options */
public static function create(string $propertyName = "Select", array $options = []): self
{
$property = Property::create("", $propertyName, self::TYPE);
return new self($property, $options);
}
public function property(): Property
{
return $this->property;
}
/** @return SelectOption[] */
public function options(): array
{
return $this->options;
}
public function withOptions(SelectOption ...$options): self
{
return new self($this->property, $options);
}
public function addOption(SelectOption $option): self
{
$options = $this->options;
$options[] = $option;
return new self($this->property, $options);
}
public static function fromArray(array $array): self
{
/** @psalm-var SelectJson $array */
$property = Property::fromArray($array);
$options = array_map(
function (array $option): SelectOption {
return SelectOption::fromArray($option);
},
$array[self::TYPE]["options"],
);
return new self($property, $options);
}
public function toArray(): array
{
$array = $this->property->toArray();
$array[self::TYPE] = [
"options" => array_map(fn(SelectOption $o) => $o->toArray(), $this->options),
];
return $array;
}
}