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.

170 lines
3.6 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage db
  7. * @since 2011-11-15
  8. */
  9. class MongoCommandBuilder
  10. {
  11. const FIND = 'Find';
  12. const INSERT = 'Insert';
  13. const UPDATE = 'Update';
  14. const REMOVE = 'Remove';
  15. static public function factory($type, $collection = null)
  16. {
  17. $class = ucfirst($type) . 'MongoCommand';
  18. $command = new $class();
  19. $command->setCollection($collection);
  20. return $command;
  21. }
  22. }
  23. abstract class MongoDbCommand
  24. {
  25. protected $collection;
  26. public function execute()
  27. {
  28. if ($this->checkParams()) {
  29. return $this->concreteExecute();
  30. } else {
  31. throw new Exception(get_called_class() . ' error. Bind all required params first.');
  32. }
  33. }
  34. public function bindParam($name, $value)
  35. {
  36. if (property_exists($this, $name)) {
  37. $this->$name = $value;
  38. }
  39. return $this;
  40. }
  41. public function setCollection($collection)
  42. {
  43. $this->collection = $collection;
  44. return $this;
  45. }
  46. abstract protected function concreteExecute();
  47. abstract protected function checkParams();
  48. }
  49. class FindMongoCommand extends MongoDbCommand
  50. {
  51. protected $condition;
  52. protected $fields;
  53. protected $multiple = true;
  54. protected function concreteExecute()
  55. {
  56. if($this->multiple) {
  57. return $this->collection->find($this->condition, $this->fields);
  58. } else {
  59. return $this->collection->findOne($this->condition, $this->fields);
  60. }
  61. }
  62. protected function checkParams()
  63. {
  64. if (isset($this->collection) && isset($this->condition) && isset($this->fields)) {
  65. return true;
  66. } else {
  67. return false;
  68. }
  69. }
  70. }
  71. class InsertMongoCommand extends MongoDbCommand
  72. {
  73. protected $data;
  74. protected $safe = true;
  75. protected $insertId = false;
  76. protected function concreteExecute()
  77. {
  78. $result = $this->collection->insert($this->data, array('safe' => $this->safe));
  79. $this->insertId = $this->data['_id'];
  80. return $result;
  81. }
  82. protected function checkParams()
  83. {
  84. if (isset($this->collection) && isset($this->data) && isset($this->safe)) {
  85. return true;
  86. } else {
  87. return false;
  88. }
  89. }
  90. public function getInsertId()
  91. {
  92. return $this->insertId;
  93. }
  94. }
  95. class UpdateMongoCommand extends MongoDbCommand
  96. {
  97. protected $condition;
  98. protected $data;
  99. protected $multiple = true;
  100. protected $upsert = false;
  101. protected $safe = true;
  102. protected function concreteExecute()
  103. {
  104. return $this->collection->update($this->condition, $this->data,
  105. array('multiple' => $this->multiple, 'upsert' => $this->upsert, 'safe' => $this->safe));
  106. }
  107. protected function checkParams()
  108. {
  109. if (isset($this->collection) && isset($this->condition) && isset($this->data) &&
  110. isset($this->upsert) && isset($this->safe)
  111. ) {
  112. return true;
  113. } else {
  114. return false;
  115. }
  116. }
  117. }
  118. class RemoveMongoCommand extends MongoDbCommand
  119. {
  120. protected $condition;
  121. protected $safe = true;
  122. protected function concreteExecute()
  123. {
  124. return $this->collection->remove($this->condition, array('safe' => $this->safe));
  125. }
  126. protected function checkParams()
  127. {
  128. if (isset($this->collection) && isset($this->condition) && isset($this->safe)) {
  129. return true;
  130. } else {
  131. return false;
  132. }
  133. }
  134. }