Beispiel #1
0
 /**
  * @param resource $stream
  *
  * @return Iterator
  */
 public function parse($stream)
 {
     if ($this->format->isEachLine()) {
         $iterator = new LineStreamIterator($stream, [LineStreamIterator::OPTION_ENDING => "\n", LineStreamIterator::OPTION_IGNORE_BLANK => $this->format->isIgnoreBlankLines(), LineStreamIterator::OPTION_INCLUDE_ENDING => false]);
         $iterator->setIgnoreBlank($this->format->isIgnoreBlankLines());
         return new MapIterator($iterator, [$this, 'decodeJson']);
     } else {
         $json = $this->decodeJson(stream_get_contents($stream));
         if (is_array($json)) {
             return new ArrayIterator($json);
         } else {
             throw new RuntimeException("Expecting a json array to parse, unknown format detected");
         }
     }
 }
Beispiel #2
0
 /**
  * JsonFormatter constructor.
  *
  * @param JsonFormatInterface $format
  */
 public function __construct(JsonFormatInterface $format)
 {
     $this->addProcessor(new DateTimeProcessor());
     $this->format = $format;
     $this->linePostfix = $format->isSingleBlock() ? static::JSON_ARRAY_SEPARATOR : PHP_EOL;
     $this->encodeOptions = $format->getJsonEncodeOptions();
     // If the json is a blob on each line, turn off pretty print to ensure it is all on a single line
     if ($format->isEachLine()) {
         $this->encodeOptions &= ~JSON_PRETTY_PRINT;
     }
 }