Perforce Chronicle 2012.2/486814
API Documentation
|
Provide a common container for a set of models. More...
Public Member Functions | |
__construct ($models=null) | |
Create a new model iterator. | |
current () | |
Return the value of the array element that's currently being pointed to by the internal pointer. | |
filterByCallback ($callback, $params=null, $options=array()) | |
Filter items of this instance by callback function. | |
first () | |
Get the value of the first element. | |
getProperties () | |
Get all properties of this iterator. | |
getProperty ($name) | |
Get a particular property value of this iterator. | |
hasProperty ($name) | |
Check if iterator has a particular property. | |
invoke ($functionName, $params=array()) | |
Will run the specified function on each entry in the iterator, optionally passing arguments. | |
key () | |
Return the key of the array element that's currently being pointed to by the internal pointer. | |
keys () | |
Get all of the keys for all of the entries in this iterator. | |
last () | |
Get the value of the last element. | |
merge (P4_Iterator $iterator) | |
Merges the passed iterator's values into this iterator. | |
next () | |
Overwrite next() method to return current value or false. | |
offsetSet ($key, $model) | |
Set the model under the given key. | |
rewind () | |
Overwrite rewind() method to reset array pointer. | |
seek ($position) | |
Seek to an absolute position. | |
setProperties (array $properties) | |
Set iterator properties. | |
setProperty ($name, $value) | |
Set a particular property of this iterator. | |
Public Attributes | |
const | FILTER_COPY = 'COPY' |
const | FILTER_INVERSE = 'INVERSE' |
Protected Attributes | |
$_allowedModelClass = 'P4_ConnectedInterface' | |
Define the type of models we want to accept in this iterator. | |
$_properties = array() | |
Store custom iterator properties. |
Provide a common container for a set of models.
Advantage of extending ArrayIterator is that php built-in array-walk functions reset(), next(), key(), current() can be replaced by class-implemented counterparts and vice versa. In other words, if $iterator is an instance of P4_Iterator class then $iterator->next() and next($iterator) are equivalent and same for all other pairs.
P4_Iterator::__construct | ( | $ | models = null | ) |
Create a new model iterator.
If an array of models is given, populate from the array.
array | $models | optional - the set of models to contain. |
{ if (isset($models) && is_array($models)) { foreach ($models as $model) { if (!$model instanceof $this->_allowedModelClass) { throw new InvalidArgumentException("Models array contains one or more invalid elements."); } } parent::__construct($models); } else { parent::__construct(array()); } }
P4_Iterator::current | ( | ) |
Return the value of the array element that's currently being pointed to by the internal pointer.
It does not move the pointer in any way.
{ return current($this); }
P4_Iterator::filterByCallback | ( | $ | callback, |
$ | params = null , |
||
$ | options = array() |
||
) |
Filter items of this instance by callback function.
Callback must be callable function with at least one parameter represents the allowed model class instance.
Additional parameters can be set in params, in this case callback will be called with model instance parameter followed by params.
Item (model) is acceptable if and only if callback function with item passed as first parameter returns true.
Valid filter options are:
FILTER_INVERSE - inverse filtering behavior - acceptable items are removed FILTER_COPY - return a filtered copy without modifying original
callback | $callback | callback function to determine if item is acceptable |
mixed | $params | optional additional callback parameters |
string | array | $options | optional - one or more filtering options |
{ if (!is_callable($callback)) { throw new Exception('Callback for P4Cms_Model_Iterator must be callable function.'); } $copy = new static; // remove items where callback returns false foreach ($this->getArrayCopy() as $key => $model) { $passesFilter = call_user_func_array($callback, array($model, $params)); // inverse behavior if FILTER_INVERSE option is set if (in_array(self::FILTER_INVERSE, $options)) { $passesFilter = !$passesFilter; } if (!$passesFilter && !in_array(static::FILTER_COPY, $options, true)) { $this->offsetUnset($key); } else if ($passesFilter && in_array(static::FILTER_COPY, $options, true)) { $copy[$key] = $model; } } return in_array(static::FILTER_COPY, $options, true) ? $copy : $this; }
P4_Iterator::first | ( | ) |
Get the value of the first element.
P4_Iterator::getProperties | ( | ) |
Get all properties of this iterator.
{
return $this->_properties;
}
P4_Iterator::getProperty | ( | $ | name | ) |
Get a particular property value of this iterator.
string | $name | name of the property to get the value of |
InvalidArgumentException | if the property name does not exist |
{ // return property value if it was set, otherwise throw an exception if ($this->hasProperty($name)) { return $this->_properties[$name]; } throw new InvalidArgumentException( "Cannot find iterator property '$name'. Property was not set." ); }
P4_Iterator::hasProperty | ( | $ | name | ) |
Check if iterator has a particular property.
string | $name | the property name to check for the existence of |
{
return array_key_exists($name, $this->_properties);
}
P4_Iterator::invoke | ( | $ | functionName, |
$ | params = array() |
||
) |
Will run the specified function on each entry in the iterator, optionally passing arguments.
An array of function return values will be returned.
string | $functionName | The name of the function to execute |
array | $params | Optional array of paramaters to pass the function |
InvalidArgumentException | If any entry lacks the specified function |
{ $results = array(); foreach ($this as $entry) { if (!is_object($entry) || !method_exists($entry, $functionName)) { throw new InvalidArgumentException( 'One or more entries lack the specified function' ); } $results[] = call_user_func_array(array($entry, $functionName), $params); } return $results; }
P4_Iterator::key | ( | ) |
Return the key of the array element that's currently being pointed to by the internal pointer.
It does not move the pointer in any way.
{ return key($this); }
P4_Iterator::keys | ( | ) |
Get all of the keys for all of the entries in this iterator.
{
return array_keys($this->getArrayCopy());
}
P4_Iterator::last | ( | ) |
Get the value of the last element.
{ end($this); return $this->current(); }
P4_Iterator::merge | ( | P4_Iterator $ | iterator | ) |
Merges the passed iterator's values into this iterator.
If the input iterator has the same string keys, then the later value for that key will overwrite the previous one. If, however, the key are numeric, the later value will not overwrite the original value, but will be appended.
P4_Iterator | $iterator | The new values to merge in |
{ foreach ($iterator as $key => $value) { if (is_int($key)) { $this[] = $value; } else { $this[$key] = $value; } } return $this; }
P4_Iterator::next | ( | ) |
Overwrite next() method to return current value or false.
If php built-in next() function is not called then array pointer is not advanced and other php array-walk functions like current() or key() won't work.
{ parent::next(); next($this); return $this->valid() ? $this->current() : false; }
P4_Iterator::offsetSet | ( | $ | key, |
$ | model | ||
) |
Set the model under the given key.
string | integer | $key | the key to store the model under. |
P4_Model | $model | the model to store. |
{ if (!$model instanceof $this->_allowedModelClass) { throw new InvalidArgumentException("Invalid model supplied."); } return parent::offsetSet($key, $model); }
P4_Iterator::rewind | ( | ) |
Overwrite rewind() method to reset array pointer.
If php built-in reset() function is not called then array pointer is not advanced and other php array-walk functions like current() or key() won't work.
{ parent::rewind(); reset($this); }
P4_Iterator::seek | ( | $ | position | ) |
Seek to an absolute position.
integer | $position | the numeric position to seek to. |
P4_Iterator::setProperties | ( | array $ | properties | ) |
Set iterator properties.
array | $properties | array with properties to set |
{
$this->_properties = $properties;
return $this;
}
P4_Iterator::setProperty | ( | $ | name, |
$ | value | ||
) |
Set a particular property of this iterator.
string | $name | name of the property to set the value of |
mixed | $value | value to set |
{
$this->_properties[$name] = $value;
return $this;
}
P4_Iterator::$_allowedModelClass = 'P4_ConnectedInterface' [protected] |
Define the type of models we want to accept in this iterator.
Reimplemented in P4_Model_Iterator, and P4Cms_Model_Iterator.
P4_Iterator::$_properties = array() [protected] |
Store custom iterator properties.
const P4_Iterator::FILTER_COPY = 'COPY' |
const P4_Iterator::FILTER_INVERSE = 'INVERSE' |