Completed
Branch master (87d3f3)
by
unknown
03:30
created
core/services/request/RequestInterface.php 1 patch
Indentation   +218 added lines, -218 removed lines patch added patch discarded remove patch
@@ -15,224 +15,224 @@
 block discarded – undo
15 15
  */
16 16
 interface RequestInterface extends RequestTypeContextCheckerInterface
17 17
 {
18
-    /**
19
-     * @param RequestTypeContextCheckerInterface $type
20
-     */
21
-    public function setRequestTypeContextChecker(RequestTypeContextCheckerInterface $type);
18
+	/**
19
+	 * @param RequestTypeContextCheckerInterface $type
20
+	 */
21
+	public function setRequestTypeContextChecker(RequestTypeContextCheckerInterface $type);
22 22
 
23 23
 
24
-    /**
25
-     * @return array
26
-     */
27
-    public function getParams();
28
-
29
-
30
-    /**
31
-     * @return array
32
-     */
33
-    public function postParams();
34
-
35
-
36
-    /**
37
-     * @return array
38
-     */
39
-    public function cookieParams();
40
-
41
-
42
-    /**
43
-     * @return array
44
-     */
45
-    public function serverParams();
46
-
47
-
48
-    /**
49
-     * @param string $key
50
-     * @param mixed|null $default
51
-     * @return array|int|float|string
52
-     */
53
-    public function getServerParam($key, $default = null);
54
-
55
-
56
-    /**
57
-     * @param string                 $key
58
-     * @param array|int|float|string $value
59
-     * @param bool                   $set_global_too
60
-     * @return void
61
-     */
62
-    public function setServerParam(string $key, $value, bool $set_global_too = false);
63
-
64
-
65
-    /**
66
-     * @param string $key
67
-     * @return bool
68
-     */
69
-    public function serverParamIsSet($key);
70
-
71
-
72
-    /**
73
-     * @return array
74
-     */
75
-    public function filesParams();
76
-
77
-
78
-    /**
79
-     * returns sanitized contents of $_REQUEST
80
-     *
81
-     * @return array
82
-     */
83
-    public function requestParams();
84
-
85
-
86
-    /**
87
-     * @param string $key
88
-     * @param string $value
89
-     * @param bool   $override_ee
90
-     * @return void
91
-     */
92
-    public function setRequestParam($key, $value, $override_ee = false);
93
-
94
-
95
-    /**
96
-     * returns   the value for a request param if the given key exists
97
-     *
98
-     * @param string     $key
99
-     * @param mixed|null $default
100
-     * @param string     $type      the expected data type for the parameter's value, ie: string, int, bool, etc
101
-     * @param bool       $is_array  if true, then parameter value will be treated as an array of $type
102
-     * @param string     $delimiter for CSV type strings that should be returned as an array
103
-     * @return array|bool|float|int|string
104
-     */
105
-    public function getRequestParam($key, $default = null, $type = DataType::STRING, $is_array = false, $delimiter = '');
106
-
107
-
108
-    /**
109
-     * check if param exists
110
-     *
111
-     * @param string $key
112
-     * @return bool
113
-     */
114
-    public function requestParamIsSet($key);
115
-
116
-
117
-    /**
118
-     * check if a request parameter exists whose key that matches the supplied wildcard pattern
119
-     * and return the value for the first match found
120
-     * wildcards can be either of the following:
121
-     *      ? to represent a single character of any type
122
-     *      * to represent one or more characters of any type
123
-     *
124
-     * @param string     $pattern
125
-     * @param mixed|null $default
126
-     * @param string     $type      the expected data type for the parameter's value, ie: string, int, bool, etc
127
-     * @param bool       $is_array  if true, then parameter value will be treated as an array of $type
128
-     * @param string     $delimiter for CSV type strings that should be returned as an array
129
-     * @return array|bool|float|int|string
130
-     */
131
-    public function getMatch($pattern, $default = null, $type = DataType::STRING, $is_array = false, $delimiter = '');
132
-
133
-
134
-    /**
135
-     * check if a request parameter exists whose key matches the supplied wildcard pattern
136
-     * wildcards can be either of the following:
137
-     *      ? to represent a single character of any type
138
-     *      * to represent one or more characters of any type
139
-     * returns true if a match is found or false if not
140
-     *
141
-     * @param string $pattern
142
-     * @return false|int
143
-     */
144
-    public function matches($pattern);
145
-
146
-
147
-    /**
148
-     * remove param
149
-     *
150
-     * @param string $key
151
-     * @param bool   $unset_from_global_too
152
-     */
153
-    public function unSetRequestParam($key, $unset_from_global_too = false);
154
-
155
-
156
-    /**
157
-     * remove params
158
-     *
159
-     * @param array $keys
160
-     * @param bool  $unset_from_global_too
161
-     */
162
-    public function unSetRequestParams(array $keys, $unset_from_global_too = false);
163
-
164
-
165
-    /**
166
-     * @param string $key
167
-     * @param bool   $unset_from_global_too
168
-     * @return void
169
-     */
170
-    public function unSetServerParam(string $key, bool $unset_from_global_too = false);
171
-
172
-
173
-    /**
174
-     * @return string
175
-     */
176
-    public function ipAddress();
177
-
178
-
179
-    /**
180
-     * @param boolean $relativeToWpRoot    whether or not to return the uri relative to WordPress' home URL.
181
-     * @param boolean $remove_query_params whether or not to return the uri with all query params removed.
182
-     * @return string
183
-     */
184
-    public function requestUri($relativeToWpRoot = false, $remove_query_params = false);
185
-
186
-
187
-    /**
188
-     * @return string
189
-     */
190
-    public function userAgent();
191
-
192
-
193
-    /**
194
-     * @param string $user_agent
195
-     */
196
-    public function setUserAgent($user_agent = '');
197
-
198
-
199
-    /**
200
-     * @return bool
201
-     */
202
-    public function isBot();
203
-
204
-
205
-    /**
206
-     * @param bool $is_bot
207
-     */
208
-    public function setIsBot($is_bot);
209
-
210
-
211
-    /**
212
-     * returns the path portion of the current request URI with both the WP Root (home_url()) and query params removed
213
-     *
214
-     * @return string
215
-     * @since   $VID:$
216
-     */
217
-    public function requestPath();
218
-
219
-
220
-    /**
221
-     * returns true if the last segment of the current request path (without params) matches the provided string
222
-     *
223
-     * @param string $uri_segment
224
-     * @return bool
225
-     * @since   $VID:$
226
-     */
227
-    public function currentPageIs($uri_segment);
228
-
229
-
230
-    /**
231
-     * merges the incoming array of parameters into the existing request parameters
232
-     *
233
-     * @param array $request_params
234
-     * @return mixed
235
-     * @since   4.10.24.p
236
-     */
237
-    public function mergeRequestParams(array $request_params);
24
+	/**
25
+	 * @return array
26
+	 */
27
+	public function getParams();
28
+
29
+
30
+	/**
31
+	 * @return array
32
+	 */
33
+	public function postParams();
34
+
35
+
36
+	/**
37
+	 * @return array
38
+	 */
39
+	public function cookieParams();
40
+
41
+
42
+	/**
43
+	 * @return array
44
+	 */
45
+	public function serverParams();
46
+
47
+
48
+	/**
49
+	 * @param string $key
50
+	 * @param mixed|null $default
51
+	 * @return array|int|float|string
52
+	 */
53
+	public function getServerParam($key, $default = null);
54
+
55
+
56
+	/**
57
+	 * @param string                 $key
58
+	 * @param array|int|float|string $value
59
+	 * @param bool                   $set_global_too
60
+	 * @return void
61
+	 */
62
+	public function setServerParam(string $key, $value, bool $set_global_too = false);
63
+
64
+
65
+	/**
66
+	 * @param string $key
67
+	 * @return bool
68
+	 */
69
+	public function serverParamIsSet($key);
70
+
71
+
72
+	/**
73
+	 * @return array
74
+	 */
75
+	public function filesParams();
76
+
77
+
78
+	/**
79
+	 * returns sanitized contents of $_REQUEST
80
+	 *
81
+	 * @return array
82
+	 */
83
+	public function requestParams();
84
+
85
+
86
+	/**
87
+	 * @param string $key
88
+	 * @param string $value
89
+	 * @param bool   $override_ee
90
+	 * @return void
91
+	 */
92
+	public function setRequestParam($key, $value, $override_ee = false);
93
+
94
+
95
+	/**
96
+	 * returns   the value for a request param if the given key exists
97
+	 *
98
+	 * @param string     $key
99
+	 * @param mixed|null $default
100
+	 * @param string     $type      the expected data type for the parameter's value, ie: string, int, bool, etc
101
+	 * @param bool       $is_array  if true, then parameter value will be treated as an array of $type
102
+	 * @param string     $delimiter for CSV type strings that should be returned as an array
103
+	 * @return array|bool|float|int|string
104
+	 */
105
+	public function getRequestParam($key, $default = null, $type = DataType::STRING, $is_array = false, $delimiter = '');
106
+
107
+
108
+	/**
109
+	 * check if param exists
110
+	 *
111
+	 * @param string $key
112
+	 * @return bool
113
+	 */
114
+	public function requestParamIsSet($key);
115
+
116
+
117
+	/**
118
+	 * check if a request parameter exists whose key that matches the supplied wildcard pattern
119
+	 * and return the value for the first match found
120
+	 * wildcards can be either of the following:
121
+	 *      ? to represent a single character of any type
122
+	 *      * to represent one or more characters of any type
123
+	 *
124
+	 * @param string     $pattern
125
+	 * @param mixed|null $default
126
+	 * @param string     $type      the expected data type for the parameter's value, ie: string, int, bool, etc
127
+	 * @param bool       $is_array  if true, then parameter value will be treated as an array of $type
128
+	 * @param string     $delimiter for CSV type strings that should be returned as an array
129
+	 * @return array|bool|float|int|string
130
+	 */
131
+	public function getMatch($pattern, $default = null, $type = DataType::STRING, $is_array = false, $delimiter = '');
132
+
133
+
134
+	/**
135
+	 * check if a request parameter exists whose key matches the supplied wildcard pattern
136
+	 * wildcards can be either of the following:
137
+	 *      ? to represent a single character of any type
138
+	 *      * to represent one or more characters of any type
139
+	 * returns true if a match is found or false if not
140
+	 *
141
+	 * @param string $pattern
142
+	 * @return false|int
143
+	 */
144
+	public function matches($pattern);
145
+
146
+
147
+	/**
148
+	 * remove param
149
+	 *
150
+	 * @param string $key
151
+	 * @param bool   $unset_from_global_too
152
+	 */
153
+	public function unSetRequestParam($key, $unset_from_global_too = false);
154
+
155
+
156
+	/**
157
+	 * remove params
158
+	 *
159
+	 * @param array $keys
160
+	 * @param bool  $unset_from_global_too
161
+	 */
162
+	public function unSetRequestParams(array $keys, $unset_from_global_too = false);
163
+
164
+
165
+	/**
166
+	 * @param string $key
167
+	 * @param bool   $unset_from_global_too
168
+	 * @return void
169
+	 */
170
+	public function unSetServerParam(string $key, bool $unset_from_global_too = false);
171
+
172
+
173
+	/**
174
+	 * @return string
175
+	 */
176
+	public function ipAddress();
177
+
178
+
179
+	/**
180
+	 * @param boolean $relativeToWpRoot    whether or not to return the uri relative to WordPress' home URL.
181
+	 * @param boolean $remove_query_params whether or not to return the uri with all query params removed.
182
+	 * @return string
183
+	 */
184
+	public function requestUri($relativeToWpRoot = false, $remove_query_params = false);
185
+
186
+
187
+	/**
188
+	 * @return string
189
+	 */
190
+	public function userAgent();
191
+
192
+
193
+	/**
194
+	 * @param string $user_agent
195
+	 */
196
+	public function setUserAgent($user_agent = '');
197
+
198
+
199
+	/**
200
+	 * @return bool
201
+	 */
202
+	public function isBot();
203
+
204
+
205
+	/**
206
+	 * @param bool $is_bot
207
+	 */
208
+	public function setIsBot($is_bot);
209
+
210
+
211
+	/**
212
+	 * returns the path portion of the current request URI with both the WP Root (home_url()) and query params removed
213
+	 *
214
+	 * @return string
215
+	 * @since   $VID:$
216
+	 */
217
+	public function requestPath();
218
+
219
+
220
+	/**
221
+	 * returns true if the last segment of the current request path (without params) matches the provided string
222
+	 *
223
+	 * @param string $uri_segment
224
+	 * @return bool
225
+	 * @since   $VID:$
226
+	 */
227
+	public function currentPageIs($uri_segment);
228
+
229
+
230
+	/**
231
+	 * merges the incoming array of parameters into the existing request parameters
232
+	 *
233
+	 * @param array $request_params
234
+	 * @return mixed
235
+	 * @since   4.10.24.p
236
+	 */
237
+	public function mergeRequestParams(array $request_params);
238 238
 }
Please login to merge, or discard this patch.
core/services/routing/RouteInterface.php 1 patch
Indentation   +36 added lines, -36 removed lines patch added patch discarded remove patch
@@ -18,50 +18,50 @@
 block discarded – undo
18 18
  */
19 19
 interface RouteInterface
20 20
 {
21
-    /**
22
-     * @return JsonDataNode
23
-     */
24
-    public function dataNode(): ?JsonDataNode;
21
+	/**
22
+	 * @return JsonDataNode
23
+	 */
24
+	public function dataNode(): ?JsonDataNode;
25 25
 
26 26
 
27
-    /**
28
-     * final method called by RouteHandler on Route which in turn calls requestHandler()
29
-     *
30
-     * @return bool
31
-     * @since   $VID:$
32
-     */
33
-    public function handleRequest(): bool;
27
+	/**
28
+	 * final method called by RouteHandler on Route which in turn calls requestHandler()
29
+	 *
30
+	 * @return bool
31
+	 * @since   $VID:$
32
+	 */
33
+	public function handleRequest(): bool;
34 34
 
35 35
 
36
-    /**
37
-     * a place to run any setup required for matchesCurrentRequest() which runs immediately after
38
-     *
39
-     * @since $VID:$
40
-     */
41
-    public function initialize();
36
+	/**
37
+	 * a place to run any setup required for matchesCurrentRequest() which runs immediately after
38
+	 *
39
+	 * @since $VID:$
40
+	 */
41
+	public function initialize();
42 42
 
43 43
 
44
-    /**
45
-     * returns true if the route has already been handled
46
-     *
47
-     * @return bool
48
-     */
49
-    public function isHandled(): bool;
44
+	/**
45
+	 * returns true if the route has already been handled
46
+	 *
47
+	 * @return bool
48
+	 */
49
+	public function isHandled(): bool;
50 50
 
51 51
 
52
-    /**
53
-     * returns true if the route has not yet been handled
54
-     *
55
-     * @return bool
56
-     */
57
-    public function isNotHandled(): bool;
52
+	/**
53
+	 * returns true if the route has not yet been handled
54
+	 *
55
+	 * @return bool
56
+	 */
57
+	public function isNotHandled(): bool;
58 58
 
59 59
 
60
-    /**
61
-     * returns true if the current request matches this route
62
-     *
63
-     * @return bool
64
-     * @since   $VID:$
65
-     */
66
-    public function matchesCurrentRequest(): bool;
60
+	/**
61
+	 * returns true if the current request matches this route
62
+	 *
63
+	 * @return bool
64
+	 * @since   $VID:$
65
+	 */
66
+	public function matchesCurrentRequest(): bool;
67 67
 }
Please login to merge, or discard this patch.
core/services/json/JsonDataNodeInterface.php 1 patch
Indentation   +45 added lines, -45 removed lines patch added patch discarded remove patch
@@ -18,61 +18,61 @@
 block discarded – undo
18 18
  */
19 19
 interface JsonDataNodeInterface extends JsonSerializable
20 20
 {
21
-    /**
22
-     * @param JsonDataNode $data_node
23
-     * @throws DomainException
24
-     */
25
-    public function addDataNode(JsonDataNode $data_node);
21
+	/**
22
+	 * @param JsonDataNode $data_node
23
+	 * @throws DomainException
24
+	 */
25
+	public function addDataNode(JsonDataNode $data_node);
26 26
 
27 27
 
28
-    /**
29
-     * the actual data in key value array format
30
-     *
31
-     * @return array
32
-     */
33
-    public function data();
28
+	/**
29
+	 * the actual data in key value array format
30
+	 *
31
+	 * @return array
32
+	 */
33
+	public function data();
34 34
 
35 35
 
36
-    /**
37
-     * specifies the domain (use case) that this route defines
38
-     * ! IMPORTANT !
39
-     * only one domain can be set pre request
40
-     *
41
-     * @return string
42
-     */
43
-    public function domain();
36
+	/**
37
+	 * specifies the domain (use case) that this route defines
38
+	 * ! IMPORTANT !
39
+	 * only one domain can be set pre request
40
+	 *
41
+	 * @return string
42
+	 */
43
+	public function domain();
44 44
 
45 45
 
46
-    /**
47
-     * !!! IMPORTANT !!!
48
-     * JsonDataNode::setInitialized(true) needs to be called once initialization is complete
49
-     * else you're a bad person and bad things will happen to you !!!
50
-     *
51
-     * @since $VID:$
52
-     */
53
-    public function initialize();
46
+	/**
47
+	 * !!! IMPORTANT !!!
48
+	 * JsonDataNode::setInitialized(true) needs to be called once initialization is complete
49
+	 * else you're a bad person and bad things will happen to you !!!
50
+	 *
51
+	 * @since $VID:$
52
+	 */
53
+	public function initialize();
54 54
 
55 55
 
56
-    /**
57
-     * true if the data node has been initialized,
58
-     * which entails retrieving the required data and adding it to the data node data array
59
-     *
60
-     * @return bool
61
-     */
62
-    public function isInitialized();
56
+	/**
57
+	 * true if the data node has been initialized,
58
+	 * which entails retrieving the required data and adding it to the data node data array
59
+	 *
60
+	 * @return bool
61
+	 */
62
+	public function isInitialized();
63 63
 
64 64
 
65
-    /**
66
-     * true if the data node has NOT been initialized
67
-     *
68
-     * @return bool
69
-     */
70
-    public function isNotInitialized();
65
+	/**
66
+	 * true if the data node has NOT been initialized
67
+	 *
68
+	 * @return bool
69
+	 */
70
+	public function isNotInitialized();
71 71
 
72 72
 
73
-    /**
74
-     * @return string
75
-     * @since $VID:$
76
-     */
77
-    public function nodeName();
73
+	/**
74
+	 * @return string
75
+	 * @since $VID:$
76
+	 */
77
+	public function nodeName();
78 78
 }
Please login to merge, or discard this patch.
core/services/json/JsonConfig.php 1 patch
Indentation   +187 added lines, -187 removed lines patch added patch discarded remove patch
@@ -16,191 +16,191 @@
 block discarded – undo
16 16
  */
17 17
 abstract class JsonConfig
18 18
 {
19
-    /**
20
-     * @var boolean $has_changes
21
-     */
22
-    private $has_changes = false;
23
-
24
-    /**
25
-     * @var string $option_name
26
-     */
27
-    private $option_name;
28
-
29
-
30
-    /**
31
-     * SettingsConfig constructor.
32
-     *
33
-     * @param array $defaults
34
-     */
35
-    public function __construct(array $defaults)
36
-    {
37
-        $this->setOptionName();
38
-        $this->load($defaults);
39
-        $this->clearChanges();
40
-    }
41
-
42
-
43
-    /**
44
-     * @return array
45
-     */
46
-    abstract protected function getProperties();
47
-
48
-
49
-    /**
50
-     * converts property name to:
51
-     *      camelCase for getters ex: show_expired => showExpired
52
-     *      PascalCase for setters ex: show_expired => ShowExpired
53
-     *
54
-     * @param string $string
55
-     * @param false  $camelCase
56
-     * @return string|string[]
57
-     * @since   $VID:$
58
-     */
59
-    private function convertCase($string, $camelCase = false)
60
-    {
61
-        $string = str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
62
-        if ($camelCase) {
63
-            $string = lcfirst($string);
64
-        }
65
-        return $string;
66
-    }
67
-
68
-
69
-    /**
70
-     * @param string $property
71
-     * @param bool   $getter
72
-     * @return string
73
-     */
74
-    private function createGetterSetter($property, $getter = true)
75
-    {
76
-        $setterOrGetter = $this->convertCase($property, $getter);
77
-        // if not a getter, prepend with "set". ex: Show_expired => setShowExpired
78
-        $setterOrGetter = ! $getter ? 'set' . $setterOrGetter : $setterOrGetter;
79
-        return $this->isValidMethod($setterOrGetter) ? $setterOrGetter : '';
80
-    }
81
-
82
-
83
-    /**
84
-     * @param string $method
85
-     * @return bool
86
-     * @throws DomainException
87
-     */
88
-    private function isValidMethod($method)
89
-    {
90
-        if (method_exists($this, $method)) {
91
-            return true;
92
-        }
93
-        throw new DomainException(
94
-            sprintf(
95
-                esc_html__('Missing %1$s method on JsonConfig class %2$s.', 'event_espresso'),
96
-                $method,
97
-                get_class($this)
98
-            )
99
-        );
100
-    }
101
-
102
-
103
-    /**
104
-     * converts class name to option name by changing backslashes to dashes
105
-     */
106
-    private function setOptionName()
107
-    {
108
-        $this->option_name = str_replace(['EventEspresso', '\\'], ['ee', '-'], get_class($this));
109
-    }
110
-
111
-
112
-    /**
113
-     * retrieves WP option for class, decodes the data, and resigns values to properties
114
-     *
115
-     * @param array $defaults
116
-     */
117
-    protected function load(array $defaults)
118
-    {
119
-        $config = get_option($this->option_name, '{}');
120
-        $config = (array) json_decode($config) + $defaults;
121
-        foreach ($this->getProperties() as $property => $value) {
122
-            if ($property === 'option_name') {
123
-                continue;
124
-            }
125
-            // convert to PascalCase and prepend with "set". ex: show_expired => setShowExpired
126
-            $setter = $this->createGetterSetter($property, false);
127
-            $value  = array_key_exists($property, $config) ? $config[ $property ] : null;
128
-            $this->{$setter}($value);
129
-        }
130
-    }
131
-
132
-
133
-    /**
134
-     * updates property value and marks changes if property value has changed
135
-     *
136
-     * @param string $property
137
-     * @param mixed  $value
138
-     */
139
-    protected function setProperty($property, $value)
140
-    {
141
-        $this->markChanges($this->{$property} === $value);
142
-        $this->{$property} = $value;
143
-    }
144
-
145
-
146
-    /**
147
-     * will only toggle has_changes to true otherwise keeps existing value (ie: will never toggle to false)
148
-     * why? this allows this method to be fed with the result of a conditional
149
-     * that compares an incoming value in a setter with it's previously set value.
150
-     * ie: if $x = 1 and you call setX(1) then the value has not really changed.
151
-     *
152
-     * @param bool $changes
153
-     * @since   $VID:$
154
-     */
155
-    protected function markChanges($changes = true)
156
-    {
157
-        $this->has_changes = filter_var($changes, FILTER_VALIDATE_BOOLEAN) ? true : $this->has_changes;
158
-    }
159
-
160
-
161
-    /**
162
-     * resets $has_changes flag to false but does NOT actually reset any data
163
-     */
164
-    public function clearChanges()
165
-    {
166
-        $this->has_changes = false;
167
-    }
168
-
169
-
170
-    /**
171
-     * flag for marking that changes have been made to property data
172
-     *
173
-     * @return bool
174
-     */
175
-    public function hasChanges()
176
-    {
177
-        return $this->has_changes;
178
-    }
179
-
180
-
181
-    /**
182
-     * encodes all property data to JSON and saves it to a WP option
183
-     */
184
-    public function update()
185
-    {
186
-        $config_exists = get_option($this->option_name);
187
-        if ($config_exists && ! $this->has_changes) {
188
-            return;
189
-        }
190
-        $config = [];
191
-        foreach ($this->getProperties() as $property => $value) {
192
-            if ($property === 'option_name') {
193
-                continue;
194
-            }
195
-            $getter = $this->createGetterSetter($property);
196
-            $config[ $property ] = $this->{$getter}();
197
-        }
198
-        $config = wp_json_encode($config);
199
-        if ($config_exists) {
200
-            update_option($this->option_name, $config);
201
-        } else {
202
-            add_option($this->option_name, $config, '', 'no');
203
-        }
204
-        $this->clearChanges();
205
-    }
19
+	/**
20
+	 * @var boolean $has_changes
21
+	 */
22
+	private $has_changes = false;
23
+
24
+	/**
25
+	 * @var string $option_name
26
+	 */
27
+	private $option_name;
28
+
29
+
30
+	/**
31
+	 * SettingsConfig constructor.
32
+	 *
33
+	 * @param array $defaults
34
+	 */
35
+	public function __construct(array $defaults)
36
+	{
37
+		$this->setOptionName();
38
+		$this->load($defaults);
39
+		$this->clearChanges();
40
+	}
41
+
42
+
43
+	/**
44
+	 * @return array
45
+	 */
46
+	abstract protected function getProperties();
47
+
48
+
49
+	/**
50
+	 * converts property name to:
51
+	 *      camelCase for getters ex: show_expired => showExpired
52
+	 *      PascalCase for setters ex: show_expired => ShowExpired
53
+	 *
54
+	 * @param string $string
55
+	 * @param false  $camelCase
56
+	 * @return string|string[]
57
+	 * @since   $VID:$
58
+	 */
59
+	private function convertCase($string, $camelCase = false)
60
+	{
61
+		$string = str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
62
+		if ($camelCase) {
63
+			$string = lcfirst($string);
64
+		}
65
+		return $string;
66
+	}
67
+
68
+
69
+	/**
70
+	 * @param string $property
71
+	 * @param bool   $getter
72
+	 * @return string
73
+	 */
74
+	private function createGetterSetter($property, $getter = true)
75
+	{
76
+		$setterOrGetter = $this->convertCase($property, $getter);
77
+		// if not a getter, prepend with "set". ex: Show_expired => setShowExpired
78
+		$setterOrGetter = ! $getter ? 'set' . $setterOrGetter : $setterOrGetter;
79
+		return $this->isValidMethod($setterOrGetter) ? $setterOrGetter : '';
80
+	}
81
+
82
+
83
+	/**
84
+	 * @param string $method
85
+	 * @return bool
86
+	 * @throws DomainException
87
+	 */
88
+	private function isValidMethod($method)
89
+	{
90
+		if (method_exists($this, $method)) {
91
+			return true;
92
+		}
93
+		throw new DomainException(
94
+			sprintf(
95
+				esc_html__('Missing %1$s method on JsonConfig class %2$s.', 'event_espresso'),
96
+				$method,
97
+				get_class($this)
98
+			)
99
+		);
100
+	}
101
+
102
+
103
+	/**
104
+	 * converts class name to option name by changing backslashes to dashes
105
+	 */
106
+	private function setOptionName()
107
+	{
108
+		$this->option_name = str_replace(['EventEspresso', '\\'], ['ee', '-'], get_class($this));
109
+	}
110
+
111
+
112
+	/**
113
+	 * retrieves WP option for class, decodes the data, and resigns values to properties
114
+	 *
115
+	 * @param array $defaults
116
+	 */
117
+	protected function load(array $defaults)
118
+	{
119
+		$config = get_option($this->option_name, '{}');
120
+		$config = (array) json_decode($config) + $defaults;
121
+		foreach ($this->getProperties() as $property => $value) {
122
+			if ($property === 'option_name') {
123
+				continue;
124
+			}
125
+			// convert to PascalCase and prepend with "set". ex: show_expired => setShowExpired
126
+			$setter = $this->createGetterSetter($property, false);
127
+			$value  = array_key_exists($property, $config) ? $config[ $property ] : null;
128
+			$this->{$setter}($value);
129
+		}
130
+	}
131
+
132
+
133
+	/**
134
+	 * updates property value and marks changes if property value has changed
135
+	 *
136
+	 * @param string $property
137
+	 * @param mixed  $value
138
+	 */
139
+	protected function setProperty($property, $value)
140
+	{
141
+		$this->markChanges($this->{$property} === $value);
142
+		$this->{$property} = $value;
143
+	}
144
+
145
+
146
+	/**
147
+	 * will only toggle has_changes to true otherwise keeps existing value (ie: will never toggle to false)
148
+	 * why? this allows this method to be fed with the result of a conditional
149
+	 * that compares an incoming value in a setter with it's previously set value.
150
+	 * ie: if $x = 1 and you call setX(1) then the value has not really changed.
151
+	 *
152
+	 * @param bool $changes
153
+	 * @since   $VID:$
154
+	 */
155
+	protected function markChanges($changes = true)
156
+	{
157
+		$this->has_changes = filter_var($changes, FILTER_VALIDATE_BOOLEAN) ? true : $this->has_changes;
158
+	}
159
+
160
+
161
+	/**
162
+	 * resets $has_changes flag to false but does NOT actually reset any data
163
+	 */
164
+	public function clearChanges()
165
+	{
166
+		$this->has_changes = false;
167
+	}
168
+
169
+
170
+	/**
171
+	 * flag for marking that changes have been made to property data
172
+	 *
173
+	 * @return bool
174
+	 */
175
+	public function hasChanges()
176
+	{
177
+		return $this->has_changes;
178
+	}
179
+
180
+
181
+	/**
182
+	 * encodes all property data to JSON and saves it to a WP option
183
+	 */
184
+	public function update()
185
+	{
186
+		$config_exists = get_option($this->option_name);
187
+		if ($config_exists && ! $this->has_changes) {
188
+			return;
189
+		}
190
+		$config = [];
191
+		foreach ($this->getProperties() as $property => $value) {
192
+			if ($property === 'option_name') {
193
+				continue;
194
+			}
195
+			$getter = $this->createGetterSetter($property);
196
+			$config[ $property ] = $this->{$getter}();
197
+		}
198
+		$config = wp_json_encode($config);
199
+		if ($config_exists) {
200
+			update_option($this->option_name, $config);
201
+		} else {
202
+			add_option($this->option_name, $config, '', 'no');
203
+		}
204
+		$this->clearChanges();
205
+	}
206 206
 }
Please login to merge, or discard this patch.
core/services/converters/json/ModelObjectToJsonConverterInterface.php 1 patch
Indentation   +46 added lines, -46 removed lines patch added patch discarded remove patch
@@ -12,50 +12,50 @@
 block discarded – undo
12 12
  */
13 13
 interface ModelObjectToJsonConverterInterface
14 14
 {
15
-    /**
16
-     * @param array $entities
17
-     * @return array
18
-     * @since $VID:$
19
-     */
20
-    public function convertAndEncodeArrayOf(array $entities);
21
-
22
-
23
-    /**
24
-     * @param $entity
25
-     * @return false|string
26
-     * @since $VID:$
27
-     */
28
-    public function convertAndEncode($entity);
29
-
30
-
31
-    /**
32
-     * @param array $entities
33
-     * @return array
34
-     * @since $VID:$
35
-     */
36
-    public function convertArrayOf(array $entities);
37
-
38
-
39
-    /**
40
-     * @param $entity
41
-     * @return array
42
-     * @since $VID:$
43
-     */
44
-    public function convert($entity);
45
-
46
-
47
-    /**
48
-     * @param array $entities
49
-     * @return array
50
-     * @since $VID:$
51
-     */
52
-    public function encodeArrayOf(array $entities);
53
-
54
-
55
-    /**
56
-     * @param array $entity_array
57
-     * @return false|string
58
-     * @since $VID:$
59
-     */
60
-    public function encode(array $entity_array);
15
+	/**
16
+	 * @param array $entities
17
+	 * @return array
18
+	 * @since $VID:$
19
+	 */
20
+	public function convertAndEncodeArrayOf(array $entities);
21
+
22
+
23
+	/**
24
+	 * @param $entity
25
+	 * @return false|string
26
+	 * @since $VID:$
27
+	 */
28
+	public function convertAndEncode($entity);
29
+
30
+
31
+	/**
32
+	 * @param array $entities
33
+	 * @return array
34
+	 * @since $VID:$
35
+	 */
36
+	public function convertArrayOf(array $entities);
37
+
38
+
39
+	/**
40
+	 * @param $entity
41
+	 * @return array
42
+	 * @since $VID:$
43
+	 */
44
+	public function convert($entity);
45
+
46
+
47
+	/**
48
+	 * @param array $entities
49
+	 * @return array
50
+	 * @since $VID:$
51
+	 */
52
+	public function encodeArrayOf(array $entities);
53
+
54
+
55
+	/**
56
+	 * @param array $entity_array
57
+	 * @return false|string
58
+	 * @since $VID:$
59
+	 */
60
+	public function encode(array $entity_array);
61 61
 }
Please login to merge, or discard this patch.
core/helpers/EEH_Array.helper.php 1 patch
Indentation   +290 added lines, -290 removed lines patch added patch discarded remove patch
@@ -11,313 +11,313 @@
 block discarded – undo
11 11
  */
12 12
 class EEH_Array extends EEH_Base
13 13
 {
14
-    /**
15
-     * This method basically works the same as the PHP core function array_diff except it allows you to compare arrays
16
-     * of EE_Base_Class objects NOTE: This will ONLY work on an array of EE_Base_Class objects
17
-     *
18
-     * @uses array_udiff core php function for setting up our own array comparison
19
-     * @uses self::_compare_objects as the custom method for array_udiff
20
-     * @param  array $array1 an array of objects
21
-     * @param  array $array2 an array of objects
22
-     * @return array         an array of objects found in array 1 that aren't found in array 2.
23
-     */
24
-    public static function object_array_diff($array1, $array2)
25
-    {
26
-        return array_udiff($array1, $array2, array('self', '_compare_objects'));
27
-    }
14
+	/**
15
+	 * This method basically works the same as the PHP core function array_diff except it allows you to compare arrays
16
+	 * of EE_Base_Class objects NOTE: This will ONLY work on an array of EE_Base_Class objects
17
+	 *
18
+	 * @uses array_udiff core php function for setting up our own array comparison
19
+	 * @uses self::_compare_objects as the custom method for array_udiff
20
+	 * @param  array $array1 an array of objects
21
+	 * @param  array $array2 an array of objects
22
+	 * @return array         an array of objects found in array 1 that aren't found in array 2.
23
+	 */
24
+	public static function object_array_diff($array1, $array2)
25
+	{
26
+		return array_udiff($array1, $array2, array('self', '_compare_objects'));
27
+	}
28 28
 
29
-    /**
30
-     * Given that $arr is an array, determines if it's associative or numerically AND sequentially indexed
31
-     *
32
-     * @param array $array
33
-     * @return boolean
34
-     */
35
-    public static function is_associative_array(array $array): bool
36
-    {
37
-        return ! empty($array) && array_keys($array) !== range(0, count($array) - 1);
38
-    }
29
+	/**
30
+	 * Given that $arr is an array, determines if it's associative or numerically AND sequentially indexed
31
+	 *
32
+	 * @param array $array
33
+	 * @return boolean
34
+	 */
35
+	public static function is_associative_array(array $array): bool
36
+	{
37
+		return ! empty($array) && array_keys($array) !== range(0, count($array) - 1);
38
+	}
39 39
 
40
-    /**
41
-     * Gets an item from the array and leave the array intact. Use in place of end()
42
-     * when you don't want to change the array
43
-     *
44
-     * @param array $arr
45
-     * @return mixed what ever is in the array
46
-     */
47
-    public static function get_one_item_from_array($arr)
48
-    {
49
-        $item = end($arr);
50
-        reset($arr);
51
-        return $item;
52
-    }
40
+	/**
41
+	 * Gets an item from the array and leave the array intact. Use in place of end()
42
+	 * when you don't want to change the array
43
+	 *
44
+	 * @param array $arr
45
+	 * @return mixed what ever is in the array
46
+	 */
47
+	public static function get_one_item_from_array($arr)
48
+	{
49
+		$item = end($arr);
50
+		reset($arr);
51
+		return $item;
52
+	}
53 53
 
54
-    /**
55
-     * Detects if this is a multi-dimensional array
56
-     * meaning that at least one top-level value is an array. Eg [ [], ...]
57
-     *
58
-     * @param mixed $arr
59
-     * @return boolean
60
-     */
61
-    public static function is_multi_dimensional_array($arr)
62
-    {
63
-        if (is_array($arr)) {
64
-            foreach ($arr as $item) {
65
-                if (is_array($item)) {
66
-                    return true; // yep, there's at least 2 levels to this array
67
-                }
68
-            }
69
-        }
70
-        return false; // there's only 1 level, or it's not an array at all!
71
-    }
54
+	/**
55
+	 * Detects if this is a multi-dimensional array
56
+	 * meaning that at least one top-level value is an array. Eg [ [], ...]
57
+	 *
58
+	 * @param mixed $arr
59
+	 * @return boolean
60
+	 */
61
+	public static function is_multi_dimensional_array($arr)
62
+	{
63
+		if (is_array($arr)) {
64
+			foreach ($arr as $item) {
65
+				if (is_array($item)) {
66
+					return true; // yep, there's at least 2 levels to this array
67
+				}
68
+			}
69
+		}
70
+		return false; // there's only 1 level, or it's not an array at all!
71
+	}
72 72
 
73
-    /**
74
-     * Shorthand for isset( $arr[ $index ] ) ? $arr[ $index ] : $default
75
-     *
76
-     * @param array $arr
77
-     * @param mixed $index
78
-     * @param mixed $default
79
-     * @return mixed
80
-     */
81
-    public static function is_set($arr, $index, $default)
82
-    {
83
-        return isset($arr[ $index ]) ? $arr[ $index ] : $default;
84
-    }
73
+	/**
74
+	 * Shorthand for isset( $arr[ $index ] ) ? $arr[ $index ] : $default
75
+	 *
76
+	 * @param array $arr
77
+	 * @param mixed $index
78
+	 * @param mixed $default
79
+	 * @return mixed
80
+	 */
81
+	public static function is_set($arr, $index, $default)
82
+	{
83
+		return isset($arr[ $index ]) ? $arr[ $index ] : $default;
84
+	}
85 85
 
86
-    /**
87
-     * Exactly like `maybe_unserialize`, but also accounts for a WP bug: http://core.trac.wordpress.org/ticket/26118
88
-     *
89
-     * @param mixed $value usually a string, but could be an array or object
90
-     * @return mixed the UN-serialized data
91
-     */
92
-    public static function maybe_unserialize($value)
93
-    {
94
-        $data = maybe_unserialize($value);
95
-        // it's possible that this still has serialized data if it's the session.
96
-        //  WP has a bug, http://core.trac.wordpress.org/ticket/26118 that doesn't unserialize this automatically.
97
-        $token = 'C';
98
-        $data = is_string($data) ? trim($data) : $data;
99
-        if (is_string($data) && strlen($data) > 1 && $data[0] == $token && preg_match("/^{$token}:[0-9]+:/s", $data)) {
100
-            return unserialize($data);
101
-        } else {
102
-            return $data;
103
-        }
104
-    }
86
+	/**
87
+	 * Exactly like `maybe_unserialize`, but also accounts for a WP bug: http://core.trac.wordpress.org/ticket/26118
88
+	 *
89
+	 * @param mixed $value usually a string, but could be an array or object
90
+	 * @return mixed the UN-serialized data
91
+	 */
92
+	public static function maybe_unserialize($value)
93
+	{
94
+		$data = maybe_unserialize($value);
95
+		// it's possible that this still has serialized data if it's the session.
96
+		//  WP has a bug, http://core.trac.wordpress.org/ticket/26118 that doesn't unserialize this automatically.
97
+		$token = 'C';
98
+		$data = is_string($data) ? trim($data) : $data;
99
+		if (is_string($data) && strlen($data) > 1 && $data[0] == $token && preg_match("/^{$token}:[0-9]+:/s", $data)) {
100
+			return unserialize($data);
101
+		} else {
102
+			return $data;
103
+		}
104
+	}
105 105
 
106 106
 
107
-    /**
108
-     * insert_into_array
109
-     *
110
-     * @param array        $target_array the array to insert new data into
111
-     * @param array        $array_to_insert the new data to be inserted
112
-     * @param int|string|null $offset a known key within $target_array where new data will be inserted
113
-     * @param bool         $add_before whether to add new data before or after the offset key
114
-     * @param bool         $preserve_keys whether or not to reset numerically indexed arrays
115
-     * @return array
116
-     */
117
-    public static function insert_into_array(
118
-        array $target_array = array(),
119
-        array $array_to_insert = array(),
120
-        $offset = null,
121
-        bool $add_before = true,
122
-        bool $preserve_keys = true
123
-    ) {
124
-        $target_array_keys = array_keys($target_array);
125
-        // if no offset key was supplied
126
-        if (empty($offset)) {
127
-            // use start or end of $target_array based on whether we are adding before or not
128
-            $offset = $add_before ? 0 : count($target_array);
129
-        }
130
-        // if offset key is a string, then find the corresponding numeric location for that element
131
-        $offset = is_int($offset) ? $offset : array_search($offset, $target_array_keys, true);
132
-        // add one to the offset if adding after
133
-        $offset = $add_before ? $offset : $offset + 1;
134
-        // but ensure offset does not exceed the length of the array
135
-        $offset = $offset > count($target_array) ? count($target_array) : $offset;
136
-        // reindex array ???
137
-        if ($preserve_keys) {
138
-            // take a slice of the target array from the beginning till the offset,
139
-            // then add the new data
140
-            // then add another slice that starts at the offset and goes till the end
141
-            return array_slice($target_array, 0, $offset, true) + $array_to_insert + array_slice(
142
-                $target_array,
143
-                $offset,
144
-                null,
145
-                true
146
-            );
147
-        } else {
148
-            // since we don't want to preserve keys, we can use array_splice
149
-            array_splice($target_array, $offset, 0, $array_to_insert);
150
-            return $target_array;
151
-        }
152
-    }
107
+	/**
108
+	 * insert_into_array
109
+	 *
110
+	 * @param array        $target_array the array to insert new data into
111
+	 * @param array        $array_to_insert the new data to be inserted
112
+	 * @param int|string|null $offset a known key within $target_array where new data will be inserted
113
+	 * @param bool         $add_before whether to add new data before or after the offset key
114
+	 * @param bool         $preserve_keys whether or not to reset numerically indexed arrays
115
+	 * @return array
116
+	 */
117
+	public static function insert_into_array(
118
+		array $target_array = array(),
119
+		array $array_to_insert = array(),
120
+		$offset = null,
121
+		bool $add_before = true,
122
+		bool $preserve_keys = true
123
+	) {
124
+		$target_array_keys = array_keys($target_array);
125
+		// if no offset key was supplied
126
+		if (empty($offset)) {
127
+			// use start or end of $target_array based on whether we are adding before or not
128
+			$offset = $add_before ? 0 : count($target_array);
129
+		}
130
+		// if offset key is a string, then find the corresponding numeric location for that element
131
+		$offset = is_int($offset) ? $offset : array_search($offset, $target_array_keys, true);
132
+		// add one to the offset if adding after
133
+		$offset = $add_before ? $offset : $offset + 1;
134
+		// but ensure offset does not exceed the length of the array
135
+		$offset = $offset > count($target_array) ? count($target_array) : $offset;
136
+		// reindex array ???
137
+		if ($preserve_keys) {
138
+			// take a slice of the target array from the beginning till the offset,
139
+			// then add the new data
140
+			// then add another slice that starts at the offset and goes till the end
141
+			return array_slice($target_array, 0, $offset, true) + $array_to_insert + array_slice(
142
+				$target_array,
143
+				$offset,
144
+				null,
145
+				true
146
+			);
147
+		} else {
148
+			// since we don't want to preserve keys, we can use array_splice
149
+			array_splice($target_array, $offset, 0, $array_to_insert);
150
+			return $target_array;
151
+		}
152
+	}
153 153
 
154 154
 
155
-    /**
156
-     * array_merge() is slow and should never be used while looping over data
157
-     * if you don't need to preserve keys from all arrays, then using a foreach loop is much faster
158
-     * so really this acts more like array_replace( $array1, $array2 )
159
-     * or a union with the arrays flipped ( $array2 + $array1 )
160
-     * this saves a few lines of code and improves readability
161
-     *
162
-     * @param array $array1
163
-     * @param array $array2
164
-     * @return array
165
-     */
166
-    public static function merge_arrays_and_overwrite_keys(array $array1, array $array2)
167
-    {
168
-        foreach ($array2 as $key => $value) {
169
-            $array1[ $key ] = $value;
170
-        }
171
-        return $array1;
172
-    }
155
+	/**
156
+	 * array_merge() is slow and should never be used while looping over data
157
+	 * if you don't need to preserve keys from all arrays, then using a foreach loop is much faster
158
+	 * so really this acts more like array_replace( $array1, $array2 )
159
+	 * or a union with the arrays flipped ( $array2 + $array1 )
160
+	 * this saves a few lines of code and improves readability
161
+	 *
162
+	 * @param array $array1
163
+	 * @param array $array2
164
+	 * @return array
165
+	 */
166
+	public static function merge_arrays_and_overwrite_keys(array $array1, array $array2)
167
+	{
168
+		foreach ($array2 as $key => $value) {
169
+			$array1[ $key ] = $value;
170
+		}
171
+		return $array1;
172
+	}
173 173
 
174 174
 
175
-    /**
176
-     * given a flat array like $array = array('A', 'B', 'C')
177
-     * will convert into a multidimensional array like $array[A][B][C]
178
-     * if $final_value is provided and is anything other than null,
179
-     * then that will be set as the value for the innermost array key
180
-     * like so: $array[A][B][C] = $final_value
181
-     *
182
-     * @param array $flat_array
183
-     * @param mixed $final_value
184
-     * @return array
185
-     */
186
-    public static function convert_array_values_to_keys(array $flat_array, $final_value = null)
187
-    {
188
-        $multidimensional = array();
189
-        $reference = &$multidimensional;
190
-        foreach ($flat_array as $key) {
191
-            $reference[ $key ] = array();
192
-            $reference = &$reference[ $key ];
193
-        }
194
-        if ($final_value !== null) {
195
-            $reference = $final_value;
196
-        }
197
-        return $multidimensional;
198
-    }
175
+	/**
176
+	 * given a flat array like $array = array('A', 'B', 'C')
177
+	 * will convert into a multidimensional array like $array[A][B][C]
178
+	 * if $final_value is provided and is anything other than null,
179
+	 * then that will be set as the value for the innermost array key
180
+	 * like so: $array[A][B][C] = $final_value
181
+	 *
182
+	 * @param array $flat_array
183
+	 * @param mixed $final_value
184
+	 * @return array
185
+	 */
186
+	public static function convert_array_values_to_keys(array $flat_array, $final_value = null)
187
+	{
188
+		$multidimensional = array();
189
+		$reference = &$multidimensional;
190
+		foreach ($flat_array as $key) {
191
+			$reference[ $key ] = array();
192
+			$reference = &$reference[ $key ];
193
+		}
194
+		if ($final_value !== null) {
195
+			$reference = $final_value;
196
+		}
197
+		return $multidimensional;
198
+	}
199 199
 
200 200
 
201
-    /**
202
-     * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential
203
-     * @param array $array
204
-     * @return bool
205
-     */
206
-    public static function is_array_numerically_and_sequentially_indexed(array $array)
207
-    {
208
-        return empty($array) || array_keys($array) === range(0, count($array) - 1);
209
-    }
201
+	/**
202
+	 * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential
203
+	 * @param array $array
204
+	 * @return bool
205
+	 */
206
+	public static function is_array_numerically_and_sequentially_indexed(array $array)
207
+	{
208
+		return empty($array) || array_keys($array) === range(0, count($array) - 1);
209
+	}
210 210
 
211 211
 
212
-    /**
213
-     * recursively walks through an array and adds slashes to all no array elements
214
-     *
215
-     * @param mixed $element
216
-     * @return array|string
217
-     * @since   4.10.29.p
218
-     */
219
-    public static function addSlashesRecursively($element)
220
-    {
221
-        if (is_array($element)) {
222
-            foreach ($element as $key => $value) {
223
-                $element[ $key ] = EEH_Array::addSlashesRecursively($value);
224
-            }
225
-            return $element;
226
-        }
227
-        return is_string($element) ? addslashes($element) : $element;
228
-    }
212
+	/**
213
+	 * recursively walks through an array and adds slashes to all no array elements
214
+	 *
215
+	 * @param mixed $element
216
+	 * @return array|string
217
+	 * @since   4.10.29.p
218
+	 */
219
+	public static function addSlashesRecursively($element)
220
+	{
221
+		if (is_array($element)) {
222
+			foreach ($element as $key => $value) {
223
+				$element[ $key ] = EEH_Array::addSlashesRecursively($value);
224
+			}
225
+			return $element;
226
+		}
227
+		return is_string($element) ? addslashes($element) : $element;
228
+	}
229 229
 
230 230
 
231
-    /**
232
-     * link https://stackoverflow.com/a/3877494
233
-     *
234
-     * @param array $array_1
235
-     * @param array $array_2
236
-     * @return array
237
-     * @since   $VID:$
238
-     */
239
-    public static function array_diff_recursive(array $array_1, array $array_2): array
240
-    {
241
-        $diff = [];
242
-        foreach ($array_1 as $key => $value) {
243
-            if (array_key_exists($key, $array_2)) {
244
-                if (is_array($value)) {
245
-                    $inner_diff = EEH_Array::array_diff_recursive($value, $array_2[ $key ]);
246
-                    if (count($inner_diff)) {
247
-                        $diff[ $key ] = $inner_diff;
248
-                    }
249
-                } else {
250
-                    if ($value != $array_2[ $key ]) {
251
-                        $diff[ $key ] = $value;
252
-                    }
253
-                }
254
-            } else {
255
-                $diff[ $key ] = $value;
256
-            }
257
-        }
258
-        return $diff;
259
-    }
231
+	/**
232
+	 * link https://stackoverflow.com/a/3877494
233
+	 *
234
+	 * @param array $array_1
235
+	 * @param array $array_2
236
+	 * @return array
237
+	 * @since   $VID:$
238
+	 */
239
+	public static function array_diff_recursive(array $array_1, array $array_2): array
240
+	{
241
+		$diff = [];
242
+		foreach ($array_1 as $key => $value) {
243
+			if (array_key_exists($key, $array_2)) {
244
+				if (is_array($value)) {
245
+					$inner_diff = EEH_Array::array_diff_recursive($value, $array_2[ $key ]);
246
+					if (count($inner_diff)) {
247
+						$diff[ $key ] = $inner_diff;
248
+					}
249
+				} else {
250
+					if ($value != $array_2[ $key ]) {
251
+						$diff[ $key ] = $value;
252
+					}
253
+				}
254
+			} else {
255
+				$diff[ $key ] = $value;
256
+			}
257
+		}
258
+		return $diff;
259
+	}
260 260
 
261 261
 
262
-    /**
263
-     * converts multidimensional arrays into a single depth associative array
264
-     * or converts arrays of any depth into a readable string representation
265
-     *
266
-     *  $example = [
267
-     *      'a' => 'A',
268
-     *      'b' => 'B',
269
-     *      'c' => [
270
-     *          'd' => 'D',
271
-     *          'e' => 'E',
272
-     *          'f' => [ 'G', 'H', 'I' ],
273
-     *      ],
274
-     *      [ 'J', 'K' ],
275
-     *      'L',
276
-     *      'M',
277
-     *      'n' => [
278
-     *          'o' => 'P'
279
-     *      ],
280
-     *  ];
281
-     *
282
-     *  print_r( EEH_Array::flattenArray($example) );
283
-     *
284
-     *  Array (
285
-     *      [a] => A
286
-     *      [b] => B
287
-     *      [c] => [ d:D, e:E, f:[ G, H, I ] ]
288
-     *      [0] => [ J, K ]
289
-     *      [1] => L
290
-     *      [2] => M
291
-     *      [n] => [ o:P ]
292
-     *  )
293
-     *
294
-     *  print_r( EEH_Array::flattenArray($example, true) );
295
-     *
296
-     *  "a:A, b:B, c:[ d:D, e:E, f:[ G, H, I ] ], [ J, K ], L, M, n:[ o:P ]"
297
-     *
298
-     * @param array $array      the array to be flattened
299
-     * @param bool  $to_string  [true] will flatten the entire array down into a string
300
-     *                          [false] will only flatten sub-arrays down into strings and return a array
301
-     * @param bool  $top_level  used for formatting purposes only, best to leave this alone as it's set internally
302
-     * @return array|false|string
303
-     * @since $VID:$
304
-     */
305
-    public static function flattenArray(array $array, bool $to_string = false, bool $top_level = true)
306
-    {
307
-        $flat_array = [];
308
-        foreach ($array as $key => $value) {
309
-            $flat_array[ $key ] = is_array($value)
310
-                ? EEH_Array::flattenArray($value, true, false)
311
-                : $value;
312
-        }
313
-        if (! $to_string) {
314
-            return $flat_array;
315
-        }
316
-        $flat = '';
317
-        foreach ($flat_array as $key => $value) {
318
-            $flat .= is_int($key) ? "$value, " : "$key:$value, ";
319
-        }
320
-        $flat = substr($flat, 0, -2);
321
-        return $top_level ? $flat : "[ $flat ]";
322
-    }
262
+	/**
263
+	 * converts multidimensional arrays into a single depth associative array
264
+	 * or converts arrays of any depth into a readable string representation
265
+	 *
266
+	 *  $example = [
267
+	 *      'a' => 'A',
268
+	 *      'b' => 'B',
269
+	 *      'c' => [
270
+	 *          'd' => 'D',
271
+	 *          'e' => 'E',
272
+	 *          'f' => [ 'G', 'H', 'I' ],
273
+	 *      ],
274
+	 *      [ 'J', 'K' ],
275
+	 *      'L',
276
+	 *      'M',
277
+	 *      'n' => [
278
+	 *          'o' => 'P'
279
+	 *      ],
280
+	 *  ];
281
+	 *
282
+	 *  print_r( EEH_Array::flattenArray($example) );
283
+	 *
284
+	 *  Array (
285
+	 *      [a] => A
286
+	 *      [b] => B
287
+	 *      [c] => [ d:D, e:E, f:[ G, H, I ] ]
288
+	 *      [0] => [ J, K ]
289
+	 *      [1] => L
290
+	 *      [2] => M
291
+	 *      [n] => [ o:P ]
292
+	 *  )
293
+	 *
294
+	 *  print_r( EEH_Array::flattenArray($example, true) );
295
+	 *
296
+	 *  "a:A, b:B, c:[ d:D, e:E, f:[ G, H, I ] ], [ J, K ], L, M, n:[ o:P ]"
297
+	 *
298
+	 * @param array $array      the array to be flattened
299
+	 * @param bool  $to_string  [true] will flatten the entire array down into a string
300
+	 *                          [false] will only flatten sub-arrays down into strings and return a array
301
+	 * @param bool  $top_level  used for formatting purposes only, best to leave this alone as it's set internally
302
+	 * @return array|false|string
303
+	 * @since $VID:$
304
+	 */
305
+	public static function flattenArray(array $array, bool $to_string = false, bool $top_level = true)
306
+	{
307
+		$flat_array = [];
308
+		foreach ($array as $key => $value) {
309
+			$flat_array[ $key ] = is_array($value)
310
+				? EEH_Array::flattenArray($value, true, false)
311
+				: $value;
312
+		}
313
+		if (! $to_string) {
314
+			return $flat_array;
315
+		}
316
+		$flat = '';
317
+		foreach ($flat_array as $key => $value) {
318
+			$flat .= is_int($key) ? "$value, " : "$key:$value, ";
319
+		}
320
+		$flat = substr($flat, 0, -2);
321
+		return $top_level ? $flat : "[ $flat ]";
322
+	}
323 323
 }
Please login to merge, or discard this patch.
core/services/json/JsonDataHandler.php 1 patch
Indentation   +342 added lines, -342 removed lines patch added patch discarded remove patch
@@ -14,346 +14,346 @@
 block discarded – undo
14 14
  */
15 15
 class JsonDataHandler
16 16
 {
17
-    const DATA_TYPE_ARRAY     = 'array';
18
-
19
-    const DATA_TYPE_OBJECT    = 'object';
20
-
21
-    const DATA_TYPE_USE_FLAGS = 'flags';
22
-
23
-    const NO_ERROR_MSG        = 'No error';
24
-
25
-    /**
26
-     * @var string|null
27
-     */
28
-    private $data_type = null;
29
-
30
-    /**
31
-     * @var array|stdClass|null
32
-     */
33
-    private $decoded_data = null;
34
-
35
-    /**
36
-     * JSON_BIGINT_AS_STRING,
37
-     * JSON_INVALID_UTF8_IGNORE,
38
-     * JSON_INVALID_UTF8_SUBSTITUTE,
39
-     * JSON_OBJECT_AS_ARRAY,
40
-     * JSON_THROW_ON_ERROR
41
-     *
42
-     * @var int
43
-     */
44
-    private $decode_flags = 0;
45
-
46
-    /**
47
-     * @var int
48
-     */
49
-    private $depth = 512;
50
-
51
-    /**
52
-     * @var string
53
-     */
54
-    private $encoded_data = '';
55
-
56
-    /**
57
-     * JSON_FORCE_OBJECT,
58
-     * JSON_HEX_QUOT,
59
-     * JSON_HEX_TAG,
60
-     * JSON_HEX_AMP,
61
-     * JSON_HEX_APOS,
62
-     * JSON_INVALID_UTF8_IGNORE,
63
-     * JSON_INVALID_UTF8_SUBSTITUTE,
64
-     * JSON_NUMERIC_CHECK,
65
-     * JSON_PARTIAL_OUTPUT_ON_ERROR,
66
-     * JSON_PRESERVE_ZERO_FRACTION,
67
-     * JSON_PRETTY_PRINT,
68
-     * JSON_UNESCAPED_LINE_TERMINATORS,
69
-     * JSON_UNESCAPED_SLASHES,
70
-     * JSON_UNESCAPED_UNICODE,
71
-     * JSON_THROW_ON_ERROR.
72
-     *
73
-     * @var int
74
-     */
75
-    private $encode_flags = 0;
76
-
77
-    /**
78
-     * @var int
79
-     */
80
-    private $last_error_code = JSON_ERROR_NONE;
81
-
82
-    /**
83
-     * @var string
84
-     */
85
-    private $last_error_msg = JsonDataHandler::NO_ERROR_MSG;
86
-
87
-
88
-    /**
89
-     * JsonDataHandler constructor.
90
-     */
91
-    public function __construct()
92
-    {
93
-        if (! defined('JSON_INVALID_UTF8_IGNORE')) {
94
-            define('JSON_INVALID_UTF8_IGNORE', 1048576);
95
-        }
96
-        if (! defined('JSON_INVALID_UTF8_SUBSTITUTE')) {
97
-            define('JSON_INVALID_UTF8_SUBSTITUTE', 2097152);
98
-        }
99
-        if (! defined('JSON_THROW_ON_ERROR')) {
100
-            define('JSON_THROW_ON_ERROR', 4194304);
101
-        }
102
-    }
103
-
104
-
105
-    /**
106
-     * set $data_type, $decode_flags, $encode_flags, and depth all in one shot
107
-     *
108
-     * @param string $data_type
109
-     * @param int    $decode_flags
110
-     * @param int    $encode_flags
111
-     * @param int    $depth
112
-     */
113
-    public function configure(
114
-        string $data_type = JsonDataHandler::DATA_TYPE_USE_FLAGS,
115
-        int $decode_flags = 0,
116
-        int $encode_flags = 0,
117
-        int $depth = 512
118
-    ) {
119
-        $this->setDataType($data_type);
120
-        $this->setDecodeFlags($decode_flags);
121
-        $this->setDepth($depth);
122
-        $this->setEncodeFlags($encode_flags);
123
-    }
124
-
125
-
126
-    /**
127
-     * @param string $data_type
128
-     */
129
-    public function setDataType(string $data_type): void
130
-    {
131
-        $this->data_type = $data_type === JsonDataHandler::DATA_TYPE_ARRAY
132
-                           || $data_type === JsonDataHandler::DATA_TYPE_OBJECT
133
-                           || $data_type === JsonDataHandler::DATA_TYPE_USE_FLAGS
134
-            ? $data_type
135
-            : JsonDataHandler::DATA_TYPE_USE_FLAGS;
136
-    }
137
-
138
-
139
-    /**
140
-     * One or more Bitmask values:
141
-     * JSON_BIGINT_AS_STRING,
142
-     * JSON_INVALID_UTF8_IGNORE,        PHP >= 7.2
143
-     * JSON_INVALID_UTF8_SUBSTITUTE,    PHP >= 7.2
144
-     * JSON_OBJECT_AS_ARRAY,
145
-     * JSON_THROW_ON_ERROR              PHP >= 7.3
146
-     *
147
-     * pass multiple values separated with |
148
-     * ex: JSON_BIGINT_AS_STRING | JSON_INVALID_UTF8_IGNORE | JSON_OBJECT_AS_ARRAY
149
-     *
150
-     * @param int $decode_flags
151
-     */
152
-    public function setDecodeFlags(int $decode_flags): void
153
-    {
154
-        $this->decode_flags = $decode_flags === JSON_BIGINT_AS_STRING
155
-                              || $decode_flags === JSON_OBJECT_AS_ARRAY
156
-                              // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_invalid_utf8_ignoreFound
157
-                              || $decode_flags === JSON_INVALID_UTF8_IGNORE
158
-                              // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_invalid_utf8_substituteFound
159
-                              || $decode_flags === JSON_INVALID_UTF8_SUBSTITUTE
160
-                              // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_throw_on_errorFound
161
-                              || $decode_flags === JSON_THROW_ON_ERROR
162
-            ? $decode_flags
163
-            : 0;
164
-    }
165
-
166
-
167
-    /**
168
-     * @param int $depth
169
-     */
170
-    public function setDepth(int $depth): void
171
-    {
172
-        $depth       = absint($depth);
173
-        $this->depth = $depth ?: 512;
174
-    }
175
-
176
-
177
-    /**
178
-     * One or more Bitmask values:
179
-     * JSON_FORCE_OBJECT,
180
-     * JSON_HEX_QUOT,
181
-     * JSON_HEX_TAG,
182
-     * JSON_HEX_AMP,
183
-     * JSON_HEX_APOS,
184
-     * JSON_INVALID_UTF8_IGNORE,        PHP >= 7.2
185
-     * JSON_INVALID_UTF8_SUBSTITUTE,    PHP >= 7.2
186
-     * JSON_NUMERIC_CHECK,
187
-     * JSON_PARTIAL_OUTPUT_ON_ERROR,
188
-     * JSON_PRESERVE_ZERO_FRACTION,
189
-     * JSON_PRETTY_PRINT,
190
-     * JSON_UNESCAPED_LINE_TERMINATORS,
191
-     * JSON_UNESCAPED_SLASHES,
192
-     * JSON_UNESCAPED_UNICODE,
193
-     * JSON_THROW_ON_ERROR.             PHP >= 7.3
194
-     *
195
-     * pass multiple values separated with |
196
-     * ex: JSON_FORCE_OBJECT | JSON_INVALID_UTF8_IGNORE | JSON_THROW_ON_ERROR
197
-     *
198
-     * @param int $encode_flags
199
-     */
200
-    public function setEncodeFlags(int $encode_flags): void
201
-    {
202
-        $this->encode_flags = $encode_flags === JSON_FORCE_OBJECT
203
-                              || $encode_flags === JSON_HEX_QUOT
204
-                              || $encode_flags === JSON_HEX_TAG
205
-                              || $encode_flags === JSON_HEX_AMP
206
-                              || $encode_flags === JSON_HEX_APOS
207
-                              || $encode_flags === JSON_NUMERIC_CHECK
208
-                              || $encode_flags === JSON_PARTIAL_OUTPUT_ON_ERROR
209
-                              || $encode_flags === JSON_PRESERVE_ZERO_FRACTION
210
-                              || $encode_flags === JSON_PRETTY_PRINT
211
-                              || $encode_flags === JSON_UNESCAPED_LINE_TERMINATORS
212
-                              || $encode_flags === JSON_UNESCAPED_SLASHES
213
-                              || $encode_flags === JSON_UNESCAPED_UNICODE
214
-                              // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_invalid_utf8_ignoreFound
215
-                              || $encode_flags === JSON_INVALID_UTF8_IGNORE
216
-                              // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_invalid_utf8_substituteFound
217
-                              || $encode_flags === JSON_INVALID_UTF8_SUBSTITUTE
218
-                              // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_throw_on_errorFound
219
-                              || $encode_flags === JSON_THROW_ON_ERROR
220
-            ? $encode_flags
221
-            : 0;
222
-    }
223
-
224
-
225
-    /**
226
-     * @return string|null
227
-     */
228
-    public function dataType(): ?string
229
-    {
230
-        return $this->data_type;
231
-    }
232
-
233
-
234
-    /**
235
-     * @return bool|null
236
-     */
237
-    private function asAssociative(): ?bool
238
-    {
239
-        switch ($this->data_type) {
240
-            case JsonDataHandler::DATA_TYPE_ARRAY:
241
-                return true;
242
-            case JsonDataHandler::DATA_TYPE_OBJECT:
243
-                return false;
244
-            case JsonDataHandler::DATA_TYPE_USE_FLAGS:
245
-                return null;
246
-        }
247
-        return null;
248
-    }
249
-
250
-
251
-    /**
252
-     * @param array|string $json
253
-     * @return array|mixed|stdClass
254
-     */
255
-    public function decodeJson($json)
256
-    {
257
-        $this->resetErrors();
258
-        if ($this->isJson($json)) {
259
-            $this->decoded_data    = json_decode($json, $this->asAssociative(), $this->depth, $this->decode_flags);
260
-            $this->last_error_code = json_last_error();
261
-            $this->last_error_msg  = json_last_error_msg();
262
-        } else {
263
-            $this->decoded_data    = $json;
264
-            $this->last_error_code = JSON_ERROR_NONE;
265
-            $this->last_error_msg  = JsonDataHandler::NO_ERROR_MSG;
266
-        }
267
-        return $this->decoded_data;
268
-    }
269
-
270
-
271
-    /**
272
-     * @param $data
273
-     * @return string
274
-     */
275
-    public function encodeData($data): string
276
-    {
277
-        $this->resetErrors();
278
-        if ($this->isJson($data)) {
279
-            $this->encoded_data = $data;
280
-            $this->last_error_code = JSON_ERROR_NONE;
281
-            $this->last_error_msg  = JsonDataHandler::NO_ERROR_MSG;
282
-        } else {
283
-            $this->encoded_data = json_encode($data, $this->encode_flags, $this->depth);
284
-            $this->last_error_code = json_last_error();
285
-            $this->last_error_msg  = json_last_error_msg();
286
-        }
287
-        return $this->encoded_data ?: '{}';
288
-    }
289
-
290
-
291
-    /**
292
-     * @return array|stdClass
293
-     */
294
-    public function getDecodedData()
295
-    {
296
-        return $this->decoded_data;
297
-    }
298
-
299
-
300
-    /**
301
-     * @return string
302
-     */
303
-    public function getEncodedData(): string
304
-    {
305
-        return $this->encoded_data;
306
-    }
307
-
308
-
309
-    /**
310
-     * @param bool $reset
311
-     * @return int
312
-     */
313
-    public function getLastErrorCode(bool $reset = false): int
314
-    {
315
-        $last_error = $this->last_error_code;
316
-        if ($reset) {
317
-            $this->resetErrors();
318
-        }
319
-        return $last_error;
320
-    }
321
-
322
-
323
-    /**
324
-     * @param bool $reset
325
-     * @return string
326
-     */
327
-    public function getLastErrorMessage(bool $reset = false): string
328
-    {
329
-        $last_error = $this->last_error_msg;
330
-        if ($reset) {
331
-            $this->resetErrors();
332
-        }
333
-        return $last_error;
334
-    }
335
-
336
-
337
-    /**
338
-     * @param array|string $maybe_json
339
-     * @return bool
340
-     */
341
-    public function isJson($maybe_json): bool
342
-    {
343
-        if (! is_string($maybe_json)) {
344
-            return false;
345
-        }
346
-        $decoded = json_decode($maybe_json, $this->asAssociative(), $this->depth, $this->decode_flags);
347
-        return json_last_error() === JSON_ERROR_NONE && ! ($decoded === null && ! empty($maybe_json));
348
-    }
349
-
350
-
351
-    /**
352
-     * @since $VID:$
353
-     */
354
-    public function resetErrors()
355
-    {
356
-        $this->last_error_code = JSON_ERROR_NONE;
357
-        $this->last_error_msg  = JsonDataHandler::NO_ERROR_MSG;
358
-    }
17
+	const DATA_TYPE_ARRAY     = 'array';
18
+
19
+	const DATA_TYPE_OBJECT    = 'object';
20
+
21
+	const DATA_TYPE_USE_FLAGS = 'flags';
22
+
23
+	const NO_ERROR_MSG        = 'No error';
24
+
25
+	/**
26
+	 * @var string|null
27
+	 */
28
+	private $data_type = null;
29
+
30
+	/**
31
+	 * @var array|stdClass|null
32
+	 */
33
+	private $decoded_data = null;
34
+
35
+	/**
36
+	 * JSON_BIGINT_AS_STRING,
37
+	 * JSON_INVALID_UTF8_IGNORE,
38
+	 * JSON_INVALID_UTF8_SUBSTITUTE,
39
+	 * JSON_OBJECT_AS_ARRAY,
40
+	 * JSON_THROW_ON_ERROR
41
+	 *
42
+	 * @var int
43
+	 */
44
+	private $decode_flags = 0;
45
+
46
+	/**
47
+	 * @var int
48
+	 */
49
+	private $depth = 512;
50
+
51
+	/**
52
+	 * @var string
53
+	 */
54
+	private $encoded_data = '';
55
+
56
+	/**
57
+	 * JSON_FORCE_OBJECT,
58
+	 * JSON_HEX_QUOT,
59
+	 * JSON_HEX_TAG,
60
+	 * JSON_HEX_AMP,
61
+	 * JSON_HEX_APOS,
62
+	 * JSON_INVALID_UTF8_IGNORE,
63
+	 * JSON_INVALID_UTF8_SUBSTITUTE,
64
+	 * JSON_NUMERIC_CHECK,
65
+	 * JSON_PARTIAL_OUTPUT_ON_ERROR,
66
+	 * JSON_PRESERVE_ZERO_FRACTION,
67
+	 * JSON_PRETTY_PRINT,
68
+	 * JSON_UNESCAPED_LINE_TERMINATORS,
69
+	 * JSON_UNESCAPED_SLASHES,
70
+	 * JSON_UNESCAPED_UNICODE,
71
+	 * JSON_THROW_ON_ERROR.
72
+	 *
73
+	 * @var int
74
+	 */
75
+	private $encode_flags = 0;
76
+
77
+	/**
78
+	 * @var int
79
+	 */
80
+	private $last_error_code = JSON_ERROR_NONE;
81
+
82
+	/**
83
+	 * @var string
84
+	 */
85
+	private $last_error_msg = JsonDataHandler::NO_ERROR_MSG;
86
+
87
+
88
+	/**
89
+	 * JsonDataHandler constructor.
90
+	 */
91
+	public function __construct()
92
+	{
93
+		if (! defined('JSON_INVALID_UTF8_IGNORE')) {
94
+			define('JSON_INVALID_UTF8_IGNORE', 1048576);
95
+		}
96
+		if (! defined('JSON_INVALID_UTF8_SUBSTITUTE')) {
97
+			define('JSON_INVALID_UTF8_SUBSTITUTE', 2097152);
98
+		}
99
+		if (! defined('JSON_THROW_ON_ERROR')) {
100
+			define('JSON_THROW_ON_ERROR', 4194304);
101
+		}
102
+	}
103
+
104
+
105
+	/**
106
+	 * set $data_type, $decode_flags, $encode_flags, and depth all in one shot
107
+	 *
108
+	 * @param string $data_type
109
+	 * @param int    $decode_flags
110
+	 * @param int    $encode_flags
111
+	 * @param int    $depth
112
+	 */
113
+	public function configure(
114
+		string $data_type = JsonDataHandler::DATA_TYPE_USE_FLAGS,
115
+		int $decode_flags = 0,
116
+		int $encode_flags = 0,
117
+		int $depth = 512
118
+	) {
119
+		$this->setDataType($data_type);
120
+		$this->setDecodeFlags($decode_flags);
121
+		$this->setDepth($depth);
122
+		$this->setEncodeFlags($encode_flags);
123
+	}
124
+
125
+
126
+	/**
127
+	 * @param string $data_type
128
+	 */
129
+	public function setDataType(string $data_type): void
130
+	{
131
+		$this->data_type = $data_type === JsonDataHandler::DATA_TYPE_ARRAY
132
+						   || $data_type === JsonDataHandler::DATA_TYPE_OBJECT
133
+						   || $data_type === JsonDataHandler::DATA_TYPE_USE_FLAGS
134
+			? $data_type
135
+			: JsonDataHandler::DATA_TYPE_USE_FLAGS;
136
+	}
137
+
138
+
139
+	/**
140
+	 * One or more Bitmask values:
141
+	 * JSON_BIGINT_AS_STRING,
142
+	 * JSON_INVALID_UTF8_IGNORE,        PHP >= 7.2
143
+	 * JSON_INVALID_UTF8_SUBSTITUTE,    PHP >= 7.2
144
+	 * JSON_OBJECT_AS_ARRAY,
145
+	 * JSON_THROW_ON_ERROR              PHP >= 7.3
146
+	 *
147
+	 * pass multiple values separated with |
148
+	 * ex: JSON_BIGINT_AS_STRING | JSON_INVALID_UTF8_IGNORE | JSON_OBJECT_AS_ARRAY
149
+	 *
150
+	 * @param int $decode_flags
151
+	 */
152
+	public function setDecodeFlags(int $decode_flags): void
153
+	{
154
+		$this->decode_flags = $decode_flags === JSON_BIGINT_AS_STRING
155
+							  || $decode_flags === JSON_OBJECT_AS_ARRAY
156
+							  // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_invalid_utf8_ignoreFound
157
+							  || $decode_flags === JSON_INVALID_UTF8_IGNORE
158
+							  // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_invalid_utf8_substituteFound
159
+							  || $decode_flags === JSON_INVALID_UTF8_SUBSTITUTE
160
+							  // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_throw_on_errorFound
161
+							  || $decode_flags === JSON_THROW_ON_ERROR
162
+			? $decode_flags
163
+			: 0;
164
+	}
165
+
166
+
167
+	/**
168
+	 * @param int $depth
169
+	 */
170
+	public function setDepth(int $depth): void
171
+	{
172
+		$depth       = absint($depth);
173
+		$this->depth = $depth ?: 512;
174
+	}
175
+
176
+
177
+	/**
178
+	 * One or more Bitmask values:
179
+	 * JSON_FORCE_OBJECT,
180
+	 * JSON_HEX_QUOT,
181
+	 * JSON_HEX_TAG,
182
+	 * JSON_HEX_AMP,
183
+	 * JSON_HEX_APOS,
184
+	 * JSON_INVALID_UTF8_IGNORE,        PHP >= 7.2
185
+	 * JSON_INVALID_UTF8_SUBSTITUTE,    PHP >= 7.2
186
+	 * JSON_NUMERIC_CHECK,
187
+	 * JSON_PARTIAL_OUTPUT_ON_ERROR,
188
+	 * JSON_PRESERVE_ZERO_FRACTION,
189
+	 * JSON_PRETTY_PRINT,
190
+	 * JSON_UNESCAPED_LINE_TERMINATORS,
191
+	 * JSON_UNESCAPED_SLASHES,
192
+	 * JSON_UNESCAPED_UNICODE,
193
+	 * JSON_THROW_ON_ERROR.             PHP >= 7.3
194
+	 *
195
+	 * pass multiple values separated with |
196
+	 * ex: JSON_FORCE_OBJECT | JSON_INVALID_UTF8_IGNORE | JSON_THROW_ON_ERROR
197
+	 *
198
+	 * @param int $encode_flags
199
+	 */
200
+	public function setEncodeFlags(int $encode_flags): void
201
+	{
202
+		$this->encode_flags = $encode_flags === JSON_FORCE_OBJECT
203
+							  || $encode_flags === JSON_HEX_QUOT
204
+							  || $encode_flags === JSON_HEX_TAG
205
+							  || $encode_flags === JSON_HEX_AMP
206
+							  || $encode_flags === JSON_HEX_APOS
207
+							  || $encode_flags === JSON_NUMERIC_CHECK
208
+							  || $encode_flags === JSON_PARTIAL_OUTPUT_ON_ERROR
209
+							  || $encode_flags === JSON_PRESERVE_ZERO_FRACTION
210
+							  || $encode_flags === JSON_PRETTY_PRINT
211
+							  || $encode_flags === JSON_UNESCAPED_LINE_TERMINATORS
212
+							  || $encode_flags === JSON_UNESCAPED_SLASHES
213
+							  || $encode_flags === JSON_UNESCAPED_UNICODE
214
+							  // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_invalid_utf8_ignoreFound
215
+							  || $encode_flags === JSON_INVALID_UTF8_IGNORE
216
+							  // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_invalid_utf8_substituteFound
217
+							  || $encode_flags === JSON_INVALID_UTF8_SUBSTITUTE
218
+							  // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_throw_on_errorFound
219
+							  || $encode_flags === JSON_THROW_ON_ERROR
220
+			? $encode_flags
221
+			: 0;
222
+	}
223
+
224
+
225
+	/**
226
+	 * @return string|null
227
+	 */
228
+	public function dataType(): ?string
229
+	{
230
+		return $this->data_type;
231
+	}
232
+
233
+
234
+	/**
235
+	 * @return bool|null
236
+	 */
237
+	private function asAssociative(): ?bool
238
+	{
239
+		switch ($this->data_type) {
240
+			case JsonDataHandler::DATA_TYPE_ARRAY:
241
+				return true;
242
+			case JsonDataHandler::DATA_TYPE_OBJECT:
243
+				return false;
244
+			case JsonDataHandler::DATA_TYPE_USE_FLAGS:
245
+				return null;
246
+		}
247
+		return null;
248
+	}
249
+
250
+
251
+	/**
252
+	 * @param array|string $json
253
+	 * @return array|mixed|stdClass
254
+	 */
255
+	public function decodeJson($json)
256
+	{
257
+		$this->resetErrors();
258
+		if ($this->isJson($json)) {
259
+			$this->decoded_data    = json_decode($json, $this->asAssociative(), $this->depth, $this->decode_flags);
260
+			$this->last_error_code = json_last_error();
261
+			$this->last_error_msg  = json_last_error_msg();
262
+		} else {
263
+			$this->decoded_data    = $json;
264
+			$this->last_error_code = JSON_ERROR_NONE;
265
+			$this->last_error_msg  = JsonDataHandler::NO_ERROR_MSG;
266
+		}
267
+		return $this->decoded_data;
268
+	}
269
+
270
+
271
+	/**
272
+	 * @param $data
273
+	 * @return string
274
+	 */
275
+	public function encodeData($data): string
276
+	{
277
+		$this->resetErrors();
278
+		if ($this->isJson($data)) {
279
+			$this->encoded_data = $data;
280
+			$this->last_error_code = JSON_ERROR_NONE;
281
+			$this->last_error_msg  = JsonDataHandler::NO_ERROR_MSG;
282
+		} else {
283
+			$this->encoded_data = json_encode($data, $this->encode_flags, $this->depth);
284
+			$this->last_error_code = json_last_error();
285
+			$this->last_error_msg  = json_last_error_msg();
286
+		}
287
+		return $this->encoded_data ?: '{}';
288
+	}
289
+
290
+
291
+	/**
292
+	 * @return array|stdClass
293
+	 */
294
+	public function getDecodedData()
295
+	{
296
+		return $this->decoded_data;
297
+	}
298
+
299
+
300
+	/**
301
+	 * @return string
302
+	 */
303
+	public function getEncodedData(): string
304
+	{
305
+		return $this->encoded_data;
306
+	}
307
+
308
+
309
+	/**
310
+	 * @param bool $reset
311
+	 * @return int
312
+	 */
313
+	public function getLastErrorCode(bool $reset = false): int
314
+	{
315
+		$last_error = $this->last_error_code;
316
+		if ($reset) {
317
+			$this->resetErrors();
318
+		}
319
+		return $last_error;
320
+	}
321
+
322
+
323
+	/**
324
+	 * @param bool $reset
325
+	 * @return string
326
+	 */
327
+	public function getLastErrorMessage(bool $reset = false): string
328
+	{
329
+		$last_error = $this->last_error_msg;
330
+		if ($reset) {
331
+			$this->resetErrors();
332
+		}
333
+		return $last_error;
334
+	}
335
+
336
+
337
+	/**
338
+	 * @param array|string $maybe_json
339
+	 * @return bool
340
+	 */
341
+	public function isJson($maybe_json): bool
342
+	{
343
+		if (! is_string($maybe_json)) {
344
+			return false;
345
+		}
346
+		$decoded = json_decode($maybe_json, $this->asAssociative(), $this->depth, $this->decode_flags);
347
+		return json_last_error() === JSON_ERROR_NONE && ! ($decoded === null && ! empty($maybe_json));
348
+	}
349
+
350
+
351
+	/**
352
+	 * @since $VID:$
353
+	 */
354
+	public function resetErrors()
355
+	{
356
+		$this->last_error_code = JSON_ERROR_NONE;
357
+		$this->last_error_msg  = JsonDataHandler::NO_ERROR_MSG;
358
+	}
359 359
 }
Please login to merge, or discard this patch.
core/domain/entities/routing/handlers/admin/WordPressPluginsPage.php 1 patch
Indentation   +68 added lines, -68 removed lines patch added patch discarded remove patch
@@ -17,77 +17,77 @@
 block discarded – undo
17 17
  */
18 18
 class WordPressPluginsPage extends AdminRoute
19 19
 {
20
-    /**
21
-     * returns true if the current request matches this route
22
-     *
23
-     * @return bool
24
-     * @since   $VID:$
25
-     */
26
-    public function matchesCurrentRequest(): bool
27
-    {
28
-        global $pagenow;
29
-        return $pagenow === 'plugins.php' && ($this->request->isAdmin() || $this->request->isAdminAjax());
30
-    }
20
+	/**
21
+	 * returns true if the current request matches this route
22
+	 *
23
+	 * @return bool
24
+	 * @since   $VID:$
25
+	 */
26
+	public function matchesCurrentRequest(): bool
27
+	{
28
+		global $pagenow;
29
+		return $pagenow === 'plugins.php' && ($this->request->isAdmin() || $this->request->isAdminAjax());
30
+	}
31 31
 
32 32
 
33
-    /**
34
-     * @since $VID:$
35
-     */
36
-    protected function registerDependencies()
37
-    {
38
-        $this->dependency_map->registerDependencies(
39
-            'EventEspresso\core\domain\services\assets\WordPressPluginsPageAssetManager',
40
-            [
41
-                'EventEspresso\core\services\assets\AssetCollection' => EE_Dependency_Map::load_from_cache,
42
-                'EE_Currency_Config'                                 => EE_Dependency_Map::load_from_cache,
43
-                'EE_Template_Config'                                 => EE_Dependency_Map::load_from_cache,
44
-                'EventEspresso\core\domain\Domain'                   => EE_Dependency_Map::load_from_cache,
45
-                'EventEspresso\core\services\assets\Registry'        => EE_Dependency_Map::load_from_cache,
46
-            ]
47
-        );
48
-        $this->dependency_map->registerDependencies(
49
-            'EventEspresso\core\domain\entities\routing\data_nodes\domains\WordPressPluginsPageData',
50
-            [
51
-                'EventEspresso\core\domain\services\admin\ExitModal'     => EE_Dependency_Map::load_from_cache,
52
-                'EventEspresso\core\services\json\JsonDataNodeValidator' => EE_Dependency_Map::load_from_cache,
53
-            ]
54
-        );
55
-        $this->dependency_map->registerDependencies(
56
-            'EventEspresso\core\domain\services\admin\ExitModal',
57
-            ['EventEspresso\core\services\assets\Registry' => EE_Dependency_Map::load_from_cache]
58
-        );
59
-        $this->dependency_map->registerDependencies(
60
-            'EventEspresso\core\domain\services\admin\PluginUpsells',
61
-            ['EventEspresso\core\domain\Domain' => EE_Dependency_Map::load_from_cache]
62
-        );
33
+	/**
34
+	 * @since $VID:$
35
+	 */
36
+	protected function registerDependencies()
37
+	{
38
+		$this->dependency_map->registerDependencies(
39
+			'EventEspresso\core\domain\services\assets\WordPressPluginsPageAssetManager',
40
+			[
41
+				'EventEspresso\core\services\assets\AssetCollection' => EE_Dependency_Map::load_from_cache,
42
+				'EE_Currency_Config'                                 => EE_Dependency_Map::load_from_cache,
43
+				'EE_Template_Config'                                 => EE_Dependency_Map::load_from_cache,
44
+				'EventEspresso\core\domain\Domain'                   => EE_Dependency_Map::load_from_cache,
45
+				'EventEspresso\core\services\assets\Registry'        => EE_Dependency_Map::load_from_cache,
46
+			]
47
+		);
48
+		$this->dependency_map->registerDependencies(
49
+			'EventEspresso\core\domain\entities\routing\data_nodes\domains\WordPressPluginsPageData',
50
+			[
51
+				'EventEspresso\core\domain\services\admin\ExitModal'     => EE_Dependency_Map::load_from_cache,
52
+				'EventEspresso\core\services\json\JsonDataNodeValidator' => EE_Dependency_Map::load_from_cache,
53
+			]
54
+		);
55
+		$this->dependency_map->registerDependencies(
56
+			'EventEspresso\core\domain\services\admin\ExitModal',
57
+			['EventEspresso\core\services\assets\Registry' => EE_Dependency_Map::load_from_cache]
58
+		);
59
+		$this->dependency_map->registerDependencies(
60
+			'EventEspresso\core\domain\services\admin\PluginUpsells',
61
+			['EventEspresso\core\domain\Domain' => EE_Dependency_Map::load_from_cache]
62
+		);
63 63
 
64
-        $this->loader->getShared(
65
-            'EventEspresso\core\domain\entities\routing\data_nodes\EventEspressoData'
66
-        );
67
-        /** @var WordPressPluginsPageData $data_node */
68
-        $data_node = $this->loader->getShared(
69
-            'EventEspresso\core\domain\entities\routing\data_nodes\domains\WordPressPluginsPageData'
70
-        );
71
-        $this->setDataNode($data_node);
72
-        /** @var PluginUpsells $plugin_upsells */
73
-        $plugin_upsells = $this->loader->getShared('EventEspresso\core\domain\services\admin\PluginUpsells');
74
-        $plugin_upsells->decafUpsells();
75
-    }
64
+		$this->loader->getShared(
65
+			'EventEspresso\core\domain\entities\routing\data_nodes\EventEspressoData'
66
+		);
67
+		/** @var WordPressPluginsPageData $data_node */
68
+		$data_node = $this->loader->getShared(
69
+			'EventEspresso\core\domain\entities\routing\data_nodes\domains\WordPressPluginsPageData'
70
+		);
71
+		$this->setDataNode($data_node);
72
+		/** @var PluginUpsells $plugin_upsells */
73
+		$plugin_upsells = $this->loader->getShared('EventEspresso\core\domain\services\admin\PluginUpsells');
74
+		$plugin_upsells->decafUpsells();
75
+	}
76 76
 
77 77
 
78
-    /**
79
-     * implements logic required to run during request
80
-     *
81
-     * @return bool
82
-     * @since   $VID:$
83
-     */
84
-    protected function requestHandler(): bool
85
-    {
86
-        /** @var WordPressPluginsPageAssetManager $asset_manager */
87
-        $asset_manager = $this->loader->getShared(
88
-            'EventEspresso\core\domain\services\assets\WordPressPluginsPageAssetManager'
89
-        );
90
-        add_action('admin_enqueue_scripts', [$asset_manager, 'enqueueAssets']);
91
-        return true;
92
-    }
78
+	/**
79
+	 * implements logic required to run during request
80
+	 *
81
+	 * @return bool
82
+	 * @since   $VID:$
83
+	 */
84
+	protected function requestHandler(): bool
85
+	{
86
+		/** @var WordPressPluginsPageAssetManager $asset_manager */
87
+		$asset_manager = $this->loader->getShared(
88
+			'EventEspresso\core\domain\services\assets\WordPressPluginsPageAssetManager'
89
+		);
90
+		add_action('admin_enqueue_scripts', [$asset_manager, 'enqueueAssets']);
91
+		return true;
92
+	}
93 93
 }
Please login to merge, or discard this patch.
core/admin/EE_Admin_Page_Menu_Group.core.php 1 patch
Indentation   +16 added lines, -16 removed lines patch added patch discarded remove patch
@@ -10,22 +10,22 @@
 block discarded – undo
10 10
  */
11 11
 class EE_Admin_Page_Menu_Group extends AdminMenuGroup
12 12
 {
13
-    /**
14
-     * @return string
15
-     * @deprecated $VID:$
16
-     */
17
-    protected function _add_menu_page(): string
18
-    {
19
-        return $this->registerMenuItem();
20
-    }
13
+	/**
14
+	 * @return string
15
+	 * @deprecated $VID:$
16
+	 */
17
+	protected function _add_menu_page(): string
18
+	{
19
+		return $this->registerMenuItem();
20
+	}
21 21
 
22 22
 
23
-    /**
24
-     * @return string
25
-     * @deprecated $VID:$
26
-     */
27
-    private function _group_link(): string
28
-    {
29
-        return $this->groupLink();
30
-    }
23
+	/**
24
+	 * @return string
25
+	 * @deprecated $VID:$
26
+	 */
27
+	private function _group_link(): string
28
+	{
29
+		return $this->groupLink();
30
+	}
31 31
 }
Please login to merge, or discard this patch.