DocumentFactory.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Search\Adapter\Mysql;
  7. use Magento\Framework\Api\AttributeInterface;
  8. use Magento\Framework\Api\AttributeValue;
  9. use Magento\Framework\Api\CustomAttributesDataInterface;
  10. use Magento\Framework\Api\Search\Document;
  11. use Magento\Framework\Api\Search\DocumentInterface;
  12. /**
  13. * Document Factory
  14. *
  15. * @api
  16. * @deprecated 102.0.0
  17. * @see \Magento\ElasticSearch
  18. * @since 100.0.2
  19. */
  20. class DocumentFactory
  21. {
  22. /**
  23. * Object Manager instance
  24. *
  25. * @var \Magento\Framework\ObjectManagerInterface
  26. * @deprecated 100.1.0
  27. */
  28. protected $objectManager;
  29. /**
  30. * @var \Magento\Framework\Search\EntityMetadata
  31. */
  32. private $entityMetadata;
  33. /**
  34. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  35. * @param \Magento\Framework\Search\EntityMetadata $entityMetadata
  36. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  37. */
  38. public function __construct(
  39. \Magento\Framework\ObjectManagerInterface $objectManager,
  40. \Magento\Framework\Search\EntityMetadata $entityMetadata
  41. ) {
  42. $this->entityMetadata = $entityMetadata;
  43. }
  44. /**
  45. * Create Search Document instance
  46. *
  47. * @param mixed $rawDocument
  48. * @return \Magento\Framework\Api\Search\Document
  49. */
  50. public function create($rawDocument)
  51. {
  52. $documentId = null;
  53. $entityId = $this->entityMetadata->getEntityId();
  54. $attributes = [];
  55. foreach ($rawDocument as $fieldName => $value) {
  56. if ($fieldName === $entityId) {
  57. $documentId = $value;
  58. } else {
  59. $attributes[$fieldName] = new AttributeValue(
  60. [
  61. AttributeInterface::ATTRIBUTE_CODE => $fieldName,
  62. AttributeInterface::VALUE => $value,
  63. ]
  64. );
  65. }
  66. }
  67. return new Document(
  68. [
  69. DocumentInterface::ID => $documentId,
  70. CustomAttributesDataInterface::CUSTOM_ATTRIBUTES => $attributes,
  71. ]
  72. );
  73. }
  74. }