PHP5 xmlObject

This entry was posted on 2008-08-05 18:37:23 EEST in 60° 43.992 N 24° 46.302 E Riihimäki, FI to

UPDATE [08/06/2008]: Version update. Did some fixing and made it work better with larger structures.

Here's another simple little helper I developed for Duct framework.
I use this one to convert my arrays to XML. As Duct automatically scaffolds objects from XML/YAML to database
and uses the same methods to render the database objects to html forms for editing I needed some quick way to
transform this data to and from XML. Class end examples are here

Usage example:

<?php
$resource = new xmlObjectItem();
$resource->items = array(
'id' => 100,
'name' => "Jerry Jalava",
'person_id' => 1
);
$resource->attributes = array(
'person_id' => array(
'is_assoc' => true,
'foreign_model' => 'person',
'foreign_key' => 'id'
)
);

$resource_set = new xmlObjectSet();
$resource_set->wrapper_id = 'resource';
$resource_set->items[] = $resource;

$projects = array();
for ($i=1; $i <= 1; $i++)
{
$object = new xmlObjectItem();
$object->items = array(
'id' => $i,
'name' => "Project {$i}",
'manager_id' => $i+1,
'resources' => $resource_set
);

$projects[] = $object;
}

$project_set = new xmlObjectSet();
$project_set->wrapper_id = 'project';
$project_set->items = $projects;

$clients = array();
for ($i=1; $i <= 1; $i++)
{
$client = new xmlObjectItem();
$client->items = array(
'id' => $i,
'name' => "Client {$i}",
'contact_id' => $i+1,
'team-leader' => null,
'projects' => $project_set
);
$client->attributes = array(
'contact_id' => array(
'is_assoc' => true,
'foreign_model' => 'person',
'foreign_key' => 'id'
)
);

$clients[] = $client;
}
$client_set = new xmlObjectSet();
$client_set->wrapper_id = 'client';
$client_set->items = $clients;

echo "complex set:\n";
var_dump($client_set);

echo "\n--------toXml---------\n";

$xmlSet_clients = xmlObjects::toXml($client_set, 'clients');
echo "complex:\n";
echo $xmlSet_clients;

echo "\n--------fromXml---------\n";

$client_set = xmlObjects::fromXml($xmlSet_clients);
echo "client_set root: {$client_set->root}\n";
var_dump($client_set);

echo "\n--------back toXml---------\n";

$xmlSet_clients2 = xmlObjects::toXml($client_set);
echo "complex:\n";
echo $xmlSet_clients2;

if ($xmlSet_clients == $xmlSet_clients2) {
echo "\n\nVALID SET CONVERSION\n\n";
}

?>
Back