public function testIsAnalyzed() { $analyzer = new Analyzer($this->getLogger('analyzer', true)); $data = [(object) ['id' => 1, 'str' => "hi"]]; $analyzer->analyze($data, 'test'); self::assertFalse($analyzer->isAnalyzed('test')); $analyzer = new Analyzer($this->getLogger('analyzer', true), null, 1); self::assertFalse($analyzer->isAnalyzed('test')); $analyzer->analyze($data, 'test'); self::assertTrue($analyzer->isAnalyzed('test')); }
/** * Parse data of known type * * @param array $data * @param string $type * @param string|array $parentId * @return void * @see Parser::process() */ public function parse(array $data, $type, $parentId = null) { if (!$this->analyzer->isAnalyzed($type) && (empty($this->analyzer->getRowsAnalyzed()[$type]) || $this->analyzer->getRowsAnalyzed()[$type] < count($data))) { // analyse instead of failing if the data is unknown! $this->log->log("debug", "Trying to parse an unknown data type '{$type}'. Trying on-the-fly analysis", ["data" => $data, "type" => $type, "parentId" => $parentId]); $this->analyzer->analyze($data, $type); } $parentId = $this->validateParentId($parentId); $csvFile = $this->createCsvFile($type, $parentId); $parentCols = array_fill_keys(array_keys($parentId), "string"); foreach ($data as $row) { // in case of non-associative array of strings // prepare {"data": $value} objects for each row if (is_scalar($row) || is_null($row)) { $row = (object) [self::DATA_COLUMN => $row]; } elseif ($this->analyzer->getNestedArrayAsJson() && is_array($row)) { $row = (object) [self::DATA_COLUMN => json_encode($row)]; } // Add parentId to each row if (!empty($parentId)) { $row = (object) array_replace((array) $row, $parentId); } $csvRow = $this->parseRow($row, $type, $parentCols); $csvFile->writeRow($csvRow->getRow()); } }