/** * Get the csv file as a string. * * @return string */ public function asString() { $tmp = new \SplTempFileObject(); foreach ($this->data as $row) { $tmp->fputcsv($row, $this->delimiter); if ($this->lineEnding !== "\n") { $tmp->fseek(-1, \SEEK_CUR); $tmp->fwrite($this->lineEnding); } } # Find out how much data we have written $length = $tmp->ftell(); if ($length < 1) { return ""; } # Reset the internal pointer and return all the data we have written $tmp->fseek(0); return $tmp->fread($length); }
/** * Removes a Key/Value pair based on its position in the file * * @param integer $position The offset position in the file */ public function remove($position) { $temp = new \SplTempFileObject(-1); $this->file->flock(LOCK_EX); $filesize = $this->file->getSize(); $metadata = $this->getMetadata($position); // Seek past the document we want to remove $this->file->fseek($metadata->length, SEEK_CUR); // Write everything after the target document to memory $temp->fwrite($this->file->fread($filesize)); // Clear the file up to the target document $this->file->ftruncate($position); // Write Temp back to the end of the file $temp->fseek(0); $this->file->fseek(0, SEEK_END); $this->file->fwrite($temp->fread($filesize)); $this->file->flock(LOCK_UN); }