BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sphinxqlresult.class.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. #declare(strict_types=1);
  3. class SphinxqlResult
  4. {
  5. private $Result;
  6. private $Meta;
  7. public $Errno;
  8. public $Error;
  9. /**
  10. * Create Sphinxql result object
  11. *
  12. * @param mysqli_result $Result query results
  13. * @param array $Meta meta data for the query
  14. * @param int $Errno error code returned by the query upon failure
  15. * @param string $Error error message returned by the query upon failure
  16. */
  17. public function __construct($Result, $Meta, $Errno, $Error)
  18. {
  19. $this->Result = $Result;
  20. $this->Meta = $Meta;
  21. $this->Errno = $Errno;
  22. $this->Error = $Error;
  23. }
  24. /**
  25. * Redirect to the Mysqli result object if a nonexistent method is called
  26. *
  27. * @param string $Name method name
  28. * @param array $Arguments arguments used in the function call
  29. * @return whatever the parent function returns
  30. */
  31. public function __call($Name, $Arguments)
  32. {
  33. return call_user_func_array(array($this->Result, $Name), $Arguments);
  34. }
  35. /**
  36. * Did the query find anything?
  37. *
  38. * @return bool results were found
  39. */
  40. public function has_results()
  41. {
  42. return $this->get_meta('total') > 0;
  43. }
  44. /**
  45. * Collect and return the specified key of all results as a list
  46. *
  47. * @param string $Key key containing the desired data
  48. * @return array with the $Key value of all results
  49. */
  50. public function collect($Key)
  51. {
  52. $Return = [];
  53. while ($Row = $this->fetch_array()) {
  54. $Return[] = $Row[$Key];
  55. }
  56. $this->data_seek(0);
  57. return $Return;
  58. }
  59. /**
  60. * Collect and return all available data for the matches optionally indexed by a specified key
  61. *
  62. * @param string $Key key to use as indexing value
  63. * @param string $ResultType method to use when fetching data from the mysqli_result object. Default is MYSQLI_ASSOC
  64. * @return array with all available data for the matches
  65. */
  66. public function to_array($Key, $ResultType = MYSQLI_ASSOC)
  67. {
  68. $Return = [];
  69. while ($Row = $this->fetch_array($ResultType)) {
  70. if ($Key !== false) {
  71. $Return[$Row[$Key]] = $Row;
  72. } else {
  73. $Return[] = $Row;
  74. }
  75. }
  76. $this->data_seek(0);
  77. return $Return;
  78. }
  79. /**
  80. * Collect pairs of keys for all matches
  81. *
  82. * @param string $Key1 key to use as indexing value
  83. * @param string $Key2 key to use as value
  84. * @return array with $Key1 => $Key2 pairs for matches
  85. */
  86. public function to_pair($Key1, $Key2)
  87. {
  88. $Return = [];
  89. while ($Row = $this->fetch_array()) {
  90. $Return[$Row[$Key1]] = $Row[$Key2];
  91. }
  92. $this->data_seek(0);
  93. return $Return;
  94. }
  95. /**
  96. * Return specified portions of the current Sphinxql result object's meta data
  97. *
  98. * @param mixed $Keys scalar or array with keys to return. Default is false, which returns all meta data
  99. * @return array with meta data
  100. */
  101. public function get_meta($Keys = false)
  102. {
  103. if ($Keys !== false) {
  104. if (is_array($Keys)) {
  105. $Return = [];
  106. foreach ($Keys as $Key) {
  107. if (!isset($this->Meta[$Key])) {
  108. continue;
  109. }
  110. $Return[$Key] = $this->Meta[$Key];
  111. }
  112. return $Return;
  113. } else {
  114. return isset($this->Meta[$Keys]) ? $this->Meta[$Keys] : false;
  115. }
  116. } else {
  117. return $this->Meta;
  118. }
  119. }
  120. /**
  121. * Return specified portions of the current Mysqli result object's information
  122. *
  123. * @param mixed $Keys scalar or array with keys to return. Default is false, which returns all available information
  124. * @return array with result information
  125. */
  126. public function get_result_info($Keys = false)
  127. {
  128. if ($Keys !== false) {
  129. if (is_array($Keys)) {
  130. $Return = [];
  131. foreach ($Keys as $Key) {
  132. if (!isset($this->Result->$Key)) {
  133. continue;
  134. }
  135. $Return[$Key] = $this->Result->$Key;
  136. }
  137. return $Return;
  138. } else {
  139. return isset($this->Result->$Keys) ? $this->Result->$Keys : false;
  140. }
  141. } else {
  142. return $this->Result;
  143. }
  144. }
  145. }