MongoCommandCursor.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. use Alcaeus\MongoDbAdapter\AbstractCursor;
  16. use Alcaeus\MongoDbAdapter\TypeConverter;
  17. class MongoCommandCursor extends AbstractCursor implements MongoCursorInterface
  18. {
  19. /**
  20. * @var array
  21. */
  22. private $command;
  23. /**
  24. * MongoCommandCursor constructor.
  25. * @param MongoClient $connection
  26. * @param string $ns
  27. * @param array $command
  28. */
  29. public function __construct(MongoClient $connection, $ns, array $command = [])
  30. {
  31. parent::__construct($connection, $ns);
  32. $this->command = $command;
  33. }
  34. /**
  35. * @param MongoClient $connection
  36. * @param string $hash
  37. * @param array $document
  38. * @return MongoCommandCursor
  39. */
  40. public static function createFromDocument(MongoClient $connection, $hash, array $document)
  41. {
  42. throw new \Exception('Not implemented');
  43. }
  44. /**
  45. * @return \MongoDB\Driver\Cursor
  46. */
  47. protected function ensureCursor()
  48. {
  49. if ($this->cursor === null) {
  50. $convertedCommand = TypeConverter::fromLegacy($this->command);
  51. if (isset($convertedCommand->cursor)) {
  52. if ($convertedCommand->cursor === true || $convertedCommand->cursor === []) {
  53. $convertedCommand->cursor = new \stdClass();
  54. }
  55. }
  56. $this->cursor = $this->db->command($convertedCommand, $this->getOptions());
  57. }
  58. return $this->cursor;
  59. }
  60. /**
  61. * @return array
  62. */
  63. protected function getCursorInfo()
  64. {
  65. return [
  66. 'ns' => $this->ns,
  67. 'limit' => 0,
  68. 'batchSize' => $this->batchSize,
  69. 'skip' => 0,
  70. 'flags' => 0,
  71. 'query' => $this->command,
  72. 'fields' => null,
  73. ];
  74. }
  75. /**
  76. * @return array
  77. */
  78. protected function getIterationInfo()
  79. {
  80. $iterationInfo = parent::getIterationInfo();
  81. if ($iterationInfo['started_iterating']) {
  82. $iterationInfo += [
  83. 'firstBatchAt' => $iterationInfo['at'],
  84. 'firstBatchNumReturned' => $iterationInfo['numReturned'],
  85. ];
  86. $iterationInfo['at'] = 0;
  87. $iterationInfo['numReturned'] = 0;
  88. }
  89. return $iterationInfo;
  90. }
  91. }