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.

161 lines
3.4 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 function concreteExecute()
  76. {
  77. return $this->collection->insert($this->data, array('safe' => $this->safe));
  78. }
  79. protected function checkParams()
  80. {
  81. if (isset($this->collection) && isset($this->data) && isset($this->safe)) {
  82. return true;
  83. } else {
  84. return false;
  85. }
  86. }
  87. }
  88. class UpdateMongoCommand extends MongoDbCommand
  89. {
  90. protected $condition;
  91. protected $data;
  92. protected $multiple = true;
  93. protected $upsert = false;
  94. protected $safe = true;
  95. protected function concreteExecute()
  96. {
  97. return $this->collection->update($this->condition, $this->data,
  98. array('multiple' => $this->multiple, 'upsert' => $this->upsert, 'safe' => $this->safe));
  99. }
  100. protected function checkParams()
  101. {
  102. if (isset($this->collection) && isset($this->condition) && isset($this->data) &&
  103. isset($this->upsert) && isset($this->safe)
  104. ) {
  105. return true;
  106. } else {
  107. return false;
  108. }
  109. }
  110. }
  111. class RemoveMongoCommand extends MongoDbCommand
  112. {
  113. protected $condition;
  114. protected $safe = true;
  115. protected function concreteExecute()
  116. {
  117. return $this->collection->remove($this->condition, array('safe' => $this->safe));
  118. }
  119. protected function checkParams()
  120. {
  121. if (isset($this->collection) && isset($this->condition) && isset($this->safe)) {
  122. return true;
  123. } else {
  124. return false;
  125. }
  126. }
  127. }