MongoCommandCursor.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. */
  15. if (class_exists('MongoCommandCursor', false)) {
  16. return;
  17. }
  18. use Alcaeus\MongoDbAdapter\AbstractCursor;
  19. use Alcaeus\MongoDbAdapter\TypeConverter;
  20. class MongoCommandCursor extends AbstractCursor implements MongoCursorInterface
  21. {
  22. /**
  23. * @var array
  24. */
  25. private $command;
  26. /**
  27. * MongoCommandCursor constructor.
  28. * @param MongoClient $connection
  29. * @param string $ns
  30. * @param array $command
  31. */
  32. public function __construct(MongoClient $connection, $ns, array $command = [])
  33. {
  34. parent::__construct($connection, $ns);
  35. $this->command = $command;
  36. }
  37. /**
  38. * @param MongoClient $connection
  39. * @param string $hash
  40. * @param array $document
  41. * @return MongoCommandCursor
  42. */
  43. public static function createFromDocument(MongoClient $connection, $hash, array $document)
  44. {
  45. throw new \Exception('Not implemented');
  46. }
  47. /**
  48. * @return \MongoDB\Driver\Cursor
  49. */
  50. protected function ensureCursor()
  51. {
  52. if ($this->cursor === null) {
  53. $convertedCommand = TypeConverter::fromLegacy($this->command);
  54. if (isset($convertedCommand->cursor)) {
  55. if ($convertedCommand->cursor === true || $convertedCommand->cursor === []) {
  56. $convertedCommand->cursor = new \stdClass();
  57. }
  58. }
  59. $this->cursor = $this->db->command($convertedCommand, $this->getOptions());
  60. }
  61. return $this->cursor;
  62. }
  63. /**
  64. * @return array
  65. */
  66. protected function getCursorInfo()
  67. {
  68. return [
  69. 'ns' => $this->ns,
  70. 'limit' => 0,
  71. 'batchSize' => $this->batchSize,
  72. 'skip' => 0,
  73. 'flags' => 0,
  74. 'query' => $this->command,
  75. 'fields' => null,
  76. ];
  77. }
  78. /**
  79. * @return array
  80. */
  81. protected function getIterationInfo()
  82. {
  83. $iterationInfo = parent::getIterationInfo();
  84. if ($iterationInfo['started_iterating']) {
  85. $iterationInfo += [
  86. 'firstBatchAt' => $iterationInfo['at'],
  87. 'firstBatchNumReturned' => $iterationInfo['numReturned'],
  88. ];
  89. $iterationInfo['at'] = 0;
  90. $iterationInfo['numReturned'] = 0;
  91. }
  92. return $iterationInfo;
  93. }
  94. /**
  95. * @return array
  96. */
  97. public function __sleep()
  98. {
  99. return ['command'] + parent::__sleep();
  100. }
  101. }