PluginProbe
M Chart / 1.11.2
M Chart v1.11.2
2.3.2 2.3.1 2.3 2.2.2 2.2.1 2.2 trunk 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.10 1.10.1 1.11 1.11.1 1.11.2 1.12 1.2 1.2.1 1.3 1.3.1 1.3.2 All 53 releases
m-chart / components / class-m-chart-parse.php

class-m-chart-parse.php in M Chart 1.11.2, at components/class-m-chart-parse.php

322 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class M_Chart_Parse {
4 public $data;
5 public $data_prefix;
6 public $data_suffix;
7 public $value_labels;
8 public $value_labels_position;
9 public $set_data;
10 public $parse_in;
11 public $prefix_patterns = array(
12 '/^\$/',
13 '/^£/',
14 '/^/',
15 );
16 public $suffix_patterns = array(
17 '/%$/',
18 '/°$/',
19 '/°C$/',
20 '/°F$/',
21 '/K$/',
22 '/M$/',
23 '/B$/',
24 '/T$/',
25 );
26
27 /**
28 * Constructor
29 */
30 public function __construct() {
31
32 }
33
34 /**
35 * Parses a chart's data array for labels, data sets, and prefixes
36 *
37 * @return array an array of value labels filtered out of the data set
38 */
39 public function parse_data( $data, $parse_in ) {
40 $this->data = $data;
41 $this->parse_in = $parse_in;
42 $this->value_labels_position = $this->get_value_labels_position();
43 $this->parse_value_labels();
44 $this->parse_set_data();
45 return $this;
46 }
47
48 /**
49 * Helper function builds an array of the value labels for a data set
50 *
51 * @return array an array of value labels filtered out of the data set
52 */
53 public function parse_value_labels() {
54 $this->value_labels = array();
55
56 if ( 'none' == $this->value_labels_position ) {
57 return $this->value_labels;
58 }
59
60 if ( 'first_column' == $this->value_labels_position ) {
61 foreach ( (array) $this->data as $columns ) {
62 if ( '' != trim( (string) $columns[0] ) ) {
63 $this->value_labels[] = $this->clean_labels( $columns[0] );
64 }
65 }
66 } elseif ( 'first_row' == $this->value_labels_position ) {
67 foreach ( (array) $this->data[0] as $column ) {
68 if ( '' != trim( (string) $column ) ) {
69 $this->value_labels[] = $this->clean_labels( $column );
70 }
71 }
72 } elseif ( 'both' == $this->value_labels_position ) {
73 foreach ( (array) $this->data as $columns ) {
74 if ( '' != trim( (string) $columns[0] ) ) {
75 $this->value_labels['first_column'][] = $this->clean_labels( $columns[0] );
76 }
77 }
78
79 foreach ( (array) $this->data[0] as $column ) {
80 if ( '' != trim( (string) $column ) ) {
81 $this->value_labels['first_row'][] = $this->clean_labels( $column );
82 }
83 }
84 }
85
86 $this->value_labels = apply_filters( 'm_chart_value_labels', $this->value_labels, $this->value_labels_position, $this->data );
87 }
88
89 /**
90 * Helper function returns a string describing where the value labels are (first_row, first_column, both)
91 *
92 * @return string the position of the labels in the given data set
93 */
94 public function get_value_labels_position() {
95 if ( ! isset( $this->data[0][0] ) && ! isset( $this->data[1][0] ) ) {
96 return 'none';
97 }
98
99 if ( '' == $this->data[0][0] ) {
100 return 'both';
101 } elseif (
102 ! is_numeric( $this->clean_data_point( $this->data[0][0], false ) )
103 && ! is_numeric( $this->clean_data_point( $this->data[1][0], false ) )
104 ) {
105 return 'first_column';
106 }
107
108 return 'first_row';
109 }
110
111 /**
112 * Helper function cleans data point values and by default records suffix/prefixes to a class var
113 *
114 * @param string $data_point a data point that may need to be cleaned or typed as an int
115 *
116 * @return int/string an integer of the cleaned data point or string if the cleaned value was not numeric
117 */
118 public function clean_data_point( $data_point ) {
119 $data_point = trim( (string) $data_point );
120
121 // Find any prefixes/suffixes in the data
122 if ( '' == $this->data_prefix && '' == $this->data_suffix ) {
123 $this->parse_suffix_prefix( $data_point );
124 }
125
126 // Remove prefixes and suffixes
127 $data_point = preg_replace( $this->prefix_patterns, '', $data_point );
128 $data_point = preg_replace( $this->suffix_patterns, '', $data_point );
129
130 // Remove commas
131 $data_point = str_replace( ',', '', $data_point );
132
133 // Return value without typing it as an integer if it's not numeric
134 if ( ! is_numeric( $data_point ) ) {
135 return trim( $data_point );
136 }
137
138 // By typing it as an integer we prevent json_encode from later treating it as a string
139 return floatval( trim( $data_point ) );
140 }
141
142 /**
143 * Checks for any suffix/prefix vales in a data_point
144 */
145 public function parse_suffix_prefix( $data_point ) {
146 $prefix_patterns = apply_filters( 'm_chart_prefix_patterns', $this->prefix_patterns );
147 $suffix_patterns = apply_filters( 'm_chart_suffix_patterns', $this->suffix_patterns );
148
149 if ( ! $this->data_prefix ) {
150 foreach ( $prefix_patterns as $pattern ) {
151 if ( preg_match( $pattern, $data_point, $match ) ) {
152 $this->data_prefix = $match[0];
153 }
154 }
155 }
156
157 if ( ! $this->data_suffix ) {
158 foreach ( $suffix_patterns as $pattern ) {
159 if ( preg_match( $pattern, $data_point, $match ) ) {
160 $this->data_suffix = $match[0];
161 }
162 }
163 }
164 }
165
166 /**
167 * Helper function cleans out label values
168 *
169 * @param string $label a label string
170 *
171 * @return string the label string cleaned of any problem content
172 */
173 public function clean_labels( $label ) {
174 $label = trim( html_entity_decode( $label, ENT_QUOTES ) );
175
176 return preg_replace( '#<([a-z]+)([^>]+)*(?:>(.*)<\/\1>|\s+\/>)#', '$3', $label );
177 }
178
179 /**
180 * Builds a cleaned and parsed array of data from the post's data
181 */
182 public function parse_set_data() {
183 // Reset these values in case a previous data set has altered them
184 $this->data_prefix = '';
185 $this->data_suffix = '';
186
187 $set_data_array = array();
188
189 if ( 'rows' == $this->parse_in && 'first_column' == $this->value_labels_position ) {
190 foreach ( $this->data as $row ) {
191 foreach ( $row as $key => $column ) {
192 if ( '' == $column || 0 == $key ) {
193 continue;
194 }
195
196 $data_point = $this->clean_data_point( $column );
197
198 $set_data_array[] = $data_point;
199 }
200 }
201 } elseif ( 'rows' == $this->parse_in && 'both' == $this->value_labels_position ) {
202 $limit = count( $this->data );
203 $this_sets = array();
204
205 // Collect the sets of data
206 for ( $i = 1; $i < $limit; $i++ ) {
207 foreach ( $this->data[ $i ] as $c_key => $column ) {
208 if ( 0 != $c_key ) {
209 $data_point = $this->clean_data_point( $column );
210 $key = $i - 1;
211
212 if ( ! isset( $this_sets[ $key ]['is_null'] ) ) {
213 $this_sets[ $key ]['is_null'] = true;
214 }
215
216 if ( is_numeric( $data_point ) ) {
217 $this_sets[ $key ]['is_null'] = false;
218 }
219
220 $this_sets[ $key ]['data'][] = $data_point;
221 }
222 }
223 }
224
225 // Compile the sets of data
226 foreach ( $this_sets as $key => $set ) {
227 if ( false == $set['is_null'] ) {
228 $set_data_array[ $key ] = $set['data'];
229 }
230 }
231 } elseif ( 'columns' == $this->parse_in && 'both' == $this->value_labels_position ) {
232 $limit = count( $this->data );
233 $this_sets = array();
234
235 for ( $i = 1; $i < $limit; $i++ ) {
236 foreach ( $this->data[ $i ] as $key => $column ) {
237 if ( 0 == $key ) {
238 continue;
239 }
240
241 $data_point = $this->clean_data_point( $column );
242 $a_key = $key - 1;
243
244 if ( ! isset( $this_sets[ $a_key ]['is_null'] ) ) {
245 $this_sets[ $a_key ]['is_null'] = true;
246 }
247
248 if ( is_numeric( $data_point ) ) {
249 $this_sets[ $a_key ]['is_null'] = false;
250 }
251
252 $this_sets[ $a_key ]['data'][] = $data_point;
253 }
254 }
255
256 foreach ( $this_sets as $key => $set ) {
257 if ( false == $set['is_null'] ) {
258 $set_data_array[ $key ] = $set['data'];
259 }
260 }
261 } elseif ( isset( $this->data[1] ) ) {
262 foreach ( $this->data as $key => $columns ) {
263 foreach ( $columns as $column ) {
264 if ( '' == $column || 0 == $key ) {
265 continue;
266 }
267
268 $data_point = $this->clean_data_point( $column );
269
270 $set_data_array[] = $data_point;
271 }
272 }
273 }
274
275 $set_data_array = $this->normalize_data_array( $set_data_array );
276
277 $this->set_data = apply_filters( 'm_chart_set_data', $set_data_array, $this->data, $this->parse_in );
278 }
279
280 /**
281 * Helper function normalizes the data array so that the number of data values matches the number of value labels
282 *
283 * @param array $data_array an already parsed array of data
284 *
285 * @return array a normalized array of parsed data
286 */
287 public function normalize_data_array( $data_array ) {
288 if ( 'rows' == $this->parse_in && 'both' == $this->value_labels_position ) {
289 $label_count = is_array( $this->value_labels['first_row'] ) ? count( $this->value_labels['first_row'] ) - 1 : 0;
290
291 foreach ( $data_array as $key => $data ) {
292 foreach ( $data as $t_key => $value ) {
293 if ( $t_key > $label_count ) {
294 unset( $data_array[ $key ][ $t_key ] );
295 }
296 }
297 }
298 } elseif ( 'columns' == $this->parse_in && 'both' == $this->value_labels_position ) {
299 $label_count = is_array( $this->value_labels['first_column'] ) ? count( $this->value_labels['first_column'] ) - 1 : 0;
300
301 foreach ( $data_array as $key => $data ) {
302 foreach ( $data as $t_key => $value ) {
303 if ( $t_key > $label_count ) {
304 unset( $data_array[ $key ][ $t_key ] );
305 }
306 }
307 }
308 }
309 //else {
310 // $label_count = count( $this->value_labels ) - 1;
311 //
312 // foreach ( $data_array as $key => $data ) {
313 // if ( $key > $label_count ) {
314 // unset( $data_array[ $key ] );
315 // }
316 // }
317 //}
318
319 return $data_array;
320 }
321 }
322