Gumbo PHP Library API Documentation
Converter
[
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 Array.class.php
Documentation is available at
Array.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
Converter
*
@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
*/
/**
* Array Converter Class
*
*
@category
Gumbo
*
@package
Converter
*
@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
Array Converter Class
*
@version
0.0.1
*/
gumbo_load
(
"Converter_Abstract"
)
;
class
Gumbo_Converter_Array
extends
Gumbo_Converter_Abstract
{
/** ACTION METHODS **/
/**
* Returns a Composite
*
@param
array
$data
array data
*
@return
Gumbo_Interface_Composite
*
@throws
Gumbo_Exception
*
@uses
Gumbo_Branch
*/
public
function
toComposite
(
$data
)
{
try
{
// verify precondition
if
(
!
is_array
(
$data
))
{
throw
new
Gumbo_Exception
(
"
Invalid
Argument
'
data
:
arr
' => {
$data
}
:
"
.
gettype
(
$data
))
;
}
return
$this
->
_getComposite
(
$data
)
;
}
catch
(
Gumbo_Exception
$e
)
{
$e
->
setFunction
(
__METHOD__
)
;
gumbo_trigger
(
$e
)
;
}
return
new
Gumbo_Branch
(
)
;
}
/** ACCESSOR METHODS **/
/**
* Returns a single Composite object from an array
* This is a recusive method
*
@param
array
$data
*
@param
string
$name
name of the Composite
*
@return
Gumbo_Interface_Composite
*
@throws
Gumbo_Exception
*
@uses
Gumbo_Interface_Composite
*/
private
function
_getComposite
(
$data
,
$name
=
null
)
{
try
{
// verify precondition
if
(
!
is_array
(
$data
))
{
throw
new
Gumbo_Exception
(
"
Invalid
Argument
'
data
:
arr
' => {
$data
}
:
"
.
gettype
(
$data
))
;
}
try
{
if
(
!
is_null
(
$name
)
&&
!
is_string
(
$name
)
&&
!
is_numeric
(
$name
))
{
throw
new
Gumbo_Exception
(
"
Invalid
Argument
'
name
:
num
|
str
|
null
' => {
$name
}
:
"
.
gettype
(
$name
))
;
}
}
catch
(
Gumbo_Exception
$e
)
{
$e
->
setFunction
(
__METHOD__
)
;
gumbo_trigger
(
$e
)
;
$name
=
null
;
}
$tree
=
new
Gumbo_Branch
(
$name
)
;
// loop through the array
foreach
(
$data
as
$key
=>
$val
)
{
if
(
is_array
(
$val
))
{
$tmp
=
$this
->
_getTree
(
$val
,
$key
)
;
if
(
$tmp
instanceof
Gumbo_Interface_Composite
)
{
$tree
->
add
(
$tmp
)
;
}
}
else
{
$tree
->
add
(
new
Gumbo_Leaf
(
$key
,
$val
))
;
}
}
return
$tree
;
}
catch
(
Gumbo_Exception
$e
)
{
$e
->
getFunction
(
__METHOD__
)
;
gumbo_trigger
(
$e
)
;
}
return
null
;
}
}
?>