Gumbo PHP Library API Documentation
Iterator
[
class tree
] [
index
] [
all elements
]
Todo List
Packages:
Buffer
Collection
Composite
Config
Converter
Curl
Date
DB
Debug
Encryption
Error
Factory
Filter
Flyweight
Http
Input
Iterator
List
Load
Lockable
Log
Map
Number
Observer
Output
Query
Record
Router
Session
Setting
Singleton
Template
Timer
Utility
Valid
Source for file Iterator.class.php
Documentation is available at
Iterator.class.php
<?php
/**
* Gumbo Library Framework
*
* LICENSE
* This library is being released under the terms of the New BSD License. A
* copy of the license is packaged with the software (LICENSE.txt). If no
* copy is found, a copy of the license template can be found at:
* http://www.opensource.org/licenses/bsd-license.php
*
*
@category
Gumbo
*
@package
Iterator
*
@copyright
Copyright (c) 2007, iBayou, Michael Luster
*
@license
http://www.opensource.org/licenses/bsd-license.php New BSD License
*
@author
Michael Luster <mluster79@yahoo.com>
*
@link
http://sourceforge.net/projects/phpgumbo
*
@version
0.0.1
*/
/**
* Iterator Class
*
*
@category
Gumbo
*
@package
Iterator
*
@copyright
Copyright (c) 2007, iBayou, Michael Luster
*
@license
http://www.opensource.org/licenses/bsd-license.php New BSD License
*
@author
Michael Luster <mluster79@yahoo.com>
*
@link
http://sourceforge.net/projects/phpgumbo
*
@desc
Iterator Class
*
@version
0.0.1
*/
gumbo_load
(
"Interface_Iterator"
)
;
class
Gumbo_Iterator
implements
Gumbo_Interface_Iterator
{
/**
@var
array
$_list
array of elements */
private
$_list
=
array
(
)
;
/**
@var
array
$_keys
array of keys */
private
$_keys
=
array
(
)
;
/**
@var
int
$_current
current incremental element of loop */
private
$_current
=
0
;
/**
* Constructor
*
* The constructor requires an array to be given. The object will
* save the keys, keeping array references on the original array. The
* supplied array can contain any type of values.
*
*
@param
array
|
Gumbo_Interface_List
$list
array to loop through
*
@throws
Gumbo_Exception
*/
public
function
__construct
(
$list
)
{
try
{
// verify precondition
if
(
!
is_array
(
$list
)
&&
!
is_object
(
$list
))
{
throw
new
Gumbo_Exception
(
"
Invalid
Argument
'
list
:
arr
' => {
$list
}
:
"
.
gettype
(
$list
))
;
}
if
(
is_object
(
$list
)
&&
!
(
$list
instanceof
Gumbo_Interface_List
))
{
throw
new
Gumbo_Exception
(
"
Invalid
Argument
'
list
:
Gumbo_Interface_List
' => {
$list
}
:
"
.
getType
(
$list
))
;
}
if
(
is_array
(
$list
))
{
$this
->
_keys
=
array_keys
(
$list
)
;
$this
->
_list
=
$list
;
}
else
{
$this
->
_keys
=
$list
->
keys
(
)
;
$this
->
_list
=
$list
->
getAll
(
)
;
}
}
catch
(
Gumbo_Exception
$e
)
{
$e
->
setFunction
(
__METHOD__
)
;
gumbo_trigger
(
$e
)
;
}
}
/** ACTION METHODS **/
/**
* Returns the current element
*
@return
mixed
*/
public
function
current
(
)
{
if
(
!
$this
->
valid
(
))
{
return
null
;
}
return
$this
->
_list
[
$this
->
key
(
)
]
;
}
/**
* Returns the current key value
*
@return
int
*/
public
function
key
(
)
{
if
(
!
isset
(
$this
->
_keys
[
$this
->
_current
]
))
{
return
null
;
}
return
$this
->
_keys
[
$this
->
_current
]
;
}
/**
* Moves the target to the next element
*/
public
function
next
(
)
{
$this
->
_current
++
;
}
/**
* Resets the Iterator
*/
public
function
rewind
(
)
{
$this
->
_current
=
0
;
}
/**
* Checks if the current element is valid
*
@return
bool
*/
public
function
valid
(
)
{
if
(
count
(
$this
->
_list
)
<=
0
)
{
return
false
;
}
if
(
is_null
(
$this
->
key
(
)))
{
return
false
;
}
return
isset
(
$this
->
_list
[
$this
->
key
(
)
]
)
;
}
/** ACCESSOR METHODS **/
/**
* Returns the array of keys in the object
*
@return
array
*/
public
function
getKeys
(
)
{
return
$this
->
_keys
;
}
/**
* Returns the full array list
*
@return
array
*/
public
function
getList
(
)
{
return
$this
->
_list
;
}
/**
* Returns the current incremental element number
*
@return
int
*/
public
function
getCurrent
(
)
{
return
$this
->
_current
;
}
/**
* Returns the total number of elements in the Iterator
*
@return
int
*/
public
function
getTotal
(
)
{
return
count
(
$this
->
getList
(
))
;
}
}
?>