| 1 |
<?php |
| 2 |
|
| 3 |
// +----------------------------------------------------------------------+ |
| 4 |
// | Copyright 2013 Madpixels (email : visualizer@madpixels.net) | |
| 5 |
// +----------------------------------------------------------------------+ |
| 6 |
// | This program is free software; you can redistribute it and/or modify | |
| 7 |
// | it under the terms of the GNU General Public License, version 2, as | |
| 8 |
// | published by the Free Software Foundation. | |
| 9 |
// | | |
| 10 |
// | This program is distributed in the hope that it will be useful, | |
| 11 |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 |
// | GNU General Public License for more details. | |
| 14 |
// | | |
| 15 |
// | You should have received a copy of the GNU General Public License | |
| 16 |
// | along with this program; if not, write to the Free Software | |
| 17 |
// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, | |
| 18 |
// | MA 02110-1301 USA | |
| 19 |
// +----------------------------------------------------------------------+ |
| 20 |
// | Author: Eugene Manuilov <eugene@manuilov.org> | |
| 21 |
// +----------------------------------------------------------------------+ |
| 22 |
|
| 23 |
/** |
| 24 |
* The abstract class for source managers. |
| 25 |
* |
| 26 |
* @category Visualizer |
| 27 |
* @package Source |
| 28 |
* |
| 29 |
* @since 1.0.0 |
| 30 |
* @abstract |
| 31 |
*/ |
| 32 |
abstract class Visualizer_Source { |
| 33 |
|
| 34 |
/** |
| 35 |
* The array of data. |
| 36 |
* |
| 37 |
* @since 1.0.0 |
| 38 |
* |
| 39 |
* @access protected |
| 40 |
* @var array |
| 41 |
*/ |
| 42 |
protected $_data = array(); |
| 43 |
|
| 44 |
/** |
| 45 |
* The array of series. |
| 46 |
* |
| 47 |
* @since 1.0.0 |
| 48 |
* |
| 49 |
* @access protected |
| 50 |
* @var array |
| 51 |
*/ |
| 52 |
protected $_series = array(); |
| 53 |
|
| 54 |
/** |
| 55 |
* Returns source name. |
| 56 |
* |
| 57 |
* @since 1.0.0 |
| 58 |
* |
| 59 |
* @abstract |
| 60 |
* @access public |
| 61 |
* @return string The name of source. |
| 62 |
*/ |
| 63 |
public abstract function getSourceName(); |
| 64 |
|
| 65 |
/** |
| 66 |
* Fetches information from source, parses it and builds series and data arrays. |
| 67 |
* |
| 68 |
* @since 1.0.0 |
| 69 |
* |
| 70 |
* @abstract |
| 71 |
* @access public |
| 72 |
*/ |
| 73 |
public abstract function fetch(); |
| 74 |
|
| 75 |
/** |
| 76 |
* Returns series parsed from source. |
| 77 |
* |
| 78 |
* @since 1.0.0 |
| 79 |
* |
| 80 |
* @access public |
| 81 |
* @return array The array of series. |
| 82 |
*/ |
| 83 |
public function getSeries() { |
| 84 |
return $this->_series; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Returns data parsed from source. |
| 89 |
* |
| 90 |
* @since 1.0.0 |
| 91 |
* |
| 92 |
* @access public |
| 93 |
* @return string The serialized array of data. |
| 94 |
*/ |
| 95 |
public function getData() { |
| 96 |
return serialize( $this->_data ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Returns raw data array. |
| 101 |
* |
| 102 |
* @since 1.1.0 |
| 103 |
* |
| 104 |
* @access public |
| 105 |
* @return array |
| 106 |
*/ |
| 107 |
public function getRawData() { |
| 108 |
return $this->_data; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Normalizes values according to series' type. |
| 113 |
* |
| 114 |
* @since 1.0.0 |
| 115 |
* |
| 116 |
* @access protected |
| 117 |
* @param array $data The row of data. |
| 118 |
* @return array Normalized row of data. |
| 119 |
*/ |
| 120 |
protected function _normalizeData( $data ) { |
| 121 |
// normalize values |
| 122 |
foreach ( $this->_series as $i => $series ) { |
| 123 |
// if no value exists for the seires, then add null |
| 124 |
if ( !isset( $data[$i] ) ) { |
| 125 |
$data[$i] = null; |
| 126 |
} |
| 127 |
|
| 128 |
if ( is_null( $data[$i] ) ) { |
| 129 |
continue; |
| 130 |
} |
| 131 |
|
| 132 |
switch ( $series['type'] ) { |
| 133 |
case 'number': |
| 134 |
$data[$i] = !empty( $data[$i] ) ? floatval( $data[$i] ) : null; |
| 135 |
break; |
| 136 |
case 'boolean': |
| 137 |
$data[$i] = !empty( $data[$i] ) ? filter_validate( $data[$i], FILTER_VALIDATE_BOOLEAN ) : null; |
| 138 |
break; |
| 139 |
case 'timeofday': |
| 140 |
$date = new DateTime( '1984-03-16T' . $data[$i] ); |
| 141 |
if ( $date ) { |
| 142 |
$data[$i] = array( |
| 143 |
intval( $date->format( 'H' ) ), |
| 144 |
intval( $date->format( 'i' ) ), |
| 145 |
intval( $date->format( 's' ) ), |
| 146 |
0, |
| 147 |
); |
| 148 |
} |
| 149 |
break; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
return $data; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Validates series tyeps. |
| 158 |
* |
| 159 |
* @since 1.0.1 |
| 160 |
* |
| 161 |
* @static |
| 162 |
* @access protected |
| 163 |
* @param array $types The icoming series types. |
| 164 |
* @return boolean TRUE if sereis types are valid, otherwise FALSE. |
| 165 |
*/ |
| 166 |
protected static function _validateTypes( $types ) { |
| 167 |
$allowed_types = array( 'string', 'number', 'boolean', 'date', 'datetime', 'timeofday' ); |
| 168 |
foreach ( $types as $type ) { |
| 169 |
if ( !in_array( $type, $allowed_types ) ) { |
| 170 |
return false; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
return true; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Re populates series if the source is dynamic. |
| 179 |
* |
| 180 |
* @since 1.1.0 |
| 181 |
* |
| 182 |
* @access public |
| 183 |
* @param array $series The actual array of series. |
| 184 |
* @param int $chart_id The chart id. |
| 185 |
* @return array The re populated array of series or old one. |
| 186 |
*/ |
| 187 |
public function repopulateSeries( $series, $chart_id ) { |
| 188 |
return $series; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Re populates data if the source is dynamic. |
| 193 |
* |
| 194 |
* @since 1.1.0 |
| 195 |
* |
| 196 |
* @access public |
| 197 |
* @param array $data The actual array of data. |
| 198 |
* @param int $chart_id The chart id. |
| 199 |
* @return array The re populated array of data or old one. |
| 200 |
*/ |
| 201 |
public function repopulateData( $data, $chart_id ) { |
| 202 |
return $data; |
| 203 |
} |
| 204 |
|
| 205 |
} |