/**
  * @param Config $config
  * @return ParserInterface
  */
 protected function initParser(Config $config)
 {
     if (!empty($this->parser) && $this->parser instanceof ParserInterface) {
         return $this->parser;
     }
     $parser = Json::create($config, $this->getLogger(), $this->getTemp(), $this->metadata);
     $parser->getParser()->getStruct()->setAutoUpgradeToArray(true);
     $parser->getParser()->setCacheMemoryLimit('2M');
     $parser->getParser()->getAnalyzer()->setNestedArrayAsJson(true);
     if (empty($config->getAttribute('mappings'))) {
         $this->parser = $parser;
     } else {
         $this->parser = JsonMap::create($config, $parser);
     }
     return $this->parser;
 }
Esempio n. 2
0
 public function testMergeResults()
 {
     Logger::setLogger($this->getLogger('testMergeResults', true));
     $configFirst = JobConfig::create(['endpoint' => '1st', 'dataType' => 'first']);
     $configTags = JobConfig::create(['endpoint' => '2nd', 'dataType' => 'tags']);
     $config = new Config('ex', 'test', []);
     $config->setAttributes(['mappings' => ['first' => ['id' => ['type' => 'column', 'mapping' => ['destination' => 'item_id']], 'tags' => ['type' => 'table', 'destination' => 'tags', 'tableMapping' => ['user' => ['mapping' => ['destination' => 'user', 'primaryKey' => true]], 'tag' => ['mapping' => ['destination' => 'tag', 'primaryKey' => true]]], 'parentKey' => ['disable' => true]]], 'tags' => ['user' => ['mapping' => ['destination' => 'user', 'primaryKey' => true]], 'tag' => ['mapping' => ['destination' => 'tag', 'primaryKey' => true]]]]]);
     $firstData = json_decode('[
         {
             "id": 1,
             "arr": [1,2,3]
         },
         {
             "id": 2,
             "arr": ["a","b","c"],
             "tags": [
                 {
                     "user": "******",
                     "tag": "tag1"
                 },
                 {
                     "user": "******",
                     "tag": "tag2"
                 }
             ]
         }
     ]');
     $secondData = json_decode('[
         {
             "user": "******",
             "tag": "tag3"
         },
         {
             "user": "******",
             "tag": "tag4"
         }
     ]');
     $parser = JsonMap::create($config);
     $parser->process($firstData, $configFirst->getDataType());
     $parser->process($secondData, $configTags->getDataType());
     self::assertEquals(['"user","tag"' . PHP_EOL, '"asd","tag1"' . PHP_EOL, '"asd","tag2"' . PHP_EOL, '"asd","tag3"' . PHP_EOL, '"asd","tag4"' . PHP_EOL], file($parser->getResults()['tags']));
 }