/** * {@inheritdoc} */ public function normalize($value, $format = null, array $context = []) { if (!$this->normalizer instanceof NormalizerInterface) { throw new \LogicException('Serializer must be a normalizer'); } if (null === $value->getData()) { return null; } $productId = $context[ProductNormalizer::MONGO_ID]; $productCollection = $context[ProductNormalizer::MONGO_COLLECTION_NAME]; $data = []; $data['_id'] = $this->mongoFactory->createMongoId(); $data['attribute'] = $value->getAttribute()->getId(); $data['entity'] = $this->mongoFactory->createMongoDBRef($productCollection, $productId); if (null !== $value->getLocale()) { $data['locale'] = $value->getLocale(); } if (null !== $value->getScope()) { $data['scope'] = $value->getScope(); } $backendType = $value->getAttribute()->getBackendType(); if ('options' !== $backendType) { $data[$backendType] = $this->normalizeValueData($value->getData(), $backendType, $context); } else { $data['optionIds'] = $this->normalizeValueData($value->getData(), $backendType, $context); } return $data; }
/** * {@inheritdoc} */ public function normalize($product, $format = null, array $context = []) { if (!$this->normalizer instanceof NormalizerInterface) { throw new \LogicException('Serializer must be a normalizer'); } $data = []; if (null !== $product->getId()) { $data[self::MONGO_ID] = $this->mongoFactory->createMongoId($product->getId()); } else { $data[self::MONGO_ID] = $this->mongoFactory->createMongoId(); } $context[self::MONGO_ID] = $data[self::MONGO_ID]; if (null !== $product->getCreated()) { $data['created'] = $this->normalizer->normalize($product->getCreated(), self::FORMAT, $context); } else { $data['created'] = $this->mongoFactory->createMongoDate(); } $data['updated'] = $this->mongoFactory->createMongoDate(); $data['family'] = $product->getFamily() ? $product->getFamily()->getId() : null; $data['enabled'] = $product->isEnabled(); $data['groupIds'] = $this->normalizeGroups($product->getGroups()); $data['categoryIds'] = $this->normalizeCategories($product->getCategories()); $data['associations'] = $this->normalizeAssociations($product->getAssociations(), $context); $data['values'] = $this->normalizeValues($product->getValues(), $context); $data['normalizedData'] = $this->normalizer->normalize($product, 'mongodb_json'); $data['completenesses'] = []; unset($data['normalizedData']['completenesses']); return $data; }
/** * Get the products ids as an array of MongoDBRef * * @param Collection|ProductInterface[] $products * @param string $productCollection * * @return array */ protected function normalizeProducts($products, $productCollection) { $data = []; foreach ($products as $product) { $data[] = $this->mongoFactory->createMongoDBRef($productCollection, $product->getId()); } return $data; }
/** * {@inheritdoc} */ public function normalize($price, $format = null, array $context = []) { $data = []; $data['_id'] = $this->mongoFactory->createMongoId(); $data['currency'] = $price->getCurrency(); if (null !== $price->getData()) { $data['data'] = (double) $price->getData(); } return $data; }
/** * {@inheritdoc} */ public function normalize($media, $format = null, array $context = []) { $data = []; $data['_id'] = $this->mongoFactory->createMongoId(); $data['filename'] = $media->getFilename(); $data['filePath'] = $media->getFilePath(); $data['originalFilename'] = $media->getOriginalFilename(); $data['mimeType'] = $media->getMimeType(); return $data; }
/** * {@inheritdoc} */ public function normalize($metric, $format = null, array $context = []) { if (null === $metric->getData() || "" === $metric->getData() || null === $metric->getUnit() || "" === $metric->getUnit()) { return null; } $this->createMetricBaseValues($metric); $data = []; $data['_id'] = $this->mongoFactory->createMongoId(); $data['unit'] = $metric->getUnit(); $data['data'] = $metric->getData(); $data['baseUnit'] = $metric->getBaseUnit(); $data['baseData'] = $metric->getBaseData(); $data['family'] = $metric->getFamily(); return $data; }
/** * {@inheritdoc} */ public function write(array $products) { $this->collection = $this->documentManager->getDocumentCollection($this->productClass); $productsToInsert = array(); $productsToUpdate = array(); foreach ($products as $product) { if (null === $product->getId()) { $productsToInsert[] = $product; $product->setId($this->mongoFactory->createMongoId()); } else { $productsToUpdate[] = $product; } } $this->productManager->handleAllMedia($products); $this->eventDispatcher->dispatch(self::PRE_INSERT, new GenericEvent($productsToInsert)); $this->eventDispatcher->dispatch(self::PRE_UPDATE, new GenericEvent($productsToUpdate)); $insertDocs = $this->getDocsFromProducts($productsToInsert); $updateDocs = $this->getDocsFromProducts($productsToUpdate); if (count($insertDocs) > 0) { $this->insertDocuments($insertDocs); } if (count($updateDocs) > 0) { $this->updateDocuments($updateDocs); } $this->pendingPersister->persistPendingVersions($products); $this->eventDispatcher->dispatch(self::POST_INSERT, new GenericEvent($productsToInsert)); $this->eventDispatcher->dispatch(self::POST_UPDATE, new GenericEvent($productsToUpdate)); $this->documentManager->clear(); $this->cacheClearer->clear(); }
/** * {@inheritdoc} */ public function normalize($version, $format = null, array $context = []) { if (!$this->normalizer instanceof NormalizerInterface) { throw new \LogicException('Serializer must be a normalizer'); } $data = []; if (null !== $version->getId()) { $data[self::MONGO_ID] = $this->mongoFactory->createMongoId($version->getId()); } else { $data[self::MONGO_ID] = $this->mongoFactory->createMongoId(); } $data['author'] = $version->getAuthor(); $data['resourceName'] = $version->getResourceName(); $data['resourceId'] = (string) $version->getResourceId(); $data['snapshot'] = $version->getSnapshot(); $data['changeset'] = $version->getChangeset(); $data['context'] = $version->getContext(); $data['version'] = $version->getVersion(); $data['loggedAt'] = $this->normalizer->normalize($version->getLoggedAt(), self::FORMAT); $data['pending'] = $version->isPending(); return $data; }
/** * {@inheritdoc} */ public function normalize($dateTime, $format = null, array $context = []) { return $this->mongoFactory->createMongoDate($dateTime->getTimestamp()); }