Esempio n. 1
0
 public function testAddContentWhenQuotesHaveBeenClosedWillThrowAnException()
 {
     $value = new Value();
     $value->setInQuotes(true);
     $value->addContent('some stuff');
     $value->setInQuotes(false);
     static::assertEquals('some stuff', $value->getValue());
     static::expectException(RuntimeException::class);
     $value->addContent('more content');
 }
Esempio n. 2
0
 /**
  * @param Iterator $tokens
  *
  * @return Iterator Iterator of csv line arrays
  */
 public function parse(Iterator $tokens)
 {
     $value = new Value($this->valueParsers);
     $row = [];
     foreach ($tokens as $token) {
         switch ($token[0]) {
             case Token::T_QUOTE:
                 $value->setInQuotes(!$value->isInQuotes());
                 break;
             case Token::T_CONTENT:
                 $value->addContent($token[1]);
                 break;
             case Token::T_DOUBLE_QUOTE:
                 $value->addContent(substr($token[1], 0, $token[3] / 2));
                 break;
             case Token::T_NULL:
                 if ($value->isEmpty() && !$value->isInQuotes() && !$value->wasQuoted()) {
                     $value->addContent($token[1]);
                     $value->setIsNull();
                 } else {
                     $value->addContent($token[1]);
                 }
                 break;
             case Token::T_DELIMITER:
                 $row[] = $value->getValue();
                 $value->reset();
                 break;
             case Token::T_NEW_LINE:
                 $row[] = $value->getValue();
                 $value->reset();
                 (yield $row);
                 $row = [];
                 break;
             default:
                 break;
         }
     }
     if (!$value->isEmpty()) {
         if ($value->isInQuotes()) {
             throw new RuntimeException("Unmatched quote at the end of the csv data");
         }
         $row[] = $value->getValue();
     }
     if (count($row) > 0) {
         (yield $row);
     }
 }