/** * {@inheritdoc} * * Source format: CSV * * <code> * $options = [ * 'delimiter' => ',', // typically ,|;|\t * 'enclosure' => '"', // typically "|' * 'escape' => '\\', // how the above is to be escaped * 'pivot' => false // pivot input array * ]; * </code> * * @param mixed $resource * @param array $options */ public function fromArray($resource, array $options = []) { $options = array_merge(['delimiter' => ',', 'enclosure' => '"', 'escape' => '\\', 'pivot' => false], $options); $resource = parent::getRealResource($resource, $options); if ($options['pivot']) { $resource = ArrayUtils::pivot($resource); } $dataDepth = self::verifyDataDepthLimit($resource, 2); if ($dataDepth === 1) { $resource = [$resource]; } else { if ($dataDepth > 2) { $msg = '$resource must have one or two levels'; if ($options['pivot']) { $msg .= ' after it has been pivoted'; } throw new JigException($msg); } } $csv = ''; foreach ($resource as $valueArr) { $line = ''; foreach ($valueArr as $value) { $line .= StringUtils::quote($value, $options) . $options['delimiter']; } $csv .= rtrim($line, $options['delimiter']) . PHP_EOL; } return $csv; }
public function fromHtml($resource, array $options = []) { $resource = parent::getRealResource($resource, $options); DompdfNsWrapper::init(); $dompdf = new \DOMPDF(); $dompdf->load_html($resource); $dompdf->render(); $dompdf->stream("sample.pdf"); }
public function toHtml($resource, array $options = []) { $resource = parent::getRealResource($resource, $options); $parser = new Parser(); return $parser->transform($resource); }
/** * Avoids duplicate code for toArray and toObject * * @param mixed $resource * @param array $options * @return mixed */ protected function toMixed($resource, array $options = []) { $options = array_merge(['depth' => 512, 'options' => 0], $options); return json_decode(parent::getRealResource($resource, $options), $options['assoc'], $options['depth'], $options['options']); }
public function fromArray($resource, array $options = []) { $options = array_merge(['delimiter' => ',', 'enclosure' => '"', 'escape' => '\\', 'input_encoding' => 'UTF-8', 'parse_file' => false, 'output_format' => 'xlsx'], $options); $resource = parent::getRealResource($resource, $options); if (!is_string($resource) || !is_file($resource)) { $csv = new Csv($options); $resource = parent::tempStore(uniqid(), $csv->fromArray($resource, $options)); } PhpExcelNsWrapper::init(); $objReader = new \PHPExcel_Reader_CSV(); $objReader->setInputEncoding($options['input_encoding']); $objReader->setDelimiter($options['delimiter']); $objReader->setEnclosure($options['enclosure']); $objReader->setLineEnding(false !== strpos($resource, "\r\n") ? "\r\n" : "\n"); $objReader->setSheetIndex(0); $objPHPExcel = $objReader->load($resource); // The following statement is borrowed from PHPExcel_IOFactory switch (strtolower($options['output_format'])) { case 'xlsx': // Excel (OfficeOpenXML) Spreadsheet // Excel (OfficeOpenXML) Spreadsheet case 'xlsm': // Excel (OfficeOpenXML) Macro Spreadsheet (macros will be discarded) // Excel (OfficeOpenXML) Macro Spreadsheet (macros will be discarded) case 'xltx': // Excel (OfficeOpenXML) Template // Excel (OfficeOpenXML) Template case 'xltm': // Excel (OfficeOpenXML) Macro Template (macros will be discarded) $extensionType = 'Excel2007'; break; case 'xls': // Excel (BIFF) Spreadsheet // Excel (BIFF) Spreadsheet case 'xlt': // Excel (BIFF) Template $extensionType = 'Excel5'; break; case 'ods': // Open/Libre Offic Calc // Open/Libre Offic Calc case 'ots': // Open/Libre Offic Calc Template $extensionType = 'OOCalc'; break; case 'slk': $extensionType = 'SYLK'; break; case 'xml': // Excel 2003 SpreadSheetML $extensionType = 'Excel2003XML'; break; case 'gnumeric': $extensionType = 'Gnumeric'; break; case 'htm': case 'html': $extensionType = 'HTML'; break; case 'csv': // Do nothing // This must be handled by the CSV class break; default: break; } if (is_null($extensionType)) { throw new JigException('Unable to identify a reader for this file'); } $writer = \PHPExcel_IOFactory::createWriter($objPHPExcel, $extensionType); ob_start(); $writer->save('php://output'); $spreadsheet = ob_get_contents(); ob_end_clean(); return $spreadsheet; }
/** * {@inheritdoc} * * Source format: YAML * * @param mixed $resource * @param array $options * @return array */ public function toArray($resource, array $options = []) { return sfYaml::parse(parent::getRealResource($resource, $options)); }