Jake Vanderwerf
5 days ago 0dfe1d8afafc59c4a5559c498342668d5a58d6ef
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
<?php
namespace JVBase\integrations;
 
if (!defined('ABSPATH')) {
    exit;
}
 
class Endpoint {
    protected string $endpoint;
    protected array $fields;
    public function __construct(string $endpoint) {
        $this->endpoint = $endpoint;
    }
 
    public function getEndpoint():string
    {
        return $this->endpoint;
    }
 
    public function addIdempotencyKey(?string $key = null):EndpointField
    {
        if (is_null($key)) {
            $key = wp_generate_uuid4();
        }
        $field = new EndpointField('idempotency_key');
        $this->fields['idempotency_key'] = $field;
        return $field;
    }
 
    public function getFields():array
    {
        return $this->fields;
    }
    public function addField(string $slug, array $data = []):EndpointField
    {
        $slug = EndpointField::sanitizeSlug($slug);
        $field = new EndpointField($slug);
        if (!empty($data)) {
            foreach ($data as $key => $value) {
                if ($key === 'required' && $value !== false) {
                    $field->setRequired();
                } elseif (property_exists($field, $key)) {
                    $method = 'set'.implode('',array_map('ucfirst', explode('_',$key)));
                    if (method_exists($field, $method)) {
                        $field->$method($value);
                    }
                }
            }
        }
        $this->fields[$slug] = $field;
        return $field;
    }
    public function checkFields(array $fields):array|false
    {
        $fields = array_filter($fields, function ($f) use ($fields) {
            if (!array_key_exists($f, $this->fields)) {
                error_log('Endpoint '.$this->endpoint.'::checkFields removing field because it is not defined: '.print_r($fields[$f], true));
                return false;
            }
            return true;
        }, ARRAY_FILTER_USE_KEY);
 
        $required = array_filter($this->fields, function ($f) {
            return $f->isRequired();
        });
 
        $missing = [];
        foreach ($required as $field => $config) {
            if (!array_key_exists($field, $fields) || empty($fields[$field])) {
                error_log('Endpoint '.$this->endpoint.'::checkFields required field not found: '.$field);
                $missing[] = $field;
            }
        }
        if (!empty($missing)) {
            return false;
        }
 
        foreach ($fields as $field => $value) {
            $test = $this->fields[$field]->validate($value);
            if (!$test) {
                error_log('Endpoint '.$this->endpoint.'::checkFields could not validate value for '.$field.': '.print_r($value, true));
                unset($fields[$field]);
            }
        }
 
        return $fields;
    }
}