src/DataParserBase.php
/**
     * Parse data string
     * 
     * @param string $raw SwissMetNet data string
     * @return array
     */
Parse data string

@param string $raw SwissMetNet data string
@return array
Parse data string @param string $raw SwissMetNet data string @return array
public static function parse(string $raw)
    {
        $data = [];
        $indices = [];
        foreach (explode("\n", $raw) as $line) {
            $line = trim($line);
            if ($line == '') {
                continue;
            }
            $parts = explode(static::SEPARATOR, $line);
            if ($parts[0] == static::LOCATION_FIELD_NAME) {
                $indices = $parts;
                continue;
            }
            if (count($parts) != count($indices)) {
                continue;
            }
            $location = null;
            $timestamp = null;
            foreach ($parts as $index => $value) {
                $column = $indices[$index];
                switch ($column) {
                    case static::LOCATION_FIELD_NAME:
                        $location = $value;
                        break;
                    case static::DATETIME_FIELD_NAME:
                        $timestamp = strtotime("{$value} " . static::TIMEZONE);
                        break;
                    default:
                        if ($value == static::MISSING_VALUE_STRING) {
                            break;
                        }
                        $floatValue = floatval($value);
                        $data[] = (object) ([
                            'timestamp' => $timestamp,
                            'loc' => $location,
                            'par' => $column,
                            'val' => $floatValue
                        ]);
                        break;
                }
            }
        }
        return $data;
    }
public static function parse ( string $ raw ) { $ data = [ ] ; $ indices = [ ] ; foreach ( explode ( " \n " , $ raw ) as $ line ) { $ line = trim ( $ line ) ; if ( $ line == '' ) { continue ; } $ parts = explode ( static :: SEPARATOR , $ line ) ; if ( $ parts [ 0 ] == static :: LOCATION_FIELD_NAME ) { $ indices = $ parts ; continue ; } if ( count ( $ parts ) != count ( $ indices ) ) { continue ; } $ location = null ; $ timestamp = null ; foreach ( $ parts as $ index => $ value ) { $ column = $ indices [ $ index ] ; switch ( $ column ) { case static :: LOCATION_FIELD_NAME : $ location = $ value ; break ; case static :: DATETIME_FIELD_NAME : $ timestamp = strtotime ( " { $ value } " . static :: TIMEZONE ) ; break ; default : if ( $ value == static :: MISSING_VALUE_STRING ) { break ; } $ floatValue = floatval ( $ value ) ; $ data [ ] = ( object ) ( [ 'timestamp' => $ timestamp , 'loc' => $ location , 'par' => $ column , 'val' => $ floatValue ] ) ; break ; } } } return $ data ; }
Parse data string
Parse data string
// Parse data // Skip over empty lines // Handle indices (Hopefully they come first) // Handle non-value lines // Finally a value line // Store current location // Store current timestamp parsed from date time string, example: "202005161230" // At this point the location and timestamp are known, so store the value if it isn't missing // All good, insert value