예제 #1
0
 /**
  * Edit message event.
  *
  * @param TranslationEditMessageEvent $event
  */
 public function addToHistory(TranslationEditMessageEvent $event)
 {
     $manager = $this->repository->getManager();
     $document = $event->getDocument();
     $locale = $this->getLocale($event);
     $oldMessage = $this->getOldMessage($document, $locale);
     $historyDocument = $this->setDocument($document, $oldMessage, $locale);
     $manager->persist($historyDocument);
     $manager->commit();
 }
예제 #2
0
 /**
  * Converts raw array to document.
  *
  * @param array      $rawData
  * @param Repository $repository
  *
  * @return DocumentInterface
  *
  * @throws \LogicException
  */
 public function convertToDocument($rawData, $repository)
 {
     $types = $this->metadataCollector->getMappings($repository->getManager()->getConfig()['mappings']);
     if (isset($types[$rawData['_type']])) {
         $metadata = $types[$rawData['_type']];
     } else {
         throw new \LogicException("Got document of unknown type '{$rawData['_type']}'.");
     }
     $data = isset($rawData['_source']) ? $rawData['_source'] : array_map('reset', $rawData['fields']);
     /** @var DocumentInterface $object */
     $object = $this->assignArrayToObject($data, new $metadata['namespace'](), $metadata['aliases']);
     $this->setObjectFields($object, $rawData, ['_id', '_score', 'fields _parent', 'fields _ttl']);
     return $object;
 }
예제 #3
0
파일: Crud.php 프로젝트: saimaz/ApiBundle
 /**
  * {@inheritdoc}
  */
 public function readAll(Repository $repository, array $parameters = [])
 {
     $search = $repository->createSearch();
     $search->addQuery(new MatchAllQuery());
     if (isset($parameters['size'])) {
         $search->setSize($parameters['size']);
     }
     if (isset($parameters['from'])) {
         $search->setFrom($parameters['from']);
     }
     $results = $repository->execute($search, Result::RESULTS_ARRAY);
     if (!isset($results[0])) {
         return null;
     }
     return $results;
 }
 /**
  * Returns scan results iterator.
  *
  * @param Repository $repository
  * @param int        $chunkSize
  *
  * @return RawIterator
  */
 protected function getResults(Repository $repository, $chunkSize)
 {
     $search = $repository->createSearch();
     $search->setScroll()->setSize($chunkSize);
     $search->addQuery(new MatchAllQuery());
     return $repository->execute($search, Repository::RESULTS_RAW_ITERATOR);
 }
예제 #5
0
 /**
  * Tests setter and getter of index name.
  */
 public function testIndexName()
 {
     $uniqueIndexName = 'test_index_' . uniqid();
     $manager = $this->repository->getManager();
     $this->assertNotEquals($uniqueIndexName, $manager->getIndexName());
     $manager->setIndexName($uniqueIndexName);
     $this->assertEquals($uniqueIndexName, $manager->getIndexName());
 }
 /**
  * Advances scan page.
  *
  * @return $this
  */
 protected function page()
 {
     if ($this->key() == $this->count() || !$this->isScrollable()) {
         return $this;
     }
     $raw = $this->repository->scroll($this->scrollId, $this->scrollDuration, Repository::RESULTS_RAW);
     $this->rewind();
     $this->scrollId = $raw['_scroll_id'];
     $this->documents = $raw['hits']['hits'];
     return $this;
 }
 /**
  * Creates locales list.
  *
  * @param ViewData\ChoicesAwareViewData $filter
  *
  * @return array
  */
 private function buildLocalesList($filter)
 {
     $search = $this->repository->createSearch();
     $localeAgg = new TermsAggregation('locale_agg');
     $localeAgg->setField('messages.locale');
     $search->addAggregation($localeAgg);
     $result = $this->repository->execute($search, Result::RESULTS_RAW);
     $list = [];
     foreach ($result['aggregations']['agg_locale_agg']['buckets'] as $value) {
         $list[$value['key']] = true;
     }
     ksort($list);
     $activeLocales = [];
     if ($filter->getState()->isActive()) {
         foreach ($filter->getChoices() as $choice) {
             $activeLocales[$choice->getLabel()] = $choice->isActive();
         }
         $list = array_merge($list, $activeLocales);
     }
     return $list;
 }
 /**
  * Returns translation from elasticsearch.
  *
  * @param string $id
  *
  * @return object
  *
  * @throws BadRequestHttpException
  */
 private function getTranslation($id)
 {
     try {
         $document = $this->repository->find($id);
     } catch (Missing404Exception $e) {
         $document = null;
     }
     if ($document === null) {
         throw new BadRequestHttpException('Invalid translation Id.');
     }
     return $document;
 }
예제 #9
0
 /**
  * Converts raw array to document.
  *
  * @param array      $rawData
  * @param Repository $repository
  *
  * @return DocumentInterface
  *
  * @throws \LogicException
  */
 public function convertToDocument($rawData, Repository $repository)
 {
     $types = $this->metadataCollector->getMappings($repository->getManager()->getConfig()['mappings']);
     if (isset($types[$rawData['_type']])) {
         $metadata = $types[$rawData['_type']];
     } else {
         throw new \LogicException("Got document of unknown type '{$rawData['_type']}'.");
     }
     switch (true) {
         case isset($rawData['_source']):
             $rawData = array_merge($rawData, $rawData['_source']);
             break;
         case isset($rawData['fields']):
             $rawData = array_merge($rawData, $rawData['fields']);
             break;
         default:
             // Do nothing.
             break;
     }
     /** @var DocumentInterface $object */
     $object = $this->assignArrayToObject($rawData, new $metadata['namespace'](), $metadata['aliases']);
     return $object;
 }
예제 #10
0
 /**
  * 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;
 }
예제 #11
0
 /**
  * Tests cloning documents.
  */
 public function testCloningDocuments()
 {
     $manager = $this->repository->getManager();
     $document = new ProductDocument();
     $document->setId('tuna_id');
     $document->title = 'tuna';
     $manager->persist($document);
     $manager->commit();
     $document = $this->repository->find('tuna_id');
     $clone = clone $document;
     $this->assertNull($clone->getId(), 'Id should be null\'ed.');
     $manager->persist($clone);
     $manager->commit();
     $search = $this->repository->createSearch()->addQuery(new TermQuery('title', 'tuna'));
     $this->assertCount(2, $this->repository->execute($search), '2 Results should be found.');
 }
예제 #12
0
 /**
  * {@inheritdoc}
  */
 public function commit(Repository $repository)
 {
     return $repository->getManager()->commit();
 }
 public function project($event)
 {
     $user = $this->repository->find($event->userId());
     $this->repository->update($event->userId(), ['points' => $user->getPoints() + $event->earnedPoints()]);
 }