/** * {@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; }
/** * 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; }