/** * Parse a FIT file into data-arrays. * After parsing, the ->records will be filled with the data. When the data * matches the product's profile, the column names will be known and added. * If not the data will still be read, but we won't know what it is, what * scale factor to apply and what unit it is. * * @param string $filepath Absolute path to the .FIT file * @return false When unable to open the file for reading. * @throws \Fit\Exception */ public function parseFile($filepath) { $handle = false; if (is_file($filepath)) { $handle = @fopen($filepath, 'rb'); } if (false === $handle) { \Fit\Exception::create(1003, \Fit\Exception::$codes[1003] . ' filepath: ' . $filepath); } $this->reader = new \Zend_Io_Reader($handle); try { $this->readFileHeader(); $this->readRecords(); } catch (\Exception $e) { $this->reader->close(); throw $e; } $this->reader->close(); }
/** * Set the filetype for the upcoming messages. * @param enum \Fit\FileType $type * @return bool True when a known filetype was set, false when not found. */ public function setFile($type) { $ref = new \ReflectionClass('\\Fit\\FileType'); $constants = $ref->getConstants(); if (array_search($type, $constants) !== false) { $this->_filetype = (int) $type; $this->_store[$this->_filetype] = array(); return true; } \Fit\Exception::create(1004); }
/** * Writes the data to the file. * @param \Fit\Data $data The data that needs to be written to the file. * @return \Fit\Writer */ protected function writeTheRecords(\Fit\Data $data) { foreach ($data->getData() as $file_type => $messages) { $file_type_definition = $this->profile->findFileTypeByType($file_type); if ($file_type_definition !== null) { //we schrijven eerst de file_id message weg $file_id_written = false; foreach ($messages as $k => $msg) { if ($msg['name'] === 'file_id') { $message_def = $this->profile->findFieldDefByName($file_type_definition, 'file_id'); if ($message_def !== null) { $file_id_written = true; $this->writeMessageDefinition($message_def); $this->writeMessageData($message_def, $msg['data'][0]); unset($messages[$k]); break; } } } if ($file_id_written === false) { //we always need a file_id \Fit\Exception::create(1002, implode(PHP_EOL, array('Missing file_id in message data. Every file type needs one file_id message.', 'Example: ', '<code>', '$data = new \\Fit\\Data;', '$data->setFile(\\Fit\\FileType::activity);', '$data->add(\'file_id\', array(', ' \'type\' => \\Fit\\FileType::activity,', ' \'manufacturer\' => \\Fit\\Manufacturer::development,', ' \'product\' => 0,', ' \'serial_number\' => 0,', ' \'time_created\' => time() - mktime(0,0,0,12,31,1989),', ' ))', '</code>'))); } foreach ($messages as $msg) { $message_def = $this->profile->findFieldDefByName($file_type_definition, $msg['name']); if ($message_def !== null) { foreach ($msg['data'] as $records) { $this->writeMessageDefinition($message_def); $this->writeMessageData($message_def, $records); } } } } } return $this; }