\OntologyWrapperMongoIterator

Mongo iterator object

This concrete class derived from {@link ObjectIterator} implements a query iterator which uses a {@link MongoCursor} instance as the object cursor.

Summary

Methods
Properties
Constants
__construct()
count()
affectedCount()
keys()
skip()
limit()
fields()
sort()
setTimeout()
cursor()
collection()
criteria()
resultType()
setKeyOffset()
current()
key()
next()
rewind()
valid()
No public properties found
No constants found
shapeResult()
$mCursor
$mCollection
$mCriteria
$mFields
$mKey
$mResultType
N/A
No private methods found
No private properties found
N/A

Properties

$mCursor

$mCursor : \OntologyWrapper\Iterator

Cursor.

This data member holds the query cursor.

Type

\OntologyWrapper\Iterator

$mCriteria

$mCriteria : array

Criteria.

This data member holds the query criteria.

Type

array

$mFields

$mFields : array

Fields.

This data member holds the selection fields.

Type

array

$mKey

$mKey : string

Key.

This data member holds the iterator key field.

Type

string

$mResultType

$mResultType : \OntologyWrapper\bitfield

Result.

This data member holds an enumerated value determining what the iterator should return:

  • {@link kQUERY_OBJECT}: Return the matched object.
  • {@link kQUERY_ARRAY}: Return the matched object array value.
  • {@link kQUERY_NID}: Return the matched object native identifier.

Type

\OntologyWrapper\bitfield

Methods

__construct()

__construct(\OntologyWrapper\Iterator $theCursor, \OntologyWrapper\CollectionObject $theCollection, array $theCriteria, array $theFields, mixed $theKey, \OntologyWrapper\bitfield $theResult)

Instantiate class.

The constructor expects the following parameters:

  • $theCursor: The query result cursor.
  • $theCollection: The collection to which the query was applied.
  • $theCriteria: The query filter.
  • $theFields: The query fields.
  • $theKey: The iterator key.
  • $theResult: A bitfield value determining what kind of data the iterator will return:
    • {@link kQUERY_OBJECT}: Return the matched object.
    • {@link kQUERY_ARRAY}: Return the matched object array value.
    • {@link kQUERY_NID}: Return the matched object native identifier.

Parameters

\OntologyWrapper\Iterator $theCursor

Query cursor.

\OntologyWrapper\CollectionObject $theCollection

Query collection.

array $theCriteria

Query criteria.

array $theFields

Query fields.

mixed $theKey

Iterator key.

\OntologyWrapper\bitfield $theResult

Result type.

count()

count() : int

<h4>Return cursor count</h4>

We act as a proxy for the object's cursor, note that we should get the count including eventual limits and skip.

In this class we return the cursor count.

Returns

int —

Element count including skip and limits.

affectedCount()

affectedCount() : int

<h4>Return affected count</h4>

Return the total count excluding eventual limits and skip.

Note that the cursor must implement the Countable interface in which the count() method should return the count including skip and limits.

Returns

int —

Element count excluding skip and limits.

keys()

keys() : array

<h4>Return the cursor keys</h4>

This method will return the keys of the cursor as an array.

Returns

array

skip()

skip(integer $theCount) : integer

<h4>Skip a number of elements</h4>

This method can be used to skip a number of records, if you provide an integer, the iterator will start from the element corresponding to the provided value; in this case the method should be used only before the cursor has beed iterated.

If you provide NULL, the method should return the current skip value.

The method will return the current skip value.

Parameters

integer $theCount

Number of elements to skip.

Returns

integer —

Current skip value.

limit()

limit(integer $theCount) : integer

<h4>Limit the number of elements</h4>

This method can be used to provide the maximum number of records to be returned, if you provide an integer, the iterator will limit its results to the number corresponding to the provided value; in this case the method should be used only before the cursor has beed iterated.

If you provide NULL, the method should return the current limit value.

The method will return the current limit value.

Parameters

integer $theCount

Maximum number of iterations.

Returns

integer —

Current limit value.

fields()

fields(array $theFields) : array

<h4>Select the fields to be returned</h4>

This method should be used only before the cursor has beed iterated, it will indicate which fields the cursor should return according to the provided array parameter:

  • key: The key corresponds to the field to be selected.
  • value: Whether to include or exclude it:
    • TRUE: Include the field and exclude all others.
    • FALSE: Exclude the field and include all others.

If the cursor has already started iterating, the method should raise an exception.

If you provide NULL, the method will return the current fields selection.

The method will return the current fields selection.

Parameters

array $theFields

Fields selection.

Returns

array —

Current fields selection.

sort()

sort(array $theOrder) : array

<h4>Sort the cursor</h4>

This method should be used only before the cursor has beed iterated, it will sort the cursor elements according to the provided array parameter:

  • key: The key corresponds to the field to be sorted.
  • value: The direction in which to sort:
    • 1: Ascending.
    • -1: Descending.

The sort is also determined by the order in which the array elements are provided.

If the cursor has already started iterating, the method should raise an exception.

Parameters

array $theOrder

Sort order indications.

Returns

array —

Current sort order.

setTimeout()

setTimeout(int $theTimeout)

<h4>Set cursor timeout</h4>

This method should be used to set the cursor timeout, the method should be available also once the cursor has been iterated.

The method accepts a single parameter indicating the timeout in milliseconds.

Parameters

int $theTimeout

Timeout in milliseconds.

cursor()

cursor() : \OntologyWrapper\Iterator

<h4>Return cursor</h4>

This method can be used to retrieve the current object's cursor, which is the native cursor returned by the query.

Returns

\OntologyWrapper\Iterator —

Query cursor.

collection()

collection() : \OntologyWrapper\CollectionObject

<h4>Return collection</h4>

This method can be used to retrieve the current object's collection, which is the collection object that was queried.

Returns

\OntologyWrapper\CollectionObject

Query collection.

criteria()

criteria() : array

<h4>Return criteria</h4>

This method can be used to retrieve the current object's criteria, which is the query filter.

Returns

array —

Query filter.

resultType()

resultType(mixed $theValue, boolean $getOld) : mixed

Manage result type

This method can be used to manage the result type, it accepts a parameter which represents either the result type code or the requested operation, depending on its value:

  • NULL: Return the current value.
  • FALSE: Delete the current value.
  • {@link kQUERY_OBJECT}: Return an object.
  • {@link kQUERY_ARRAY}: Return the object array.
  • {@link kQUERY_NID}: Return the object native identifier.
  • other: Will raise an exception.

The second parameter is a boolean which if TRUE will return the old value when replacing or resetting; if FALSE, it will return the current value.

Parameters

mixed $theValue

Data source name or operation.

boolean $getOld

TRUE get old value.

Throws

\OntologyWrapper\Exception

Returns

mixed —

New or old result type code.

setKeyOffset()

setKeyOffset(mixed $theOffset)

Set key offset

This method can be used to set the key offset, it expects a parameter which represents the offset in the current element from which to get the current key value.

Parameters

mixed $theOffset

Key offset.

current()

current() : mixed

<h4>Return the value of the current element</h4>

In this class we take the result of the cursor and pass it to the {@link shapeResult()} method which takes care of returning the desired type of value.

Returns

mixed —

Current element value.

key()

key() : mixed

<h4>Return the key of the current element</h4>

In this class we use the current element's native identifier as the key, or the cursor's key if the former is not available.

Returns

mixed —

Current element's key.

next()

next()

<h4>Advance to the next element</h4>

We act as a proxy for the cursor.

rewind()

rewind()

<h4>Rewind iterator</h4>

We act as a proxy for the cursor.

valid()

valid() : boolean

<h4>Check if iterator is valid</h4>

We act as a proxy for the cursor.

Returns

boolean

shapeResult()

shapeResult(array $theObject) : mixed

Cast iterator value

This method will format the provided cursor value according to the {@link resultType()}.

Depending on the {@link resultType()} value:

  • {@link kQUERY_OBJECT}: Return an object.
  • {@link kQUERY_ARRAY}: Return the object array.
  • {@link kQUERY_NID}: Return the object native identifier.
  • other: Will raise an exception.

The method expects the value provided as an array.

Parameters

array $theObject

Current cursor value.

Throws

\OntologyWrapper\Exception

Returns

mixed —

The formatted value.