type = $type; $search = new Search(); $search->addSchema($table); $search->addColumn('*'); $result = $search->getRows(); if ($this->type !== self::DATATYPE_GLOBAL) { $pids = $search->getPids(); } echo $this->header($search->getColumns()); foreach ($result as $i => $row) { if ($this->type !== self::DATATYPE_GLOBAL) { $pid = $pids[$i]; } else { $pid = ''; } echo $this->row($row, $pid); } } /** * Create the header * * @param Column[] $columns * @return string */ protected function header($columns) { $row = ''; if ($this->type !== self::DATATYPE_GLOBAL) { $row .= $this->escape('pid'); $row .= ','; } foreach ($columns as $col) { $row .= $this->escape($col->getLabel()); $row .= ','; } return rtrim($row, ',') . "\r\n"; } /** * Create one row of data * * @param Value[] $values * @param string $pid pid of this row * @return string */ protected function row($values, $pid) { $row = ''; if ($this->type !== self::DATATYPE_GLOBAL) { $row .= $this->escape($pid); $row .= ','; } foreach ($values as $value) { /** @var Value $value */ $val = $value->getRawValue(); if (is_array($val)) $val = implode(',', $val); // FIXME check escaping of composite ids (JSON with """") $row .= $this->escape($val); $row .= ','; } return rtrim($row, ',') . "\r\n"; } /** * Escapes and wraps the given string * * Uses doubled quotes for escaping which seems to be the standard escaping method for CSV * * @param string $str * @return string */ protected function escape($str) { return '"' . str_replace('"', '""', $str) . '"'; } }