/src/Tamayo/Stretchy/Query/Grammar.php

https://gitlab.com/mehdi-zarrin/Stretchy · PHP · 188 lines · 93 code · 32 blank · 63 comment · 10 complexity · 5f9265f0d2a40fdc1f638a887c72d804 MD5 · raw file

  1. <?php namespace Tamayo\Stretchy\Query;
  2. use Illuminate\Support\Str;
  3. use Tamayo\Stretchy\Query\Builder;
  4. use Tamayo\Stretchy\Query\Clauses\Clause;
  5. use Tamayo\Stretchy\Grammar as BaseGrammar;
  6. class Grammar extends BaseGrammar {
  7. /**
  8. * Compile the query.
  9. *
  10. * @param \Tamayo\Stretchy\Query\Builder $builder
  11. * @return array
  12. */
  13. public function compileSearch(Builder $builder)
  14. {
  15. $header = $this->compileHeader($builder);
  16. $singleStatement = $builder->getSingleStatement();
  17. if (isset($singleStatement)) {
  18. $body = $this->compileSingleStatement($singleStatement);
  19. }
  20. else
  21. {
  22. $body = $this->compileSubqueries($builder);
  23. }
  24. if ($builder->isSubquery()) {
  25. return $body;
  26. }
  27. return array_merge($header, ['body' => $body]);
  28. }
  29. /**
  30. * Compile a match all statement.
  31. *
  32. * @param array $matchAll
  33. * @return array
  34. */
  35. public function compileMatchAll($matchAll)
  36. {
  37. $compiled = $this->compileClause($matchAll['value']);
  38. $compiled = isset($compiled)? $compiled : new \StdClass;
  39. return $this->compile('match_all', $compiled);
  40. }
  41. /**
  42. * Compile a raw statement.
  43. *
  44. * @param array $raw
  45. * @return array
  46. */
  47. public function compileRaw($raw)
  48. {
  49. if (is_array($raw['value'])) {
  50. return $raw['value'];
  51. }
  52. return json_decode($raw['value']);
  53. }
  54. /**
  55. * Compile single statement.
  56. *
  57. * @param array $statement
  58. * @return array
  59. */
  60. protected function compileSingleStatement($statement)
  61. {
  62. $compiled = array();
  63. return $this->compile('query', $this->callCompileMethod($statement['type'], $statement));
  64. }
  65. /**
  66. * Compile a default statement.
  67. *
  68. * @param array $statement
  69. * @return array
  70. */
  71. public function compileDefaultStatement($type, $statement)
  72. {
  73. if (! isset($statement['field'])) {
  74. $compiled = $this->compileClause($statement['value']);
  75. }
  76. else {
  77. $compiled = $this->compile($statement['field'], $this->compileClause($statement['value']));
  78. }
  79. return $this->compile($type, $compiled);
  80. }
  81. /**
  82. * Compile the a clause with its contraints.
  83. *
  84. * @param \Tamayo\Stretchy\Search\Clauses\Clause $clause
  85. * @param array|null $container
  86. * @return array
  87. */
  88. protected function compileClause($clause)
  89. {
  90. $compiled = $clause->toArray();
  91. if (empty($compiled)) {
  92. return null;
  93. }
  94. return $compiled;
  95. }
  96. /**
  97. * Compile a set of subqueries statements.
  98. *
  99. * @param array $queries
  100. * @return array
  101. */
  102. protected function compileSubqueries(Builder $builder)
  103. {
  104. $compiled = array();
  105. foreach ($builder->getStatements() as $query) {
  106. $compiled = array_merge_recursive($compiled, $this->compileSubquery($builder, $query));
  107. }
  108. if(count($compiled) == 1){
  109. $compiled = array_shift($compiled);
  110. }
  111. return $compiled;
  112. }
  113. /**
  114. * Compile a single subquery statements.
  115. *
  116. * @param string $name
  117. * @return array
  118. */
  119. protected function compileSubquery($builder, $name)
  120. {
  121. $compiled = array();
  122. $container = Str::camel($name);
  123. if (isset($builder->$container)) {
  124. foreach ($builder->$container as $statement) {
  125. $compiled[] = $this->callCompileMethod($container, $statement);
  126. }
  127. }
  128. return $compiled;
  129. }
  130. /**
  131. * Check if method exists in class.
  132. *
  133. * @param string $name
  134. * @return bool
  135. */
  136. protected function methodExists($name)
  137. {
  138. return method_exists($this, $name);
  139. }
  140. /**
  141. * Call the compile method of the statement.
  142. *
  143. * @param string $type
  144. * @param mixed $statement
  145. * @return array
  146. */
  147. protected function callCompileMethod($type, $statement)
  148. {
  149. $method = 'compile'.ucfirst($type);
  150. if ($this->methodExists($method)) {
  151. return $this->$method($statement);
  152. }
  153. //If method does not exists, asume as default compilation
  154. return $this->compileDefaultStatement(Str::snake($type), $statement);
  155. }
  156. }