Exemple #1
0
 /**
  * Parses and converts the data to an array of multiple sessions. Keys will
  * be converted to lowercase names. Note that any second session will
  * miss header data!
  *
  * @return   array
  *
  */
 protected static function parse_data($full_data)
 {
     // Split data by sessions
     $data_sessions = explode('[END]', $full_data);
     // Prepare array data collection
     $array_data_collection = array();
     // Loop each part
     foreach ($data_sessions as $data) {
         // Empty data, ignore
         if (!trim($data)) {
             continue;
         }
         // Prepare array data
         $array_data = array();
         // Are laps zero based?
         $laps_zero_based = (bool) strpos($data, 'Lap=(0,');
         //----  Loop each line
         // Remember section
         $section = null;
         foreach (preg_split("/((\r?\n)|(\r\n?))/", $data) as $line) {
             // Empty line or META info, ignore this
             if (!$line or strpos($line, '//') !== false) {
                 continue;
             }
             // Is header
             if (preg_match('/^\\[(.*)\\]$/i', $line, $matches)) {
                 // Set section and continue to next line
                 $section = strtolower($matches[1]);
                 continue;
             }
             // No section, we failed
             if (!$section) {
                 return FALSE;
             }
             // Get key and value
             $split = explode('=', $line, 2);
             // Get key value for array
             $key = strtolower($split[0]);
             // Is lap value
             if ($key === 'lap') {
                 // Laps array does not exist yet
                 if (!array_key_exists('laps_collection', $array_data[$section])) {
                     // Init laps array
                     $array_data[$section]['laps_collection'] = array();
                 }
                 // Match lap information. e.g. (0, -1.000, 2:20.923)
                 preg_match('/^\\((.*), ?(.*), ?(.*)\\)$/i', $split[1], $lap_matches);
                 // Zero based laps
                 if ($laps_zero_based) {
                     // Increment lap number by 1
                     $lap_number = $lap_matches[1] + 1;
                 } else {
                     // Use lap numbers defined in file
                     $lap_number = $lap_matches[1];
                 }
                 // Elapsed time negative, make sure it's positive
                 if (0 > ($elapsed_time = (double) $lap_matches[2])) {
                     $elapsed_time = 0;
                 }
                 // Set lap data
                 $array_data[$section]['laps_collection'][$lap_number] = array('lap_number' => (int) $lap_number, 'elapsed_time' => $elapsed_time, 'time' => Helper::secondsFromFormattedTime($lap_matches[3]));
             } else {
                 // Value does not exist yet
                 // WARNING: Quick fix for duplicate slots (which indicate
                 //          multiple sessions?)
                 if (!isset($array_data[$section][$key])) {
                     // Set value
                     $array_data[$section][$key] = $split[1];
                 }
             }
         }
         $array_data_collection[] = $array_data;
     }
     return $array_data_collection;
 }
 /**
  * Test getting a value using a key from an array
  */
 public function testArrayGet()
 {
     $array = array('key' => 'value');
     $this->assertSame('value', Helper::arrayGet($array, 'key'));
     $this->assertNull(Helper::arrayGet($array, 'nothing'));
     $this->assertSame('default value', Helper::arrayGet($array, 'nothing', 'default value'));
 }