Completed
Branch user-caps-n-site-permissions (6f6d8f)
by
unknown
41:20 queued 31:50
created

JsonDataNodeValidator::dataArrayEmpty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\services\json;
4
5
use DomainException;
6
7
/**
8
 * Class JsonDataNodeValidator
9
 *
10
 * @package EventEspresso\core\services\json
11
 * @author  Brent Christensen
12
 * @since   $VID:$
13
 */
14
class JsonDataNodeValidator
15
{
16
17
    /**
18
     * @param JsonDataNode $data_node
19
     * @return bool             returns true if data array is safe to set, false if overwrite will occur
20
     * @throws DomainException  throws exception if WP_DEBUG is true
21
     */
22
    public function dataArrayEmpty(JsonDataNode $data_node)
23
    {
24
        $data = $data_node->data();
25
        if (! empty($data)) {
26
            $this->overwriteError($data_node->nodeName(), esc_html__('data array', 'event_espresso'));
27
            return false;
28
        }
29
        return true;
30
    }
31
32
    /**
33
     * @param array  $data      data array to check for property key
34
     * @param string $key       value for the key being checked for
35
     * @param string $type      the type of key and/or the data being checked
36
     * @return bool             returns true if property is safe to write, false if overwrite will occur
37
     * @throws DomainException  throws exception if WP_DEBUG is true
38
     */
39
    public function propertyNotSet(array $data, $key, $type = 'key')
40
    {
41
        if (isset($data[ $key ])) {
42
            $this->overwriteError($key, $type);
43
            return false;
44
        }
45
        return true;
46
    }
47
48
49
    /**
50
     * @param string $key       value for the key being checked for
51
     * @param string $type      the type of key and/or the data being checked
52
     * @throws DomainException  throws exception if WP_DEBUG is true
53
     */
54
    public function overwriteError($key, $type)
55
    {
56
        if (WP_DEBUG) {
57
            throw new DomainException(
58
                sprintf(
59
                    /*
60
                     * translators:
61
                     * 'The "i18n" JsonDataNode key is already set and would be overwritten by the current action.'
62
                     */
63
                    esc_html__(
64
                        'The "%1$s" JsonDataNode %2$s is already set and would be overwritten by the current action.',
65
                        'event_espresso'
66
                    ),
67
                    $key,
68
                    $type
69
                )
70
            );
71
        }
72
    }
73
74
75
    /**
76
     * @param string $property  name for the key being checked for
77
     * @param string $type      the type of key and/or the data being checked
78
     * @param bool   $throw     if true [default] and WP_DEBUG is also true, then will throw exceptions
79
     * @return bool             returns true if property is set, false if property is missing
80
     * @throws DomainException  throws exception if WP_DEBUG is true
81
     */
82 View Code Duplication
    public function validateCriticalProperty($property, $type, $throw = true)
83
    {
84
        if (empty($property)) {
85
            if (WP_DEBUG && $throw) {
86
                throw new DomainException(
87
                    sprintf(
88
                        /*
89
                         * translators:
90
                         * 'The JsonDataNodeHandler domain route is a required property but has not been set.'
91
                         */
92
                        esc_html__(
93
                            'The JsonDataNodeHandler %1$s is a required property but has not been set.',
94
                            'event_espresso'
95
                        ),
96
                        $type
97
                    )
98
                );
99
            }
100
            return false;
101
        }
102
        return true;
103
    }
104
}
105