data_structures
[ class tree: data_structures ] [ index: data_structures ] [ all elements ]

Source for file ABSTObject.class.php

Documentation is available at ABSTObject.class.php

  1. <?php
  2. /**
  3.  * @package data_structures
  4.  */
  5.  
  6. if (
  7.      version_compare( phpversion(), '5.0' ) < 0
  8.   && !function_exists( "clone" )
  9.    )
  10. {
  11.     eval( '
  12.     function clone( $object )
  13.     {    return $object; }
  14.     ');
  15. }
  16.  
  17. /**
  18.  * Class to inspect objects that are an extension of this class.
  19.  *
  20.  * @package   data_structures
  21.  * @version   0.1.24
  22.  * @since     0.9.3.3
  23.  * @abstract
  24.  * @author    Daniel Plücken <daniel@debakel.net>
  25.  * @license   http://www.gnu.org/copyleft/lesser.html
  26.  *             GNU Lesser General Public License
  27.  * @copyright Copyright (C) 2007 Daniel Plücken <daniel@debakel.net>
  28.  *
  29.  *  This library is free software; you can redistribute it and/or
  30.  *  modify it under the terms of the GNU Lesser General Public
  31.  *  License as published by the Free Software Foundation; either
  32.  *  version 2.1 of the License.
  33.  *
  34.  *  This library is distributed in the hope that it will be useful,
  35.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  36.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37.  *  GNU Lesser General Public License for more details.
  38.  *
  39.  *  You should have received a copy of the GNU Lesser General
  40.  *  Public License along with this library; if not, write to the
  41.  *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  42.  *  Boston, MA 02111-1307 USA
  43.  */
  44. class ABSTObject
  45. {
  46.     /**
  47.      * Prints out the primitive data by the given arrays.
  48.      *
  49.      * @require PHP 4
  50.      * @version 1.0
  51.      * @since   0.1.0
  52.      * @access  public
  53.      *
  54.      * @return  void 
  55.      *
  56.      * @author  Daniel Plücken <daniel.pluecken@wfp2.com>
  57.      */
  58.     function printPrimitiveData(
  59.                                   &$primitive_types_arr,
  60.                                   &$primitive_array_arr
  61.                                )
  62.     {
  63.         if (
  64.              count( $primitive_types_arr )
  65.            + count( $primitive_array_arr ) > 0
  66.            )
  67.         {
  68.             echo "<p>\r\n";
  69.             echo "<h2>Primitive Data:</h2>\r\n";
  70.             echo "<blockquote>\r\n";
  71.             foreach ( $primitive_types_arr as $nam => $value )
  72.             {
  73.                  $value = preg_replace( "![\r\n]+!s", "", strip_tags($value) );
  74.                  if ( strlen( $value ) < 256 )
  75.                     echo "<b>{$nam}</b>: {$value}<br />\r\n";
  76.                  else
  77.                     echo "<b>{$nam}</b>: ".substr( $value, 0, 255 )
  78.                         ." [...]<br />\r\n";
  79.             }
  80.             foreach ( $primitive_array_arr as $nam => $value )
  81.             {
  82.                echo "<b>{$nam}:</b> ";
  83.                print_r( $value );
  84.                echo "<br />\r\n";
  85.             }
  86.             echo "</blockquote>\r\n";
  87.             echo "</p>\r\n";
  88.         }
  89.     }
  90.  
  91.  
  92.  
  93.     /**
  94.      * Prints out the more complex data by the given array.
  95.      *
  96.      * @access  public
  97.      * @require PHP 4
  98.      * @version 1.0
  99.      * @since   0.1.0
  100.      *
  101.      * @param array  $multidimensional_arr 
  102.      * @param array  $depth_arr 
  103.      * @param string $exept_reg_exp 
  104.      *
  105.      * @return  void 
  106.      *
  107.      * @author  Daniel Plücken <daniel.pluecken@wfp2.com>
  108.      */
  109.     function printComplexData(
  110.                                 &$multidimensional_arr,
  111.                                 $depth_arr = array(),
  112.                                 $exept_reg_exp = null
  113.                              )
  114.     {
  115.         $dig_deeper_arr = array();
  116.         $handled_object_arr = array();
  117.         $unhandled_object_arr = array();
  118.  
  119.         foreach ( $multidimensional_arr as $key => $val )
  120.         {
  121.             if (
  122.                  is_object( $multidimensional_arr[$key] )
  123.               && !method_exists ( $multidimensional_arr[$key], "printStructure" )
  124.                )
  125.                $unhandled_object_arr[$key] =& $multidimensional_arr[$key];
  126.             else
  127.             if ( is_object( $multidimensional_arr[$key] ) )
  128.                $handled_object_arr[$key] =& $multidimensional_arr[$key];
  129.             else
  130.             if ( is_array( $multidimensional_arr[$key] ) )
  131.                $dig_deeper_arr[$key] =& $multidimensional_arr[$key];
  132.         }
  133.  
  134.  
  135.         $mem_depth_arr = $depth_arr;
  136.         $nam_prefix = array_shift( $depth_arr );
  137.         if ( count( $depth_arr ) > 1 )
  138.            $nam_prefix .= "[".implode( "][", $depth_arr )."]";
  139.  
  140.         foreach ( $unhandled_object_arr as $nam => $value )
  141.         {
  142.             echo "<b>{$nam_prefix}[{$nam}]</b>: *Object ";
  143.             echo get_class( $unhandled_object_arr[$nam] );
  144.             echo " (unhandled)<br />\r\n";
  145.         }
  146.  
  147.         foreach ( $handled_object_arr as $nam => $value )
  148.         {
  149.             echo "<b>{$nam_prefix}[{$nam}]</b>:\r\n";
  150.             echo "<blockquote>";
  151.             echo $handled_object_arr[$nam]->printStructure( $exept_reg_exp );
  152.             echo "</blockquote>";
  153.             echo "<br />\r\n";
  154.         }
  155.  
  156.         $depth_arr = $mem_depth_arr;
  157.         foreach ( $dig_deeper_arr as $nam => $value )
  158.         {
  159.             $depth_arr[] = $nam;
  160.             $this->printComplexData(
  161.                                      $dig_deeper_arr[$nam],
  162.                                      $depth_arr,
  163.                                      $exept_reg_exp
  164.                                    );
  165.  
  166.             $depth_arr = $mem_depth_arr;
  167.         }
  168.     }
  169.  
  170.  
  171.  
  172.     /**
  173.      * Compares two references whether they points to the same object
  174.      *
  175.      * @static
  176.      * @access  public
  177.      * @require PHP 4
  178.      * @version 1.0
  179.      * @since   0.1.24
  180.      *
  181.      * @param object $a_obj 
  182.      * @param object $b_obj 
  183.      *
  184.      * @return boolean 
  185.      */
  186.     function sameReferences( &$a_obj, &$b_obj )
  187.     {
  188.         $a_obj->____bool_same_checker = true;
  189.         $bool_out = !empty( $b_obj->____bool_same_checker );
  190.         unset( $a_obj->____bool_same_checker );
  191.  
  192.         return $bool_out;
  193.     }
  194.  
  195.  
  196.  
  197.     /**
  198.      * Checks whether the passed object reference points to this object.
  199.      *
  200.      * @require PHP 4
  201.      * @version 1.0
  202.      * @since   0.1.24
  203.      * @access  public
  204.      *
  205.      * @param  ABSTObject $obj 
  206.      *
  207.      * @return boolean 
  208.      */
  209.     function same( &$obj )
  210.     {
  211.         if ( !is_a( $obj, "ABSTObject" ) )
  212.         { return false; }
  213.  
  214.         return ABSTObject::sameReferences( $this, $obj );
  215.     }
  216.  
  217.  
  218.  
  219.     /**
  220.      * Prints out the Structure of this Object.
  221.      *
  222.      * @access  public
  223.      * @require PHP 4
  224.      * @version 1.01
  225.      * @since   0.1.0
  226.      *
  227.      * @param   string $exept_reg_exp No Fieldnames that match this regular
  228.      *                                 expression will print out.
  229.      *
  230.      * @return  void 
  231.      */
  232.     function printStructure( $exept_reg_exp = null )
  233.     {
  234.         $handled_object_arr = array();
  235.         $primitive_types_arr = array();
  236.         $primitive_array_arr = array();
  237.         $multidimensional_arr = array();
  238.         $unhandled_object_arr = array();
  239.  
  240.         # Getting Fields
  241.         foreach ( $this as $key => $val )
  242.         if (
  243.              !is_string( $exept_reg_exp )
  244.           || !preg_match( $exept_reg_exp, $key )
  245.            )
  246.         {
  247.             if (
  248.                  is_object( $this->$key )
  249.               && method_exists ( $this->$key, "printStructure" )
  250.                )
  251.             {  $handled_object_arr[$key] =& $this->$key; }
  252.             else
  253.             if ( is_object( $this->$key ) )
  254.             {  $unhandled_object_arr[$key] =& $this->$key; }
  255.             else
  256.             if ( !is_array( $this->$key ) )
  257.             {  $primitive_types_arr[$key] =& $this->$key; }
  258.             else
  259.             {
  260.                $is_complex = false;
  261.                foreach ( $this->$key as $kval )
  262.                     if ( is_array( $kval ) || is_object( $kval ) )
  263.                     {
  264.                        $is_complex = true;
  265.                        break;
  266.                     }
  267.  
  268.                if ( $is_complex )
  269.                   $multidimensional_arr[$key] =& $this->$key;
  270.                else
  271.                   $primitive_array_arr[$key] =& $this->$key;
  272.             }
  273.         }
  274.  
  275.  
  276.         echo "<h1>Object: ".get_class( $this )."</h1>";
  277.  
  278.         $this->printPrimitiveData(
  279.                         $primitive_types_arr,
  280.                         $primitive_array_arr
  281.                                  );
  282.  
  283.         if ( count( $multidimensional_arr ) > 0 )
  284.         {
  285.             ob_start();
  286.             $this->printComplexData(
  287.                                       $multidimensional_arr,
  288.                                       array(), $exept_reg_exp
  289.                                    );
  290.             $cont = ob_get_contents();
  291.             ob_end_clean();
  292.  
  293.             echo "<p>\r\n";
  294.             echo "<h2>Complex Data <font size=\"1\">["
  295.                             .get_class( $this )
  296.                                 ."]</font>:</h2>\r\n";
  297.             echo "<blockquote>\r\n";
  298.             if ( !empty( $cont ) )
  299.                echo $cont;
  300.             else
  301.                echo "{Some multidimensional arrays without references to "
  302.                     ."objects (I think not neccessary to display!)}";
  303.             echo "</blockquote>\r\n";
  304.             echo "</p>\r\n";
  305.         }
  306.  
  307.         if ( count( $unhandled_object_arr ) > 0 )
  308.         {
  309.             echo "<p>\r\n";
  310.             echo "<h2>Unhandled Objects <font size=\"1\">["
  311.                             .get_class( $this )
  312.                                      ."]</font>:</h2>\r\n";
  313.             echo "<blockquote>\r\n";
  314.             foreach ( $unhandled_object_arr as $nam => $value )
  315.                echo "<b>{$nam}</b>: *Object "
  316.                             .get_class( $unhandled_object_arr[$nam] )
  317.                                  ."<br />\r\n";
  318.             echo "</blockquote>\r\n";
  319.             echo "</p>\r\n";
  320.         }
  321.  
  322.         if ( count( $handled_object_arr ) > 0 )
  323.         {
  324.             echo "<p>\r\n";
  325.             echo "<h2>Handled Objects <font size=\"1\">["
  326.                             .get_class( $this )
  327.                                    ."]</font>:</h2>\r\n";
  328.             echo "<blockquote>\r\n";
  329.             foreach ( $handled_object_arr as $nam => $value )
  330.             {
  331.                 echo "<b>{$nam}</b>:\r\n";
  332.                 echo "<blockquote>\r\n";
  333.                 echo $handled_object_arr[$nam]->printStructure($exept_reg_exp);
  334.                 echo "</blockquote>\r\n";
  335.                 echo "<br />\r\n";
  336.             }
  337.             echo "</blockquote>\r\n";
  338.             echo "</p>\r\n";
  339.         }
  340.     }
  341. } // End of class ABSTObject
  342. ?>

Documentation generated on Thu, 05 Jun 2008 19:10:00 +0200 by phpDocumentor 1.4.1