Jake Vanderwerf
13 hours ago f16cb88a3218ac7bb32e43f0e0a2542d35c7a01b
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
<?php
namespace JVBase\meta;
 
if (!defined('ABSPATH')) {
    exit;
}
 
class Group
{
    protected Meta $meta;
    protected string $name;
    protected array $data;
    protected array $config;
 
    public function __construct(Meta $meta, string $name)
    {
        $this->meta = $meta;
        $this->name = $name;
        $this->config = $meta->config($name) ?? [];
 
        $data = $meta->get($name);
        $this->data = is_array($data) ? $data : [];
    }
 
    public function get(string $path): mixed
    {
        $parts = explode('|', $path);
        return $this->getPath($this->data, $this->config['fields'] ?? [], $parts);
    }
    protected function getPath(array $data, array $fieldsConfig, array $parts): mixed
    {
        $key = array_shift($parts);
        $fieldConfig = $fieldsConfig[$key] ?? false;
 
        if (!$fieldConfig) {
            return '';
        }
 
        if (empty($parts)) {
            return $data[$key] ?? $fieldConfig['default'] ?? '';
        }
 
        $nested = is_array($data[$key] ?? null) ? $data[$key] : [];
        return $this->getPath($nested, $fieldConfig['fields'] ?? [], $parts);
    }
 
    public function set(string $path, mixed $value, bool $autosave = true): self
    {
        $parts = explode('|', $path);
        $this->setPath($this->data, $this->config['fields'] ?? [], $parts, $value);
        $this->sync($autosave);
        return $this;
    }
 
    protected function setPath(array &$data, array $fieldsConfig, array $parts, mixed $value): void
    {
        $key = array_shift($parts);
        $fieldConfig = $fieldsConfig[$key] ?? false;
 
        if (!$fieldConfig) {
            error_log('[Group]::set Could not find child field config: ' . $key);
            return;
        }
 
        if (empty($parts)) {
            $data[$key] = $value;
            return;
        }
 
        // Nested group - descend into it
        if (!isset($data[$key]) || !is_array($data[$key])) {
            $data[$key] = [];
        }
        $this->setPath($data[$key], $fieldConfig['fields'] ?? [], $parts, $value);
    }
 
    public function setAll(array $data): self
    {
        foreach ($data as $field => $value) {
            $fieldConfig = $this->config['fields'][$field] ?? false;
            if (!$fieldConfig) {
                continue;
            }
            $this->data[$field] = $value;
        }
        $this->sync(true);
        return $this;
    }
 
    public function all(): array
    {
        return $this->data;
    }
 
    public function has(string $field): bool
    {
        return isset($this->config['fields'][$field]);
    }
 
    protected function sync(bool $autosave = true): void
    {
        $this->meta->set($this->name, $this->data, $autosave);
    }
}