PluginProbe
M Chart / 1.3.1
M Chart v1.3.1
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.3.1, at components/class-m-chart-parse.php

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