Completed
Branch EDTR/master (83b47e)
by
unknown
25:37 queued 16:41
created

JsonDataNodeValidator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 76
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A propertyNotSet() 0 8 2
A overwriteError() 0 19 2
A validateCriticalProperty() 0 22 4
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 array  $data      data array to check for property key
19
     * @param string $key       value for the key being checked for
20
     * @param string $type      the type of key and/or the data being checked
21
     * @return bool             returns true if property is safe to write, false if overwrite will occur
22
     * @throws DomainException  throws exception if WP_DEBUG is true
23
     */
24
    public function propertyNotSet(array $data, $key, $type = 'key')
25
    {
26
        if (isset($data[ $key ])) {
27
            $this->overwriteError($key, $type);
28
            return false;
29
        }
30
        return true;
31
    }
32
33
34
    /**
35
     * @param string $key       value for the key being checked for
36
     * @param string $type      the type of key and/or the data being checked
37
     * @throws DomainException  throws exception if WP_DEBUG is true
38
     */
39
    public function overwriteError($key, $type)
40
    {
41
        if (WP_DEBUG) {
42
            throw new DomainException(
43
                sprintf(
44
                    /*
45
                     * translators:
46
                     * 'The "i18n" JsonDataNode key is already set and would be overwritten by the current action.'
47
                     */
48
                    esc_html__(
49
                        'The "%1$s" JsonDataNode %2$s is already set and would be overwritten by the current action.',
50
                        'event_espresso'
51
                    ),
52
                    $key,
53
                    $type
54
                )
55
            );
56
        }
57
    }
58
59
60
    /**
61
     * @param string $property  name for the key being checked for
62
     * @param string $type      the type of key and/or the data being checked
63
     * @param bool   $throw     if true [default] and WP_DEBUG is also true, then will throw exceptions
64
     * @return bool             returns true if property is set, false if property is missing
65
     * @throws DomainException  throws exception if WP_DEBUG is true
66
     */
67
    public function validateCriticalProperty($property, $type, $throw = true)
68
    {
69
        if (empty($property)) {
70
            if (WP_DEBUG && $throw) {
71
                throw new DomainException(
72
                    sprintf(
73
                        /*
74
                         * translators:
75
                         * 'The JsonDataNodeHandler domain route is a required property but has not been set.'
76
                         */
77
                        esc_html__(
78
                            'The JsonDataNodeHandler %1$s is a required property but has not been set.',
79
                            'event_espresso'
80
                        ),
81
                        $type
82
                    )
83
                );
84
            }
85
            return false;
86
        }
87
        return true;
88
    }
89
}
90