Completed
Branch FET-8385-datetime-ticket-selec... (ec342c)
by
unknown
45:18 queued 34:32
created

Registry::verifyDataNotExisting()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 18
nc 3
nop 1
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
1
<?php
2
namespace EventEspresso\core\services\assets;
3
4
use InvalidArgumentException;
5
6
defined('EVENT_ESPRESSO_VERSION') || exit;
7
8
/**
9
 * Used for registering assets used in EE.
10
 *
11
 * @package    EventEspresso
12
 * @subpackage services\assets
13
 * @author     Darren Ethier
14
 * @since      4.9.24.rc.004
15
 */
16
class Registry
17
{
18
19
    /**
20
     * This holds the jsdata data object that will be exposed on pages that enqueue the `eejs-core` script.
21
     * @var array
22
     */
23
    protected $jsdata = array();
24
25
26
    /**
27
     * Registry constructor.
28
     * Hooking into WP actions for script registry.
29
     */
30
    public function __construct()
31
    {
32
        add_action('wp_enqueue_scripts', array($this, 'scripts'), 100);
33
        add_action('admin_enqueue_scripts', array($this, 'scripts'), 100);
34
    }
35
36
37
    /**
38
     * Callback for the WP script actions.
39
     * Used to register globally accessible core scripts.
40
     * Also used to add the eejs.data object to the source for any js having eejs-core as a dependency.
41
     */
42
    public function scripts()
43
    {
44
        wp_register_script(
45
            'eejs-core',
46
            EE_PLUGIN_DIR_URL . 'core/services/assets/core_assets/eejs-core.js',
47
            array(),
48
            espresso_version(),
49
            true
50
        );
51
        wp_localize_script('eejs-core', 'eejs', array('data'=>$this->jsdata));
52
    }
53
54
55
    /**
56
     * Used to add data to eejs.data object.
57
     *
58
     * Note:  Overriding existing data is not allowed.
59
     *
60
     * Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript.
61
     * If the data you add is something like this:
62
     *
63
     * $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) );
64
     *
65
     * It will be exposed in the page source as:
66
     *
67
     * eejs.data.my_plugin_data.foo == gar
68
     *
69
     * @param string $key          Key used to access your data
70
     * @param string|array $value  Value to attach to key
71
     * @throws \InvalidArgumentException
72
     */
73
    public function addData($key, $value)
74
    {
75
        if ($this->verifyDataNotExisting($key)) {
76
            $this->jsdata[$key] = $value;
77
        }
78
    }
79
80
81
    /**
82
     * Similar to addData except this allows for users to push values to an existing key where the values on key are
83
     * elements in an array.
84
     *
85
     * When you use this method, the value you include will be appended to the end of an array on $key.
86
     *
87
     * So if the $key was 'test' and you added a value of 'my_data' then it would be represented in the javascript object
88
     * like this,
89
     *
90
     * eejs.data.test = [
91
     *     my_data,
92
     * ]
93
     *
94
     * If there has already been a scalar value attached to the data object given key, then
95
     * this will throw an exception.
96
     *
97
     * @param string $key          Key to attach data to.
98
     * @param string|array $value  Value being registered.
99
     * @throws InvalidArgumentException
100
     */
101
    public function pushData($key, $value)
102
    {
103
        if (isset($this->jsdata[$key])
104
            && ! is_array($this->jsdata[$key])
105
        ) {
106
            throw new invalidArgumentException(
107
                sprintf(
108
                    __(
109
                        'The value for %1$s is already set and it is not an array. The %2$s method can only be used to
110
                         push values to this data element when it is an array.',
111
                        'event_espresso'
112
                    ),
113
                    $key,
114
                    __METHOD__
115
                )
116
            );
117
        }
118
119
        $this->jsdata[$key][] = $value;
120
    }
121
122
123
    /**
124
     * Retrieve registered data.
125
     *
126
     * @param string $key           Name of key to attach data to.
127
     * @return mixed                If there is no for the given key, then false is returned.
128
     */
129
    public function getData($key)
130
    {
131
        return isset($this->jsdata[$key])
132
            ? $this->jsdata[$key]
133
            : false;
134
    }
135
136
137
138
139
    /**
140
     * Verifies whether the given data exists already on the jsdata array.
141
     *
142
     * Overriding data is not allowed.
143
     *
144
     * @param string       $key          Index for data.
145
     * @return bool        If valid then return true.
146
     * @throws InvalidArgumentException if data already exists.
147
     */
148
    protected function verifyDataNotExisting($key)
149
    {
150
        if (isset($this->jsdata[$key])) {
151
            if (is_array($this->jsdata[$key])) {
152
                throw new InvalidArgumentException(
153
                    sprintf(
154
                        __(
155
                            'The value for %1$s already exists in the Registry::eejs object.
156
                            Overrides are not allowed. Since the value of this data is an array, you may want to use the
157
                            %2$s method to push your value to the array.',
158
                            'event_espresso'
159
                        ),
160
                        $key,
161
                        'pushData()'
162
                    )
163
                );
164
            } else {
165
                throw new InvalidArgumentException(
166
                    sprintf(
167
                        __(
168
                            'The value for %1$s already exists in the Registry::eejs object. Overrides are not
169
                            allowed.  Consider attaching your value to a different key',
170
                            'event_espresso'
171
                        ),
172
                        $key
173
                    )
174
                );
175
            }
176
        }
177
        return true;
178
    }
179
}
180