/panada/Drivers/Database/Mongodb.php

https://github.com/back2arie/Panada · PHP · 338 lines · 164 code · 60 blank · 114 comment · 31 complexity · 736ee105c26d0acefad3847a54ec6334 MD5 · raw file

  1. <?php
  2. /**
  3. * Panada MongoDB API.
  4. *
  5. * @package Driver
  6. * @subpackage Database
  7. * @author Iskandar Soesman
  8. * @since Version 0.2
  9. */
  10. namespace Drivers\Database;
  11. use Resources\Tools as Tools,
  12. Resources\RunException as RunException;
  13. class Mongodb extends \Mongo
  14. {
  15. private
  16. $database,
  17. $config,
  18. $connection,
  19. $criteria = array(),
  20. $documents = array();
  21. protected
  22. $limit = null,
  23. $offset = null,
  24. $order = array();
  25. public function __construct( $config, $connectionName )
  26. {
  27. /**
  28. * Makesure Mongo extension is enabled
  29. */
  30. if( ! class_exists('Mongo') )
  31. throw new RunException('Mongo PECL extension that required by Mongodb Driver is not available.');
  32. $this->config = $config;
  33. $this->connection = $connectionName;
  34. /**
  35. * $this->config['options'] is the mongodb connection option. Eg: array('replicaSet' => true, 'connect' => false)
  36. */
  37. try {
  38. parent::__construct($this->config['host'], $this->config['options']);
  39. }
  40. catch(\MongoConnectionException $e) {
  41. RunException::outputError( 'Unable connet to database in <strong>'.$connectionName.'</strong> connection.' );
  42. }
  43. }
  44. /**
  45. * Define the db name
  46. *
  47. * @param string $database
  48. * @return object
  49. */
  50. public function database($database)
  51. {
  52. $this->config['database'] = $database;
  53. return $this;
  54. }
  55. /**
  56. * Define the collection name
  57. *
  58. * @param string $database
  59. * @return object
  60. */
  61. public function collection($collection)
  62. {
  63. $database = $this->config['database'];
  64. $db = $this->$database;
  65. return $db->$collection;
  66. }
  67. /**
  68. * Return the mongodb object after
  69. * the db selected
  70. *
  71. * @return object
  72. */
  73. public function mongoObj()
  74. {
  75. return $this->selectDB($this->config['database']);
  76. }
  77. /**
  78. * Wrap results from mongo output into object or array.
  79. *
  80. * @param array $cursor The array data given from Mongodb
  81. * @param string $output The output type: object | array
  82. * @return boolean | object | array
  83. */
  84. public function cursorResults($cursor, $output = 'object')
  85. {
  86. if( ! $cursor )
  87. return false;
  88. $return = false;
  89. if( $output == 'array')
  90. return iterator_to_array($cursor);
  91. foreach ($cursor as $value)
  92. $return[] = (object) Tools::arrayToObject($value);
  93. return $return;
  94. }
  95. /**
  96. * Convert string time into mongo date
  97. *
  98. * @param string $str
  99. */
  100. public function date($str = null)
  101. {
  102. if( is_null($str) )
  103. return new \MongoDate();
  104. if( is_string($str) )
  105. return new \MongoDate(strtotime($str));
  106. if( is_int($str) )
  107. return new \MongoDate($str);
  108. }
  109. /**
  110. * Convert a string unique identifier into MongoId object.
  111. *
  112. * @param string $_id Mongodb string id
  113. * @return object
  114. */
  115. public function _id($_id = null)
  116. {
  117. return new \MongoId($_id);
  118. }
  119. /**
  120. * Method for select field(s) in a collection.
  121. *
  122. * @return object
  123. */
  124. public function select()
  125. {
  126. $documents = func_get_args();
  127. if( ! empty($documents) )
  128. $this->documents = $documents;
  129. if( is_array($documents[0]) )
  130. $this->documents = $documents[0];
  131. return $this;
  132. }
  133. /**
  134. * Get the collection name.
  135. *
  136. * @return object
  137. */
  138. public function from($collectionName)
  139. {
  140. $this->collectionName = $collectionName;
  141. return $this;
  142. }
  143. /**
  144. * Build criteria condition.
  145. * Translate SQL like operator into mongo string operator.
  146. *
  147. * @param string | array Document field
  148. * @param string SQL operator
  149. * @param string Vlaue to compare
  150. * @param string Separator for more then one condition
  151. * @return object
  152. */
  153. public function where($document, $operator = null, $value = null, $separator = false)
  154. {
  155. if( is_array($document) )
  156. $this->criteria = $document;
  157. if($operator == '=')
  158. $this->criteria[$document] = $value;
  159. if($operator == '>')
  160. $this->criteria[$document]['$gt'] = $value;
  161. if($operator == '<')
  162. $this->criteria[$document]['$lt'] = $value;
  163. if($operator == '>=')
  164. $this->criteria[$document]['$gte'] = $value;
  165. if($operator == '<=')
  166. $this->criteria[$document]['$lte'] = $value;
  167. return $this;
  168. }
  169. /**
  170. * Find more then one document
  171. *
  172. * @return mix
  173. */
  174. public function getAll( $collection = false, $criteria = array(), $fields = array() )
  175. {
  176. if( $collection )
  177. $this->collectionName = $collection;
  178. if( ! empty($criteria) )
  179. $this->criteria = $criteria;
  180. if( ! empty($fields) )
  181. $this->documents = $fields;
  182. $value = $this->collection($this->collectionName)->find( $this->criteria, $this->documents )->limit($this->limit)->skip($this->offset);
  183. if(count($this->order) > 0)
  184. $value = $value->sort($this->order);
  185. $this->criteria = $this->documents = array();
  186. if( ! empty($value) )
  187. return $this->cursorResults($value);
  188. return false;
  189. }
  190. /**
  191. * Find a document
  192. *
  193. * @return mix
  194. */
  195. public function getOne( $collection = false, $criteria = array(), $fields = array() )
  196. {
  197. if( $collection )
  198. $this->collectionName = $collection;
  199. if( ! empty($criteria) )
  200. $this->criteria = $criteria;
  201. if( ! empty($fields) )
  202. $this->documents = $fields;
  203. $value = $this->collection($this->collectionName)->findOne( $this->criteria, $this->documents );
  204. $this->criteria = $this->documents = array();
  205. if( ! empty($value) )
  206. return Tools::arrayToObject($value);
  207. return false;
  208. }
  209. /**
  210. * Insert new document
  211. *
  212. * @param string Collection name
  213. * @param array Data to insert
  214. * @return bool
  215. */
  216. public function insert($collection, $data = array())
  217. {
  218. return $this->collection($collection)->insert($data);
  219. }
  220. /**
  221. * Update document
  222. *
  223. * @param string Collection name
  224. * @param array Data to update
  225. * @param string SQL like criteria
  226. * @return bool
  227. */
  228. public function update($collection, $data, $criteria = null)
  229. {
  230. $this->where($criteria);
  231. $value = $this->collection($collection)->update( $this->criteria, array('$set' => $data) );
  232. $this->criteria = array();
  233. return $value;
  234. }
  235. /**
  236. * Delete a document
  237. *
  238. * @param string Collection name
  239. * param string SQL like criteria
  240. * @return bool
  241. */
  242. public function delete( $collection, $criteria = null )
  243. {
  244. if( ! empty($criteria) )
  245. $this->where($criteria);
  246. $value = $this->collection($collection)->remove( $this->criteria );
  247. $this->criteria = array();
  248. return $value;
  249. }
  250. /**
  251. * Order By part when finding a document
  252. *
  253. * @param string $column, $order
  254. * @return object
  255. */
  256. public function orderBy( $column, $order = 'asc' )
  257. {
  258. $order = strtolower($order);
  259. if( $order=='desc' )
  260. $order = -1;
  261. else
  262. $order = 1;
  263. $this->order[$column] = $order;
  264. return $this;
  265. }
  266. /**
  267. * Limit and Offset part when finding a document
  268. *
  269. * @param string $limit, $offset
  270. * @return object
  271. */
  272. public function limit( $limit, $offset = null )
  273. {
  274. $this->limit = $limit;
  275. $this->offset = $offset;
  276. return $this;
  277. }
  278. }