Example #1
0
 /**
  * @param Config $config
  * @return static
  */
 public static function create(Config $config, ParserInterface $fallbackParser = null)
 {
     if (empty($config->getAttribute('mappings'))) {
         throw new UserException("Cannot initialize JSON Mapper with no mapping");
     }
     $mappers = [];
     foreach ($config->getAttribute('mappings') as $type => $mapping) {
         if (empty($mapping)) {
             throw new UserException("Empty mapping for '{$type}' in config.");
         }
         $mappers[$type] = new Mapper($mapping, $type);
     }
     foreach ($config->getJobs() as $job) {
         $type = $job->getDataType();
         if (empty($mappers[$type])) {
             if (is_null($fallbackParser)) {
                 throw new UserException("Missing mapping for '{$type}' in config.");
             }
         }
     }
     $parser = new static($mappers);
     $parser->setFallbackParser($fallbackParser);
     return $parser;
 }