-
Notifications
You must be signed in to change notification settings - Fork 61
/
Field.php
145 lines (118 loc) · 3.8 KB
/
Field.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* Copyright (c) Vincent Klaiber
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @see https://github.com/vinkla/extended-acf
*/
declare(strict_types=1);
namespace Extended\ACF\Fields;
use Extended\ACF\Key;
use InvalidArgumentException;
abstract class Field
{
protected array $settings;
protected string $keyPrefix = 'field';
protected string|null $type = null;
public function __construct(string $label, string|null $name = null)
{
$this->settings = [
'label' => $label,
'name' => $name ?? Key::sanitize($label),
];
}
public static function make(string $label, string|null $name = null): static
{
return new static($label, $name);
}
/** @throws \InvalidArgumentException */
public function withSettings(array $settings): static
{
$invalidKeys = [
'collapsed',
'conditional_logic',
'key',
'label',
'layouts',
'name',
'sub_fields',
'type',
];
foreach ($invalidKeys as $key) {
if (array_key_exists($key, $settings)) {
throw new InvalidArgumentException("Invalid settings key [$key].");
}
}
$this->settings = array_merge($this->settings, $settings);
return $this;
}
public function dump(...$args): static
{
dump($this->get(), ...$args);
return $this;
}
public function dd(...$args): never
{
dd($this->get(), ...$args);
}
/**
* Avoid using custom field keys unless you thoroughly understand them. The
* field keys are automatically generated when you use the
* `register_extended_field_group` function.
* @throws \InvalidArgumentException
*/
public function key(string $key): static
{
if (!str_starts_with($key, $this->keyPrefix . '_')) {
throw new InvalidArgumentException(
sprintf('The key should have the prefix [%s_].', $this->keyPrefix),
);
}
if (Key::has($key)) {
throw new InvalidArgumentException("The key [$key] is not unique.");
}
$this->settings['key'] = $key;
Key::set($key, $key);
return $this;
}
/** @internal */
public function get(string|null $parentKey = null): array
{
$key =
$this->settings['key'] ??
$parentKey . '_' . Key::sanitize($this->settings['name']);
if ($this->type !== null) {
$this->settings['type'] = $this->type;
}
if (isset($this->settings['conditional_logic'])) {
$this->settings['conditional_logic'] = array_map(
fn($rules) => $rules->get($parentKey),
$this->settings['conditional_logic'],
);
}
if (isset($this->settings['layouts'])) {
$this->settings['layouts'] = array_map(
fn($layout) => $layout->get($key),
$this->settings['layouts'],
);
}
if (isset($this->settings['sub_fields'])) {
$this->settings['sub_fields'] = array_map(
fn($field) => $field->get($key),
$this->settings['sub_fields'],
);
}
if (isset($this->settings['collapsed'], $this->settings['sub_fields'])) {
foreach ($this->settings['sub_fields'] as $field) {
if ($field['name'] === $this->settings['collapsed']) {
$this->settings['collapsed'] = $field['key'];
break;
}
}
}
$this->settings['key'] ??= Key::generate($key, $this->keyPrefix);
return $this->settings;
}
}