/** * Exports es index to provided file. * * @param Manager $manager * @param string $filename * @param array $types * @param int $chunkSize * @param OutputInterface $output */ public function exportIndex(Manager $manager, $filename, $types, $chunkSize, OutputInterface $output) { $typesMapping = $manager->getMetadataCollector()->getMappings($manager->getConfig()['mappings']); $typesToExport = []; if ($types) { foreach ($types as $type) { if (!array_key_exists($type, $typesMapping)) { throw new \InvalidArgumentException(sprintf('Type "%s" does not exist.', $type)); } $typesToExport[] = $typesMapping[$type]['bundle'] . ':' . $typesMapping[$type]['class']; } } else { foreach ($typesMapping as $type => $typeConfig) { $typesToExport[] = $typeConfig['bundle'] . ':' . $typeConfig['class']; } } $repo = $manager->getRepository($typesToExport); $results = $this->getResults($repo, $chunkSize); $progress = new ProgressBar($output, $results->count()); $progress->setRedrawFrequency(100); $progress->start(); $metadata = ['count' => $results->count(), 'date' => date(\DateTime::ISO8601)]; $writer = $this->getWriter($this->getFilePath($filename), $metadata); foreach ($results as $data) { $doc = array_intersect_key($data, array_flip(['_id', '_type', '_source', 'fields'])); $writer->push($doc); $progress->advance(); } $writer->finalize(); $progress->finish(); $output->writeln(''); }
/** * Validates fields if the allow_extra_fields property * is false or allow_fields are set * * @param Request $request * @param Repository $repository * @param array $data * * @return array */ public function validateFields(Request $request, Repository $repository, $data) { $config = []; $validation = []; foreach ($this->versions as $version) { foreach ($version['endpoints'] as $endpoint) { if ($endpoint['repository'] == $request->attributes->get('repository')) { $config = $endpoint; break; } } if ($config != []) { break; } } if (!$config['allow_extra_fields'] || $config['allow_fields']) { $mapping = $this->manager->getMetadataCollector()->getMapping($repository->getClassName()); $forbiddenFields = $mapping['properties']; if ($config['allow_fields']) { foreach ($config['allow_fields'] as $field) { unset($forbiddenFields[$field]); } } foreach ($data as $parameter => $value) { if (!array_key_exists($parameter, $mapping['properties']) && $parameter != '_id') { $validation['message'] = sprintf('Property `%s` does not exist in the mapping of `%s`.', $parameter, $repository->getType()); return $validation; } if ($config['allow_fields'] && array_key_exists($parameter, $forbiddenFields)) { $validation['message'] = sprintf('You are not allowed to insert or modify the field `%s` in `%s`', $parameter, $repository->getType()); return $validation; } } } return $validation; }