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.

120 lines
2.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)
  16. {
  17. $class = ucfirst($type) . 'MongoCommand';
  18. return new $class();
  19. }
  20. }
  21. abstract class MongoDbCommand
  22. {
  23. public function execute()
  24. {
  25. if ($this->checkParams()) {
  26. return $this->concreteExecute();
  27. } else {
  28. throw new Exception(get_called_class() . ' error. Bind all required params first.');
  29. }
  30. }
  31. public function bindParam($name, $value)
  32. {
  33. $this->$name = $value;
  34. return $this;
  35. }
  36. abstract protected function concreteExecute();
  37. abstract protected function checkParams();
  38. }
  39. class FindMongoCommand extends MongoDbCommand
  40. {
  41. protected function concreteExecute()
  42. {
  43. return $this->collection->find($this->condition, $this->fields);
  44. }
  45. protected function checkParams()
  46. {
  47. if (isset($this->collection) && isset($this->condition) && isset($this->fields)) {
  48. return true;
  49. } else {
  50. return false;
  51. }
  52. }
  53. }
  54. class InsertMongoCommand extends MongoDbCommand
  55. {
  56. protected function concreteExecute()
  57. {
  58. return $this->collection->insert($this->data, array('safe' => $this->safe));
  59. }
  60. protected function checkParams()
  61. {
  62. if (isset($this->collection) && isset($this->data) && isset($this->safe)) {
  63. return true;
  64. } else {
  65. return false;
  66. }
  67. }
  68. }
  69. class UpdateMongoCommand extends MongoDbCommand
  70. {
  71. protected function concreteExecute()
  72. {
  73. return $this->collection->update($this->condition, $this->data, array('upsert' => $this->upsert, 'safe' => $this->safe));
  74. }
  75. protected function checkParams()
  76. {
  77. if (isset($this->collection) && isset($this->condition) && isset($this->data) &&
  78. isset($this->upsert) && isset($this->safe)
  79. ) {
  80. return true;
  81. } else {
  82. return false;
  83. }
  84. }
  85. }
  86. class RemoveMongoCommand extends MongoDbCommand
  87. {
  88. protected function concreteExecute()
  89. {
  90. return $this->collection->remove($this->condition, array('safe' => $this->safe));
  91. }
  92. protected function checkParams()
  93. {
  94. if (isset($this->collection) && isset($this->condition) && isset($this->safe)) {
  95. return true;
  96. } else {
  97. return false;
  98. }
  99. }
  100. }