Login   Register  
PHP Classes
elePHPant
Icontem

File: Tool_JIT.class.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Tom Schaefer  >  Tool_JIT  >  Tool_JIT.class.php  >  Download  
File: Tool_JIT.class.php
Role: Class source
Content type: text/plain
Description: jit json composer
Class: Tool_JIT
Generate data structures in JSON for JIT
Author: By
Last change:
Date: 2009-02-09 10:40
Size: 4,902 bytes
 

Contents

Class file image Download
<?php

/**
 * Tool_JIT
 *
 * @package Tool
 * @since 09.02.2009
 * @version 0.1
 * @author Thomas Schäfer
 *
 * @desc build a json object for the JIT Lib
 *
 * @see http://blog.thejit.org/javascript-information-visualization-toolkit-jit/
 * @example
 * <code>
 * $node     = Tool_JIT::getInstance();
 * $node    ->set("190","Pearl Jam")
            ->addChild(
                Tool_JIT::getInstance()
                    ->set("306208","Pearl Jam &amp; Cypress Hill")
                    ->add("Pearl Jam","collaboration")
                    ->addChild(
                        Tool_JIT::getInstance()
                            ->set("84","Cypress Hill")
                            ->add("Pearl Jam &amp; Cypress Hill","collaboration")
                            ->addChild()
                    )
            );
    echo $node->render();
    </code>
 */
class Tool_JIT {

    
/**
     * var static $iCounter
     * @desc used for id attachment
     */
    
private static $iCounter 0;

    
/**
     * @var node
     */
    
private static $instance;

    
/**
     * @var $node
     */
    
private $node;

    public function 
__construct(){}

    
/**
     * getInstance
     */
    
public function getInstance(){
        if(empty(
self::$instance)) {
            return new 
Tool_JIT;
        }
        return 
self::$instance;
    }

    
/*
     * set
     * @desc set id and node caption
     * @param string id
     * @param string $caption
     * @return self
     */
    
public function set($id$caption) {
        
$this->node = new stdClass;
        
$this->node->id $id "_"Tool_JIT::$iCounter;
        
$this->node->name $caption;
        
$this->node->data = array();

        
Tool_JIT::$iCounter++;
        return 
$this;
    }

    
/**
     * add
     * @desc add a key/value pair
     * @param string $key
     * @param mixed $value
     * @return self
     */
    
public function add($key,$value) {
        if(empty(
$this->node->data) or !array_key_exists("data",$this->node)) {
            
$this->node->data = array();
        }
        
$data = new stdClass;
        
$data->key $key;
        
$data->value $value;
        
$this->node->data[] = $data;
        return 
$this;
    }

    
/**
     * to
     * @desc connect a node with its parent
     * @param string $strNodeTo
     * @param integer $weight
     * @return self
     */
    
public function to($strNodeTo$weight) {
        if(empty(
$this->node->adjacencies) or !array_key_exists("adjacencies",$this->node)) {
            
$this->node->adjacencies = array();
        }
        
$nodeTo = new stdClass;
        
$nodeTo->nodeTo $strNodeTo;

        
$nodeTo->data = array();

        
$nodeToData = new stdClass;
        
$nodeToData->weight 3;

        
$nodeTo->data[] = $nodeToData;
        
$this->node->adjacencies[] = $nodeTo;
        return 
$this;
    }

    
/**
     * addChild
     * @desc add a new node as child
     * @param Tool_JIT $node
     * @return self
     */
    
public function addChild($node null) {
        if(
is_null($node) or $node instanceof Tool_JIT)
        {
            if(empty(
$this->node->children) or !array_key_exists("children",$this->node)) {
                
$this->node->children = array();
            }
            
$this->node->children[] = $node;
        }
        return 
$this;
    }

    
/**
     * get
     * @desc returns the data tree
     * @return mixed
     */
    
public function get() {
        return 
$this->node;
    }

    
/**
     * render
     * @desc renders the Tool_JIT-Object as JSON
     * @return string
     */
    
public function render() {
        return 
Tool_JIT::toJSON($this->get());
    }

    
/**
     * toJSON
     * @param mixed $data
     * @param bool $escape
     * return $mixed
     */
    
public static function toJSON($data=null$escape=true)
    {
        if (
is_null($data)){
            return 
'';
        }
        if (
$data === false){
            return 
'false';
        }
        if (
$data === true){
            return 
'true';
        }
        if (
is_scalar($data)) {
            
$data addslashes($data);
            
$data preg_replace('{(</)(script)}i'"$1'+'$2"$data);
            if(
                empty(
$escape) or
                
preg_match("/^([0-9]{1,14})$/",$data) or
                
preg_match("/^(\+?((([0-9]+(\.)?)|([0-9]*\.[0-9]+))([eE][+-]?[0-9]+)?))$/",$data) or
                
is_bool($data)
                ) {
                
$data stripslashes($data);
                
$data str_replace("\n"""$data);
                
$data str_replace("\r"""$data);
                
$data str_replace("\t"""$data);
                return 
trim($data);
            }else{
                
$data str_replace("\n"""$data);
                
$data str_replace("\r"""$data);
                
$data str_replace("\t"""$data);
                return 
"\"".trim($data)."\"";
            }

        }
        
$isList true;
        for (
$i=0reset($data); $i<count($data); $i++, next($data)){
            if (
key($data) !== $i) {
                
$isList false; break;
            }
        }
        
$result = array();
        if (
$isList) {
            foreach (
$data as $v){
                if((
is_scalar($v) and (
                        
preg_match("/^([0-9]{1,14})$/",$v)) or
                        
is_bool($v)
                )) {
                    
$result[] = Tool_JIT::toJSON($v,false);
                } else {
                    if(
$v instanceof Tool_JIT)
                        
$result[] = Tool_JIT::toJSON($v->get());
                    else
                        
$result[] = Tool_JIT::toJSON($v);
                }
            }
            return 
'[' join(','$result) . ']';
        } else {
            foreach (
$data as $key => $value){
                
$result[] = Tool_JIT::toJSON($key) . ':' Tool_JIT::toJSON($value);
            }
            return 
'{' join(','$result) . '}';
        }
    }

}