EMongoCriteria.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. /**
  3. * This is the extensions version of CDbCriteria.
  4. *
  5. * This class is by no means required however it can help in your programming.
  6. *
  7. * @property array $condition
  8. * @property array $sort
  9. * @property int $skip
  10. * @property int $limit
  11. * @property array $project
  12. */
  13. class EMongoCriteria extends CComponent
  14. {
  15. /**
  16. * @var array
  17. */
  18. private $_condition = array();
  19. /**
  20. * @var array
  21. */
  22. private $_sort = array();
  23. /**
  24. * @var int
  25. */
  26. private $_skip = 0;
  27. /**
  28. * @var int
  29. */
  30. private $_limit = 0;
  31. /**
  32. * Holds information for what should be projected from the cursor
  33. * into active models. The reason for this obscure name is because this
  34. * is what it is called in MongoDB, basically it is SELECT though.
  35. * @var array
  36. */
  37. private $_project = array();
  38. /**
  39. * Constructor.
  40. * @param array $data - criteria initial property values (indexed by property name)
  41. */
  42. public function __construct($data = array())
  43. {
  44. foreach($data as $name => $value){
  45. $this->$name = $value;
  46. }
  47. }
  48. /**
  49. * Sets the condition
  50. * @param array $condition
  51. * @return EMongoCriteria
  52. */
  53. public function setCondition(array $condition=array())
  54. {
  55. $this->_condition = CMap::mergeArray($condition, $this->_condition);
  56. return $this;
  57. }
  58. /**
  59. * Gets the condition
  60. * @return array
  61. */
  62. public function getCondition()
  63. {
  64. return $this->_condition;
  65. }
  66. /**
  67. * Sets the sort
  68. * @param array $sort
  69. * @return EMongoCriteria
  70. */
  71. public function setSort(array $sort)
  72. {
  73. foreach($sort as $field => $order){
  74. if($order === 'asc'){
  75. $sort[$field] = 1;
  76. }elseif($order === 'desc'){
  77. $sort[$field] = -1;
  78. }
  79. }
  80. $this->_sort = CMap::mergeArray($sort, $this->_sort);
  81. return $this;
  82. }
  83. /**
  84. * Gets the sort
  85. * @return array
  86. */
  87. public function getSort()
  88. {
  89. return $this->_sort;
  90. }
  91. /**
  92. * Sets the skip
  93. * @param int $skip
  94. * @return EMongoCriteria
  95. */
  96. public function setSkip($skip)
  97. {
  98. $this->_skip = (int)$skip;
  99. return $this;
  100. }
  101. /**
  102. * Gets the skip
  103. * @return int
  104. */
  105. public function getSkip()
  106. {
  107. return $this->_skip;
  108. }
  109. /**
  110. * Sets the limit
  111. * @param int $limit
  112. * @return EMongoCriteria
  113. */
  114. public function setLimit($limit)
  115. {
  116. $this->_limit = (int)$limit;
  117. return $this;
  118. }
  119. /**
  120. * Gets the limit
  121. * @return int
  122. */
  123. public function getLimit()
  124. {
  125. return $this->_limit;
  126. }
  127. /**
  128. * Sets the projection (SELECT in MongoDB Lingo) of the criteria
  129. * @param array $document - The document specification for projection
  130. * @return EMongoCriteria
  131. */
  132. public function setProject($document)
  133. {
  134. $this->_project = $document;
  135. return $this;
  136. }
  137. /**
  138. * This means that the getters and setters for projection will be access like:
  139. * $c->project(array('c'=>1,'d'=>0));
  140. * @return array
  141. */
  142. public function getProject()
  143. {
  144. return $this->_project;
  145. }
  146. /**
  147. * An alias for those too used to select
  148. * @see EMongoCriteria::setProject()
  149. * @param array $document
  150. * @return EMongoCriteria
  151. */
  152. public function setSelect($document)
  153. {
  154. return $this->setProject($document);
  155. }
  156. /**
  157. * An alias for those too used to select
  158. * @see EMongoCriteria::getProject()
  159. * @return array
  160. */
  161. public function getSelect()
  162. {
  163. return $this->getProject();
  164. }
  165. /**
  166. * Append condition to previous ones using the column name as the index
  167. * This will overwrite columns of the same name
  168. * @param string $column
  169. * @param mixed $value
  170. * @param string $operator
  171. * @return EMongoCriteria
  172. */
  173. public function addCondition($column, $value, $operator = null)
  174. {
  175. $this->_condition[$column] = $operator === null ? $value : array($operator => $value);
  176. return $this;
  177. }
  178. /**
  179. * Adds an $or condition to the criteria, will overwrite other $or conditions
  180. * @param array $condition
  181. * @return EMongoCriteria
  182. */
  183. public function addOrCondition($condition)
  184. {
  185. $this->_condition['$and'][] = array('$or' => $condition);
  186. return $this;
  187. }
  188. /**
  189. * Base search functionality
  190. * @param string $column
  191. * @param string|null $value
  192. * @param boolean $partialMatch
  193. * @return EMongoCriteria
  194. */
  195. public function compare($column, $value = null, $partialMatch = false)
  196. {
  197. $query = array();
  198. if($value === null){
  199. $query[$column] = null;
  200. }elseif(is_array($value)){
  201. $query[$column] = array('$in' => $value);
  202. }elseif(is_object($value)){
  203. $query[$column] = $value;
  204. }elseif(is_bool($value)){
  205. $query[$column] = $value;
  206. }elseif(preg_match('/^(?:\s*(<>|<=|>=|<|>|=))?(.*)$/', $value, $matches)){
  207. $value = $matches[2];
  208. $op = $matches[1];
  209. if($partialMatch === true){
  210. $value = new MongoRegex("/$value/i");
  211. }else{
  212. if(
  213. !is_bool($value) && !is_array($value) && preg_match('/^([0-9]|[1-9]{1}\d+)$/' /* Will only match real integers, unsigned */, $value) > 0
  214. && (
  215. (PHP_INT_MAX > 2147483647 && (string)$value < '9223372036854775807') /* If it is a 64 bit system and the value is under the long max */
  216. || (string)$value < '2147483647' /* value is under 32bit limit */
  217. )
  218. ){
  219. $value = (int)$value;
  220. }
  221. }
  222. switch($op){
  223. case "<>":
  224. $query[$column] = array('$ne' => $value);
  225. break;
  226. case "<=":
  227. $query[$column] = array('$lte' => $value);
  228. break;
  229. case ">=":
  230. $query[$column] = array('$gte' => $value);
  231. break;
  232. case "<":
  233. $query[$column] = array('$lt' => $value);
  234. break;
  235. case ">":
  236. $query[$column] = array('$gt' => $value);
  237. break;
  238. case "=":
  239. default:
  240. $query[$column] = $value;
  241. break;
  242. }
  243. }
  244. if(!$query){
  245. $query[$column] = $value;
  246. }
  247. $this->addCondition($column, $query[$column]);
  248. return $this;
  249. }
  250. /**
  251. * Merges either an array of criteria or another criteria object with this one
  252. * @param array|EMongoCriteria $criteria
  253. * @return EMongoCriteria
  254. */
  255. public function mergeWith($criteria)
  256. {
  257. if($criteria instanceof EMongoCriteria){
  258. return $this->mergeWith($criteria->toArray());
  259. }
  260. if(is_array($criteria)){
  261. if(isset($criteria['condition']) && is_array($criteria['condition'])){
  262. $this->setCondition(CMap::mergeArray($this->condition, $criteria['condition']));
  263. }
  264. if(isset($criteria['sort']) && is_array($criteria['sort'])){
  265. $this->setSort(CMap::mergeArray($this->sort, $criteria['sort']));
  266. }
  267. if(isset($criteria['skip']) && is_numeric($criteria['skip'])){
  268. $this->setSkip($criteria['skip']);
  269. }
  270. if(isset($criteria['limit']) && is_numeric($criteria['limit'])){
  271. $this->setLimit($criteria['limit']);
  272. }
  273. if(isset($criteria['project']) && is_array($criteria['project'])){
  274. $this->setProject(CMap::mergeArray($this->project, $criteria['project']));
  275. }
  276. }
  277. return $this;
  278. }
  279. /**
  280. * @param boolean $onlyCondition - indicates whether to return only condition part or criteria.
  281. * Should be "true" if the criteria is used in EMongoDocument::find() and other common find methods.
  282. * @return array - native representation of the criteria
  283. */
  284. public function toArray($onlyCondition = false)
  285. {
  286. $result = array();
  287. if($onlyCondition === true){
  288. $result = $this->condition;
  289. }else{
  290. foreach(array('_condition', '_limit', '_skip', '_sort', '_project') as $name){
  291. $result[substr($name, 1)] = $this->$name;
  292. }
  293. }
  294. return $result;
  295. }
  296. }