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.

152 lines
3.1 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 function concreteExecute()
  54. {
  55. return $this->collection->find($this->condition, $this->fields);
  56. }
  57. protected function checkParams()
  58. {
  59. if (isset($this->collection) && isset($this->condition) && isset($this->fields)) {
  60. return true;
  61. } else {
  62. return false;
  63. }
  64. }
  65. }
  66. class InsertMongoCommand extends MongoDbCommand
  67. {
  68. protected $data;
  69. protected $safe;
  70. protected function concreteExecute()
  71. {
  72. return $this->collection->insert($this->data, array('safe' => $this->safe));
  73. }
  74. protected function checkParams()
  75. {
  76. if (isset($this->collection) && isset($this->data) && isset($this->safe)) {
  77. return true;
  78. } else {
  79. return false;
  80. }
  81. }
  82. }
  83. class UpdateMongoCommand extends MongoDbCommand
  84. {
  85. protected $condition;
  86. protected $data;
  87. protected $upsert;
  88. protected $safe;
  89. protected function concreteExecute()
  90. {
  91. return $this->collection->update($this->condition, $this->data, array('upsert' => $this->upsert, 'safe' => $this->safe));
  92. }
  93. protected function checkParams()
  94. {
  95. if (isset($this->collection) && isset($this->condition) && isset($this->data) &&
  96. isset($this->upsert) && isset($this->safe)
  97. ) {
  98. return true;
  99. } else {
  100. return false;
  101. }
  102. }
  103. }
  104. class RemoveMongoCommand extends MongoDbCommand
  105. {
  106. protected $condition;
  107. protected $safe;
  108. protected function concreteExecute()
  109. {
  110. return $this->collection->remove($this->condition, array('safe' => $this->safe));
  111. }
  112. protected function checkParams()
  113. {
  114. if (isset($this->collection) && isset($this->condition) && isset($this->safe)) {
  115. return true;
  116. } else {
  117. return false;
  118. }
  119. }
  120. }