Maslosoft Mangan API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/mangan
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @copyright Copyright (c) Others as mentioned in code
* @link https://maslosoft.com/mangan/
*/
namespace Maslosoft\Mangan;
use Countable;
use Iterator;
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
use Maslosoft\Mangan\Interfaces\Adapters\FinderCursorInterface;
use Maslosoft\Mangan\Transformers\RawArray;
use MongoCursor;
use UnexpectedValueException;
/**
* Cursor
*
* Cursor object, that behaves much like the MongoCursor,
* but this one returns instantiated objects
* @author Ianaré Sévi
* @author Dariusz Górecki <darek.krk@gmail.com>
* @author Invenzzia Group, open-source division of CleverIT company http://www.invenzzia.org
* @copyright 2011 CleverIT http://www.cleverit.com.pl
* @since v1.3.4
*/
class Cursor implements Iterator, Countable
{
/**
* MongoCursor returned by the query
* @var FinderCursorInterface|MongoCursor
* @since v1.3.4
*/
private $cursor;
/**
* Model used for instantiating objects
* @var AnnotatedInterface
* @since v1.3.4
*/
private $model;
/**
* Construct a new Cursor
*
* @param FinderCursorInterface|MongoCursor $cursor the cursor returned by the query
* @param AnnotatedInterface $model the model for instantiating objects
* @since v1.3.4
*/
public function __construct(Iterator $cursor, AnnotatedInterface $model)
{
assert($cursor instanceof FinderCursorInterface || $cursor instanceof MongoCursor, new UnexpectedValueException(sprintf('Expected `%s` or `%s` got `%s`', FinderCursorInterface::class, MongoCursor::class, get_class($cursor))));
$this->cursor = $cursor;
$this->model = $model;
}
/**
* Return MongoCursor for additional tuning
*
* @return FinderCursorInterface|MongoCursor the cursor used for this query
* @since v1.3.4
*/
public function getCursor()
{
return $this->cursor;
}
/**
* Return the current element
* @return AnnotatedInterface|Document|null
* @since v1.3.4
*/
public function current()
{
$document = $this->cursor->current();
if (empty($document))
{
return null;
}
return RawArray::toModel($document, $this->model);
}
/**
* Return the key of the current element
* @return scalar
* @since v1.3.4
*/
public function key()
{
return $this->cursor->key();
}
/**
* Move forward to next element
* @return void
* @since v1.3.4
*/
public function next()
{
$this->cursor->next();
}
/**
* Rewind the Iterator to the first element
* @return void
* @since v1.3.4
*/
public function rewind()
{
$this->cursor->rewind();
}
/**
* Checks if current position is valid
* @return boolean
* @since v1.3.4
*/
public function valid()
{
return $this->cursor->valid();
}
/**
* Returns the number of documents found
* {@see http://www.php.net/manual/en/mongocursor.count.php}
* @param boolean $foundOnly default FALSE
* @return integer count of documents found
* @since v1.3.4
*/
public function count($foundOnly = false)
{
return $this->cursor->count($foundOnly);
}
/**
* Apply a limit to this cursor
* {@see http://www.php.net/manual/en/mongocursor.limit.php}
* @param integer $limit new limit
* @since v1.3.4
*/
public function limit($limit)
{
$this->cursor->limit($limit);
}
/**
* Skip a $offset records
* {@see http://www.php.net/manual/en/mongocursor.skip.php}
* @param integer $offset new skip
* @since v1.3.4
*/
public function offset($offset)
{
$this->cursor->skip($offset);
}
/**
* Apply sorting directives
* {@see http://www.php.net/manual/en/mongocursor.sort.php}
* @param array $fields sorting directives
* @since v1.3.4
*/
public function sort(array $fields)
{
$this->cursor->sort($fields);
}
}
API documentation generated by ApiGen