Binder.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Search\Request;
  7. /**
  8. * @api
  9. * @since 100.0.2
  10. */
  11. class Binder
  12. {
  13. /**
  14. * Bind data to request data
  15. *
  16. * @param array $requestData
  17. * @param array $bindData
  18. * @return array
  19. */
  20. public function bind(array $requestData, array $bindData)
  21. {
  22. $data = $this->processLimits($requestData, $bindData);
  23. $data['dimensions'] = $this->processDimensions($requestData['dimensions'], $bindData['dimensions']);
  24. $data['queries'] = $this->processData($requestData['queries'], $bindData['placeholder']);
  25. $data['filters'] = $this->processData($requestData['filters'], $bindData['placeholder']);
  26. $data['aggregations'] = $this->processData($requestData['aggregations'], $bindData['placeholder']);
  27. return $data;
  28. }
  29. /**
  30. * Replace bind limits
  31. *
  32. * @param array $data
  33. * @param array $bindData
  34. * @return array
  35. */
  36. private function processLimits($data, $bindData)
  37. {
  38. $limitList = ['from', 'size'];
  39. foreach ($limitList as $limit) {
  40. if (isset($bindData[$limit])) {
  41. $data[$limit] = $bindData[$limit];
  42. }
  43. }
  44. return $data;
  45. }
  46. /**
  47. * @param array $data
  48. * @param array $bindData
  49. * @return array
  50. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  51. */
  52. private function processDimensions($data, $bindData)
  53. {
  54. foreach ($data as $name => $value) {
  55. if (isset($bindData[$name])) {
  56. $data[$name]['value'] = $bindData[$name];
  57. }
  58. }
  59. return $data;
  60. }
  61. /**
  62. * Replace data recursive
  63. *
  64. * @param array $data
  65. * @param array $bindData
  66. * @return array
  67. */
  68. private function processData($data, $bindData)
  69. {
  70. array_walk_recursive($bindData, function (&$item) {
  71. $item = trim($item);
  72. });
  73. $bindData = array_filter($bindData, function ($element) {
  74. return is_array($element) ? count($element) : strlen($element);
  75. });
  76. foreach ($data as $key => $value) {
  77. if (is_array($value)) {
  78. $data[$key] = $this->processData($value, $bindData);
  79. } else {
  80. foreach ($bindData as $bindKey => $bindValue) {
  81. if (strpos($value, $bindKey) !== false) {
  82. if (is_string($bindValue)) {
  83. $data[$key] = str_replace($bindKey, $bindValue, $value);
  84. } else {
  85. $data[$key] = $bindValue;
  86. }
  87. $data['is_bind'] = true;
  88. }
  89. }
  90. }
  91. }
  92. return $data;
  93. }
  94. }