/library/Zend/Cloud/DocumentService/Query.php

https://bitbucket.org/hamidrezas/melobit · PHP · 191 lines · 79 code · 12 blank · 100 comment · 8 complexity · 432b1f682e98042e0f79d2ca46bd5ade MD5 · raw file

  1. <?php
  2. /**
  3. * LICENSE
  4. *
  5. * This source file is subject to the new BSD license that is bundled
  6. * with this package in the file LICENSE.txt.
  7. * It is also available through the world-wide-web at this URL:
  8. * http://framework.zend.com/license/new-bsd
  9. * If you did not receive a copy of the license and are unable to
  10. * obtain it through the world-wide-web, please send an email
  11. * to license@zend.com so we can send you a copy immediately.
  12. *
  13. * @category Zend
  14. * @package Zend_Cloud
  15. * @subpackage DocumentService
  16. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  17. * @license http://framework.zend.com/license/new-bsd New BSD License
  18. */
  19. require_once 'Zend/Cloud/DocumentService/QueryAdapter.php';
  20. /**
  21. * Generic query object
  22. *
  23. * Aggregates operations in an array of clauses, where the first element
  24. * describes the clause type, and the next element describes the criteria.
  25. *
  26. * @category Zend
  27. * @package Zend_Cloud
  28. * @subpackage DocumentService
  29. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Cloud_DocumentService_Query
  33. implements Zend_Cloud_DocumentService_QueryAdapter
  34. {
  35. /**
  36. * Known query types
  37. */
  38. const QUERY_SELECT = 'select';
  39. const QUERY_FROM = 'from';
  40. const QUERY_WHERE = 'where';
  41. const QUERY_WHEREID = 'whereid'; // request element by ID
  42. const QUERY_LIMIT = 'limit';
  43. const QUERY_ORDER = 'order';
  44. /**
  45. * Clause list
  46. *
  47. * @var array
  48. */
  49. protected $_clauses = array();
  50. /**
  51. * Generic clause
  52. *
  53. * You can use any clause by doing $query->foo('bar')
  54. * but concrete adapters should be able to recognise it
  55. *
  56. * The call will be iterpreted as clause 'foo' with argument 'bar'
  57. *
  58. * @param string $name Clause/method name
  59. * @param mixed $args
  60. * @return Zend_Cloud_DocumentService_Query
  61. */
  62. public function __call($name, $args)
  63. {
  64. $this->_clauses[] = array(strtolower($name), $args);
  65. return $this;
  66. }
  67. /**
  68. * SELECT clause (fields to be selected)
  69. *
  70. * @param null|string|array $select
  71. * @return Zend_Cloud_DocumentService_Query
  72. */
  73. public function select($select)
  74. {
  75. if (empty($select)) {
  76. return $this;
  77. }
  78. if (!is_string($select) && !is_array($select)) {
  79. require_once 'Zend/Cloud/DocumentService/Exception.php';
  80. throw new Zend_Cloud_DocumentService_Exception("SELECT argument must be a string or an array of strings");
  81. }
  82. $this->_clauses[] = array(self::QUERY_SELECT, $select);
  83. return $this;
  84. }
  85. /**
  86. * FROM clause
  87. *
  88. * @param string $name Field names
  89. * @return Zend_Cloud_DocumentService_Query
  90. */
  91. public function from($name)
  92. {
  93. if(!is_string($name)) {
  94. require_once 'Zend/Cloud/DocumentService/Exception.php';
  95. throw new Zend_Cloud_DocumentService_Exception("FROM argument must be a string");
  96. }
  97. $this->_clauses[] = array(self::QUERY_FROM, $name);
  98. return $this;
  99. }
  100. /**
  101. * WHERE query
  102. *
  103. * @param string $cond Condition
  104. * @param array $args Arguments to substitute instead of ?'s in condition
  105. * @param string $op relation to other clauses - and/or
  106. * @return Zend_Cloud_DocumentService_Query
  107. */
  108. public function where($cond, $value = null, $op = 'and')
  109. {
  110. if (!is_string($cond)) {
  111. require_once 'Zend/Cloud/DocumentService/Exception.php';
  112. throw new Zend_Cloud_DocumentService_Exception("WHERE argument must be a string");
  113. }
  114. $this->_clauses[] = array(self::QUERY_WHERE, array($cond, $value, $op));
  115. return $this;
  116. }
  117. /**
  118. * Select record or fields by ID
  119. *
  120. * @param string|int $value Identifier to select by
  121. * @return Zend_Cloud_DocumentService_Query
  122. */
  123. public function whereId($value)
  124. {
  125. if (!is_scalar($value)) {
  126. require_once 'Zend/Cloud/DocumentService/Exception.php';
  127. throw new Zend_Cloud_DocumentService_Exception("WHEREID argument must be a scalar");
  128. }
  129. $this->_clauses[] = array(self::QUERY_WHEREID, $value);
  130. return $this;
  131. }
  132. /**
  133. * LIMIT clause (how many items to return)
  134. *
  135. * @param int $limit
  136. * @return Zend_Cloud_DocumentService_Query
  137. */
  138. public function limit($limit)
  139. {
  140. if ($limit != (int) $limit) {
  141. require_once 'Zend/Cloud/DocumentService/Exception.php';
  142. throw new Zend_Cloud_DocumentService_Exception("LIMIT argument must be an integer");
  143. }
  144. $this->_clauses[] = array(self::QUERY_LIMIT, $limit);
  145. return $this;
  146. }
  147. /**
  148. * ORDER clause; field or fields to sort by, and direction to sort
  149. *
  150. * @param string|int|array $sort
  151. * @param string $direction
  152. * @return Zend_Cloud_DocumentService_Query
  153. */
  154. public function order($sort, $direction = 'asc')
  155. {
  156. $this->_clauses[] = array(self::QUERY_ORDER, array($sort, $direction));
  157. return $this;
  158. }
  159. /**
  160. * "Assemble" the query
  161. *
  162. * Simply returns the clauses present.
  163. *
  164. * @return array
  165. */
  166. public function assemble()
  167. {
  168. return $this->getClauses();
  169. }
  170. /**
  171. * Return query clauses as an array
  172. *
  173. * @return array Clauses in the query
  174. */
  175. public function getClauses()
  176. {
  177. return $this->_clauses;
  178. }
  179. }