AstConverter.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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\Framework\GraphQl\Query\Resolver\Argument;
  8. use GraphQL\Language\AST\ListValueNode;
  9. use GraphQL\Language\AST\NodeList;
  10. use Magento\Framework\GraphQl\Query\Resolver\Argument\Filter\ClauseFactory;
  11. use Magento\Framework\GraphQl\Query\Resolver\Argument\Filter\Connective;
  12. use Magento\Framework\GraphQl\Query\Resolver\Argument\Filter\ConnectiveFactory;
  13. /**
  14. * Converts the input of an object type to a @see Connective format by using entity attributes to identify clauses
  15. */
  16. class AstConverter
  17. {
  18. /**
  19. * @var ClauseFactory
  20. */
  21. private $clauseFactory;
  22. /**
  23. * @var ConnectiveFactory
  24. */
  25. private $connectiveFactory;
  26. /**
  27. * @var FieldEntityAttributesPool
  28. */
  29. private $fieldEntityAttributesPool;
  30. /**
  31. * @param ClauseFactory $clauseFactory
  32. * @param ConnectiveFactory $connectiveFactory
  33. * @param FieldEntityAttributesPool $fieldEntityAttributesPool
  34. */
  35. public function __construct(
  36. ClauseFactory $clauseFactory,
  37. ConnectiveFactory $connectiveFactory,
  38. FieldEntityAttributesPool $fieldEntityAttributesPool
  39. ) {
  40. $this->clauseFactory = $clauseFactory;
  41. $this->connectiveFactory = $connectiveFactory;
  42. $this->fieldEntityAttributesPool = $fieldEntityAttributesPool;
  43. }
  44. /**
  45. * Get a clause from an AST
  46. *
  47. * @param string $fieldName
  48. * @param array $arguments
  49. * @return array
  50. */
  51. public function getClausesFromAst(string $fieldName, array $arguments) : array
  52. {
  53. $attributes = $this->fieldEntityAttributesPool->getEntityAttributesForEntityFromField($fieldName);
  54. $conditions = [];
  55. foreach ($arguments as $argumentName => $argument) {
  56. if (in_array($argumentName, $attributes)) {
  57. foreach ($argument as $clauseType => $clause) {
  58. if (is_array($clause)) {
  59. $value = [];
  60. foreach ($clause as $item) {
  61. $value[] = $item;
  62. }
  63. } else {
  64. $value = $clause;
  65. }
  66. $conditions[] = $this->clauseFactory->create(
  67. $argumentName,
  68. $clauseType,
  69. $value
  70. );
  71. }
  72. } else {
  73. $conditions[] =
  74. $this->connectiveFactory->create(
  75. $this->getClausesFromAst($fieldName, $argument),
  76. $argumentName
  77. );
  78. }
  79. }
  80. return $conditions;
  81. }
  82. }