/** * @expectedException \Zebba\Component\Loader\Exception\ParseException */ public function testParseUnevenLines() { $input = "key1;key2\nvalue1;2;NO\nvalue2;3"; $parser = new Csv\Parser(); $parser->parse($input); }
/** * @param mixed:string|\SplFileInfo $input * @param array $options * @throws ParseException * @return array */ public static function parse($input, array $options = array()) { $default_options = array('delimiter' => ';', 'encoding_source' => 'UTF-8', 'encoding_target' => 'UTF-8'); $options = array_merge($default_options, $options); // if input is a file, process it $file = null; if (false === strpos($input, '\\r\\n') && is_file($input)) { if (false === is_readable($input)) { throw new ParseException(sprintf('Unable to parse \'%s\' as the file is not readable.', $input)); } if ($input instanceof \SplFileInfo) { if (!in_array(mime_content_type($input->getPathname()), array('text/csv', 'text/plain'))) { throw new ParseException('This is not a CSV file.'); } } else { if (!in_array(mime_content_type($input), array('text/csv', 'text/plain'))) { throw new ParseException('This is not a CSV file.'); } } $file = $input; $input = file_get_contents($file); } $csv = new Csv\Parser(); try { return $csv->parse($input, $options['delimiter'], $options); } catch (ParseException $e) { if (!is_null($file)) { $e->setParsedFile($file); } throw $e; } }