Login   Register  
PHP Classes
elePHPant
Icontem

File: DataDumper.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Matt Durell  >  DataDumper  >  DataDumper.php  >  Download  
File: DataDumper.php
Role: ???
Content type: text/plain
Description: code, comments & examples
Class: DataDumper
Author: By
Last change:
Date: 2001-12-22 02:34
Size: 6,069 bytes
 

Contents

Class file image Download
<?
/*
----------------------------------------------------------------------

DataDumper - PHP class

Author: Matt Durell <matt@specialsource.net>http://specialsource.net/

History:
1.0  2001-12-22
	Initial release.


(please check http://specialsource.net/freeware/ for latest release)


Abstract:

The idea for DataDumper came from the CPAN Perl module Data::Dumper.

DataDumper will take a PHP variable (numeric, boolean, string or
array) and convert it into a string containing the PHP code needed
to re-create the variable again, using eval().

This means that, for example, the entire structure and contents of
complex multi-dimentional arrays can be saved to a text file, then
read and eval'd back into a PHP variable; useful for sharing such
data structures across otherwise unconnected scripts.

I'm afraid that it will not currently work in versions of PHP before
4.0.? (shouldn't be too difficult to convert for v3 but I don't have
the time right now ;-)

Also, I'm affraid that comments are non-existant at the moment.

Usage Example:

	include('DataDumper.php');
	$test = array
		(
		123.45,
		"123.45",
		array
			(
			a => 'A',
			b => 'B',
			c => 'C'
			)
		);	
	$dd = new DataDumper();
	$code = $dd->dump( $test );
	print "--------------------\n";
	print $code;
	print "--------------------\n";

Will output the following:

--------------------
array
        (
        0 => 123.45,
        1 => '123.45',
        2 => array
                (
                'a' => 'A',
                'b' => 'B',
                'c' => 'C'
                )
        );
--------------------

For convenience there are a couple of further methods:

evaluate( string code )
	This is a handy wrapper for the eval() function, and returns
	a PHP variable re-built from the code string supplied.
	E.g. in the above example,

		$new = $dd->evaluate( $code );

	would create a new copy of $test in $new.

pretty( boolean )
	This turns on/off the 'pretty-printing' feature - when ON
	(true), output is nicely tabbed and spaced, when OFF (false)
	output contains no whitespace, making the resultant code 
	string smaller.
	E.g. using the same $test array from above,

		$dd = new DataDumper();
		$dd->pretty( false );
		$code = $dd->dump( $test );
		print $code;

	would result in:

	array(0=>123.45,1=>'123.45',2=>array('a'=>'A','b'=>'B','c'=>'C'));	
	
	pretty-printing is ON by default.


Legal Stuff:

You are free to use and modify this software code as you like, the only
condition of use being that the author accepts no responsibility for
anything that may happen as a consequence of it's use, and that by
using this software you are deemed to have agreed to this condition.

On a lighter note, if you _do_ use it, it would be nice to get a heads-
up and/or a pointer to the site/application where it's being used... or 
just some words of encouragement will do ;-)


Support and Reporting Bugs:

I do not have time to offer proper support, but if you find any bugs
please let me know and I'll do my best to fix them. If you've fixed a
bug yourself, please also let me know the details so I can incorporate
your fix (with due accreditation) in the next release.

----------------------------------------------------------------------
*/


class DataDumper
	{
	var $versionOK;
	var $error;
	var $newline;
	var $tab;
	var $space;

	function DataDumper()
		{
		$currentVersion = phpversion();
		if ( $currentVersion >= 4 )
			{
			$this->versionOK = true;
			$this->pretty( true );
			}
		else
			{
			$this->versionOK = false;
			$this->error = "Sorry, DataDumper currently only supports >= PHP 4";
			}
		}

	function pretty( $flag=true )
		{
		if ( $flag )
			{
			$this->newline = "\r\n";
			$this->tab = "\t";
			$this->space = ' ';
			}
		else
			{
			$this->newline = '';
			$this->tab = '';
			$this->space = '';
			}
		}

	function dump( &$input )
		{
		if ( ! $this->versionOK )
			{
			return false;
			}
		if ( $output = $this->parseElement($input,'') )
			{
			return "$output;".$this->newline;
			}
		return false;
		}

	function evaluate( &$input )
		{
		if ( ! $this->versionOK )
			{
			return false;
			}
		$output = null;
		eval("\$output = $input;");
		return $output;
		}

	function parseElement( &$input, $padding )
		{
		if ( ! $this->versionOK )
			{
			return false;
			}
		$output = null;
		if ( is_array($input) )
			{
			$padding .= $this->tab;
			$output = "array".$this->newline."$padding(".$this->newline;
			$numElems = count($input);
			$count = 0;
			foreach( $input as $k => $v )
				{
				if ( is_string($k) )
					{
					$output .= "$padding'".$this->strQuote($k)."'".$this->space."=>".$this->space;
					}
				else
					{
					$output .= $padding.$k.$this->space."=>".$this->space;
					}
				$output .= $this->parseValue( $v, $padding );
				if ( ++$count < $numElems )
					{
					$output .= ",";
					}
				$output .= $this->newline;
				}
			$output .= "$padding)";
			}
		else if ( is_bool($input) )
			{
			$output = $input ? "true" : "false";
			}
		else if ( is_string($input) )
			{
			$output = "'".$this->strQuote($input)."'";
			}
		else if ( is_numeric($input) )
			{
			$output = $input;
			}
		else
			{
			$this->error = "Supplied data was not Numeric, Boolean, String or Array";
			}
		return $output;
		}


	function parseValue( &$input, $padding )
		{
		if ( ! $this->versionOK )
			{
			return false;
			}
		if ( is_array($input) )
			{
			return $this->parseElement( $input, $padding );
			}
		else if ( is_bool($input) )
			{
			return $input ? "true" : "false";
			}
		else if ( is_string($input) )
			{
			return "'".$this->strQuote($input)."'";
			}
		else if ( is_numeric($input) )
			{
			return $input;
			}
		return "null";
		}

	function strQuote( &$subject )
		{
		return preg_replace( "/([\'\\\])/", "\\\\\\1", $subject );
		}
	}

?>