Stringify.Class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php // vim:set ts=4 sw=4 et:
  2. namespace Mall\Framework\SearchClient\DSL;
  3. /**
  4. * This file is part of the ElasticSearch PHP client
  5. *
  6. * (c) Raymond Julin <raymond.julin@gmail.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * Parse a DSL object into a string based representation
  13. * Return string representation of DSL for search.
  14. * This will remove certain fields that are not supported
  15. * in a string representation
  16. *
  17. * @author Raymond Julin <raymond.julin@gmail.com>
  18. * @package ElasticSearch
  19. * @since 0.1
  20. * Created: 2010-07-24
  21. */
  22. class Stringify {
  23. protected $dsl = array();
  24. public function __construct(array $dsl) {
  25. $this->dsl = $dsl;
  26. }
  27. public function __toString() {
  28. $dsl = $this->dsl;
  29. $query = $dsl['query'];
  30. $string = "";
  31. if (array_key_exists("term", $query))
  32. $string .= $this->transformDSLTermToString($query['term']);
  33. if (array_key_exists("wildcard", $query))
  34. $string .= $this->transformDSLTermToString($query['wildcard']);
  35. if (array_key_exists("sort", $dsl))
  36. $string .= $this->transformDSLSortToString($dsl['sort']);
  37. if (array_key_exists("fields", $dsl))
  38. $string .= $this->transformDSLFieldsToString($dsl['fields']);
  39. return $string;
  40. }
  41. /**
  42. * A naive transformation of possible term and wildcard arrays in a DSL
  43. * query
  44. *
  45. * @return string
  46. * @param mixed $dslTerm
  47. */
  48. protected function transformDSLTermToString($dslTerm) {
  49. $string = "";
  50. if (is_array($dslTerm)) {
  51. $key = key($dslTerm);
  52. $value = $dslTerm[$key];
  53. if (is_string($key))
  54. $string .= "$key:";
  55. }
  56. else
  57. $value = $dslTerm;
  58. /**
  59. * If a specific key is used as key in the array
  60. * this should translate to searching in a specific field (field:term)
  61. */
  62. if (strpos($value, " ") !== false)
  63. $string .= '"' . $value . '"';
  64. else
  65. $string .= $value;
  66. return $string;
  67. }
  68. /**
  69. * Transform search parameters to string
  70. *
  71. * @return string
  72. * @param mixed $dslSort
  73. */
  74. protected function transformDSLSortToString($dslSort) {
  75. $string = "";
  76. if (is_array($dslSort)) {
  77. foreach ($dslSort as $sort) {
  78. if (is_array($sort)) {
  79. $field = key($sort);
  80. $info = current($sort);
  81. }
  82. else
  83. $field = $sort;
  84. $string .= "&sort=" . $field;
  85. if (isset($info)) {
  86. if (is_string($info) && $info == "desc")
  87. $string .= ":reverse";
  88. elseif (is_array($info) && array_key_exists("reverse", $info) && $info['reverse'])
  89. $string .= ":reverse";
  90. }
  91. }
  92. }
  93. return $string;
  94. }
  95. /**
  96. * Transform a selection of fields to return to string form
  97. *
  98. * @return string
  99. * @param mixed $dslFields
  100. */
  101. protected function transformDSLFieldsToString($dslFields) {
  102. $string = "";
  103. if (is_array($dslFields))
  104. $string .= "&fields=" . join(",", $dslFields);
  105. return $string;
  106. }
  107. }