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

321 lines 8.2 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 ( 'first_column' == $this->value_labels_position ) {
55 foreach ( $this->data as $columns ) {
56 if ( '' != trim( $columns[0] ) ) {
57 $this->value_labels[] = $this->clean_labels( $columns[0] );
58 }
59 }
60 }
61 elseif ( 'first_row' == $this->value_labels_position ) {
62 foreach ( $this->data[0] as $column ) {
63 if ( '' != trim( $column ) ) {
64 $this->value_labels[] = $this->clean_labels( $column );
65 }
66 }
67 }
68 elseif ( 'both' == $this->value_labels_position ) {
69 foreach ( $this->data as $columns ) {
70 if ( '' != trim( $columns[0] ) ) {
71 $this->value_labels['first_column'][] = $this->clean_labels( $columns[0] );
72 }
73 }
74
75 foreach ( $this->data[0] as $column ) {
76 if ( '' != trim( $column ) ) {
77 $this->value_labels['first_row'][] = $this->clean_labels( $column );
78 }
79 }
80 }
81
82 $this->value_labels = apply_filters( 'm_chart_value_labels', $this->value_labels, $this->value_labels_position, $this->data );
83 }
84
85 /**
86 * Helper function returns a string describing where the value labels are (first_row, first_column, both)
87 *
88 * @return string the position of the labels in the given data set
89 */
90 public function get_value_labels_position() {
91 if ( '' == $this->data[0][0] ) {
92 return 'both';
93 }
94 elseif (
95 ! is_numeric( $this->clean_data_point( $this->data[0][0], false ) )
96 && ! is_numeric( $this->clean_data_point( $this->data[1][0], false ) )
97 ) {
98 return 'first_column';
99 }
100
101 return 'first_row';
102 }
103
104 /**
105 * Helper function cleans data point values and by default records suffix/prefixes to a class var
106 *
107 * @param string $data_point a data point that may need to be cleaned or typed as an int
108 *
109 * @return int/string an integer of the cleaned data point or string if the cleaned value was not numeric
110 */
111 public function clean_data_point( $data_point ) {
112 $data_point = trim( $data_point );
113
114 // Find any prefixes/suffixes in the data
115 if ( '' == $this->data_prefix && '' == $this->data_suffix ) {
116 $this->parse_suffix_prefix( $data_point );
117 }
118
119 // Remove prefixes and suffixes
120 if ( '' != $this->data_prefix ) {
121 $data_point = str_replace( $this->data_prefix, '', $data_point );
122 }
123
124 if ( '' != $this->data_suffix ) {
125 $data_point = str_replace( $this->data_suffix, '', $data_point );
126 }
127
128 // Remove commas
129 $data_point = str_replace( ',', '', $data_point );
130
131 // Return value without typing it as an integer if it's not numeric
132 if ( ! is_numeric( $data_point ) ) {
133 return trim( $data_point );
134 }
135
136 // By typing it as an integer we prevent json_encode from later treating it as a string
137 return floatval( trim( $data_point ) );
138 }
139
140 /**
141 * Checks for any suffix/prefix vales in a data_point
142 */
143 public function parse_suffix_prefix( $data_point ) {
144 $prefix_patterns = apply_filters( 'm_chart_prefix_patterns', $this->prefix_patterns );
145 $suffix_patterns = apply_filters( 'm_chart_suffix_patterns', $this->suffix_patterns );
146
147 if ( ! $this->data_prefix ) {
148 foreach ( $prefix_patterns as $pattern ) {
149 if ( preg_match( $pattern, $data_point, $match ) ) {
150 $this->data_prefix = $match[0];
151 }
152 }
153 }
154
155 if ( ! $this->data_suffix ) {
156 foreach ( $suffix_patterns as $pattern ) {
157 if ( preg_match( $pattern, $data_point, $match ) ) {
158 $this->data_suffix = $match[0];
159 }
160 }
161 }
162 }
163
164 /**
165 * Helper function cleans out label values
166 *
167 * @param string $label a label string
168 *
169 * @return string the label string cleaned of any problem content
170 */
171 public function clean_labels( $label ) {
172 $find = array(
173 '&nbsp;'
174 );
175
176 $replace = array(
177 ' '
178 );
179
180 return trim( str_replace( $find, $replace, $label ) );
181 }
182
183 /**
184 * Builds a cleaned and parsed array of data from the post's data
185 */
186 public function parse_set_data() {
187 // Reset these values in case a previous data set has altered them
188 $this->data_prefix = '';
189 $this->data_suffix = '';
190
191 $set_data_array = array();
192
193 if ( 'rows' == $this->parse_in && 'first_column' == $this->value_labels_position ) {
194 foreach ( $this->data as $columns ) {
195 if ( isset( $columns[1] ) ) {
196 $data_point = $this->clean_data_point( $columns[1] );
197
198 $set_data_array[] = $data_point;
199 }
200 }
201 }
202 elseif ( 'rows' == $this->parse_in && 'both' == $this->value_labels_position ) {
203 $limit = count( $this->data );
204 $this_sets = array();
205
206 // Collect the sets of data
207 for ( $i = 1; $i < $limit; $i++ ) {
208 foreach ( $this->data[ $i ] as $c_key => $column ) {
209 if ( 0 != $c_key ) {
210 $data_point = $this->clean_data_point( $column );
211 $key = $i - 1;
212
213 if ( ! isset( $this_sets[ $key ]['is_null'] ) ) {
214 $this_sets[ $key ]['is_null'] = true;
215 }
216
217 if ( is_numeric( $data_point ) ) {
218 $this_sets[ $key ]['is_null'] = false;
219 }
220
221 $this_sets[ $key ]['data'][] = $data_point;
222 }
223 }
224 }
225
226 // Compile the sets of data
227 foreach ( $this_sets as $key => $set ) {
228 if ( false == $set['is_null'] ) {
229 $set_data_array[ $key ] = $set['data'];
230 }
231 }
232 }
233 elseif ( 'columns' == $this->parse_in && 'both' == $this->value_labels_position ) {
234 $limit = count( $this->data );
235 $this_sets = array();
236
237 for ( $i = 1; $i < $limit; $i++ ) {
238 foreach ( $this->data[ $i ] as $key => $column ) {
239 if ( 0 == $key ) {
240 continue;
241 }
242
243 $data_point = $this->clean_data_point( $column );
244 $a_key = $key - 1;
245
246 if ( ! isset( $this_sets[ $a_key ]['is_null'] ) ) {
247 $this_sets[ $a_key ]['is_null'] = true;
248 }
249
250 if ( is_numeric( $data_point ) ) {
251 $this_sets[ $a_key ]['is_null'] = false;
252 }
253
254 $this_sets[ $a_key ]['data'][] = $data_point;
255 }
256 }
257
258 foreach ( $this_sets as $key => $set ) {
259 if ( false == $set['is_null'] ) {
260 $set_data_array[ $key ] = $set['data'];
261 }
262 }
263 }
264 else
265 {
266 foreach ( $this->data[1] as $column ) {
267 $data_point = $this->clean_data_point( $column );
268
269 $set_data_array[] = $data_point;
270 }
271 }
272
273 $set_data_array = $this->normalize_data_array( $set_data_array );
274
275 $this->set_data = apply_filters( 'm_chart_set_data', $set_data_array, $this->data, $this->parse_in );
276 }
277
278 /**
279 * Helper function normalizes the data array so that the number of data values data matches the number of value labels
280 *
281 * @param array $data_array an already parsed array of data
282 *
283 * @return array a normalized array of parsed data
284 */
285 public function normalize_data_array( $data_array ) {
286 if ( 'rows' == $this->parse_in && 'both' == $this->value_labels_position ) {
287 $label_count = count( $this->value_labels['first_row'] ) - 1;
288
289 foreach ( $data_array as $key => $data ) {
290 foreach ( $data as $t_key => $value ) {
291 if ( $t_key > $label_count ) {
292 unset( $data_array[ $key ][ $t_key ] );
293 }
294 }
295 }
296 }
297 elseif ( 'columns' == $this->parse_in && 'both' == $this->value_labels_position ) {
298 $label_count = count( $this->value_labels['first_column'] ) - 1;
299
300 foreach ( $data_array as $key => $data ) {
301 foreach ( $data as $t_key => $value ) {
302 if ( $t_key > $label_count ) {
303 unset( $data_array[ $key ][ $t_key ] );
304 }
305 }
306 }
307 }
308 else
309 {
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 }