IndexNameBuilder.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventoryMultiDimensionalIndexerApi\Model;
  8. use Magento\Framework\ObjectManagerInterface;
  9. /**
  10. * Index Name builder. It is Facade for simplifying IndexName object creation
  11. *
  12. * @api
  13. */
  14. class IndexNameBuilder
  15. {
  16. /**
  17. * Index id parameter name. Used internally in this object
  18. *
  19. * Can not replace on private constant (feature of PHP 7.1) because we need to support PHP 7.0
  20. */
  21. private static $indexId = 'indexId';
  22. /**
  23. * Dimensions parameter name. Used internally in this object
  24. *
  25. * Can not replace on private constant (feature of PHP 7.1) because we need to support PHP 7.0
  26. */
  27. private static $dimensions = 'dimensions';
  28. /**
  29. * Alias parameter name. Used internally in this object
  30. *
  31. * Can not replace on private constant (feature of PHP 7.1) because we need to support PHP 7.0
  32. */
  33. private static $alias = 'alias';
  34. /**
  35. * @var ObjectManagerInterface
  36. */
  37. private $objectManager;
  38. /**
  39. * @var DimensionFactory
  40. */
  41. private $dimensionFactory;
  42. /**
  43. * @var AliasFactory
  44. */
  45. private $aliasFactory;
  46. /**
  47. * @var array
  48. */
  49. private $data = [];
  50. /**
  51. * @param ObjectManagerInterface $objectManager
  52. * @param DimensionFactory $dimensionFactory
  53. * @param AliasFactory $aliasFactory
  54. */
  55. public function __construct(
  56. ObjectManagerInterface $objectManager,
  57. DimensionFactory $dimensionFactory,
  58. AliasFactory $aliasFactory
  59. ) {
  60. $this->objectManager = $objectManager;
  61. $this->dimensionFactory = $dimensionFactory;
  62. $this->aliasFactory = $aliasFactory;
  63. }
  64. /**
  65. * @param string $indexId
  66. * @return self
  67. */
  68. public function setIndexId(string $indexId): self
  69. {
  70. $this->data[self::$indexId] = $indexId;
  71. return $this;
  72. }
  73. /**
  74. * @param string $name
  75. * @param string $value
  76. * @return self
  77. */
  78. public function addDimension(string $name, string $value): self
  79. {
  80. $this->data[self::$dimensions][] = $this->dimensionFactory->create([
  81. 'name' => $name,
  82. 'value' => $value,
  83. ]);
  84. return $this;
  85. }
  86. /**
  87. * @param string $alias
  88. * @return self
  89. */
  90. public function setAlias(string $alias): self
  91. {
  92. $this->data[self::$alias] = $this->aliasFactory->create(['value' => $alias]);
  93. return $this;
  94. }
  95. /**
  96. * @return IndexName
  97. */
  98. public function build(): IndexName
  99. {
  100. $indexName = $this->objectManager->create(IndexName::class, $this->data);
  101. $this->data = [];
  102. return $indexName;
  103. }
  104. }