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

Source for file Text.php

Documentation is available at Text.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Frederic Poeydomenge <fpoeydomenge at free dot fr>          |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id$
  20.  
  21. require_once 'Var_Dump/Renderer/Common.php';
  22.  
  23. /**
  24.  * A concrete renderer for Var_Dump
  25.  *
  26.  * Returns a text-only representation of a variable
  27.  *
  28.  * @package Var_Dump
  29.  * @category PHP
  30.  * @author Frederic Poeydomenge <fpoeydomenge at free dot fr>
  31.  */
  32.  
  33. {
  34.  
  35.     /**
  36.      * Default configuration options.
  37.      *
  38.      * Valid configuration options are :
  39.      *     show_container  : bool,    Show the root Element or not
  40.      *     show_eol        : string,  String to insert before a newline, or false
  41.      *     mode            : string,  Can be one of the following displaying modes
  42.      *       'compact' = no keys alignment
  43.      *       'normal'  = keys alignment, proportional spacing
  44.      *       'wide'    = keys alignment, wider spacing
  45.      *     offset          : integer, Offset between the start of a group and the content
  46.      *     opening         : string,  Opening character
  47.      *     closing         : string,  Closing character
  48.      *     operator        : string,  Operator symbol
  49.      *     is_html         : bool,    Do we need to htmlspecialchars() the texts
  50.      *     before_text     : string,  Text to insert before the text
  51.      *     after_text      : string,  Text to insert after the text
  52.      *     before_num_key  : string,  Text to insert before a numerical key
  53.      *     after_num_key   : string,  Text to insert after a numerical key
  54.      *     before_str_key  : string,  Text to insert before a string key
  55.      *     after_str_key   : string,  Text to insert after a string key
  56.      *     before_operator : string,  Text to insert before the operator
  57.      *     after_operator  : string,  Text to insert after the operator
  58.      *     before_type     : string,  Text to insert before a type
  59.      *     after_type      : string,  Text to insert after a type
  60.      *     before_value    : string,  Text to insert before a value
  61.      *     after_value     : string,  Text to insert after a value
  62.      *
  63.      * @var    array 
  64.      * @access public
  65.      */
  66.     var $defaultOptions = array(
  67.         'show_container'  => TRUE,
  68.         'show_eol'        => FALSE,
  69.         'mode'            => 'compact',
  70.         'offset'          => 2,
  71.         'opening'         => '{',
  72.         'closing'         => '}',
  73.         'operator'        => ' => ',
  74.         'is_html'         => FALSE,
  75.         'before_text'     => '',
  76.         'after_text'      => '',
  77.         'before_num_key'  => '',
  78.         'after_num_key'   => '',
  79.         'before_str_key'  => '',
  80.         'after_str_key'   => '',
  81.         'before_operator' => '',
  82.         'after_operator'  => '',
  83.         'before_type'     => '',
  84.         'after_type'      => '',
  85.         'before_value'    => '',
  86.         'after_value'     => ''
  87.     );
  88.  
  89.     /**
  90.      * Class constructor.
  91.      *
  92.      * @param array $options Parameters for the rendering.
  93.      * @access public
  94.      */
  95.     function Var_Dump_Renderer_Text($options = array())
  96.     {
  97.         $this->setOptions($options);
  98.     }
  99.  
  100.     /**
  101.      * Returns the string representation of a variable.
  102.      *
  103.      * @return string The string representation of the variable.
  104.      * @access public
  105.      */
  106.     function toString()
  107.     {
  108.         $parent = array();
  109.         $stackOffset = array(0);
  110.         $offset = 0;
  111.         $txt $this->options['before_text'];
  112.         $counter count($this->family);
  113.         for ($c = 0 ; $c $counter $c++{
  114.             switch ($this->family[$c]{
  115.                 case VAR_DUMP_START_GROUP :
  116.                     if (empty($parent)) {
  117.                         $offset end($stackOffset)
  118.                             + $this->keyLen[end($parent)]
  119.                             + $this->_len($this->options['operator']);
  120.                         array_push($stackOffset$offset);
  121.                     }
  122.                     array_push($parent$c);
  123.                     if ($this->options['show_container'or $this->depth[$c> 0{
  124.                         $txt .= $this->value[$c' ' $this->options['opening'"\n";
  125.                     }
  126.                     break;
  127.                 case VAR_DUMP_FINISH_GROUP :
  128.                     if ($this->depth[$c> 0{
  129.                         $offset $this->depth[$c$this->options['offset'];
  130.                         if ($this->options['mode'== 'wide'{
  131.                             $offset += end($stackOffset);
  132.                         }
  133.                         if (!$this->options['show_container']{
  134.                             $offset -= $this->options['offset'];
  135.                         }
  136.                         $txt .= str_repeat(' '$offset);
  137.                     }
  138.                     if ($this->options['show_container'or $this->depth[$c> 0{
  139.                         $txt .= $this->options['closing'"\n";
  140.                     }
  141.                     array_pop($parent);
  142.                     array_pop($stackOffset);
  143.                     break;
  144.                 case VAR_DUMP_START_ELEMENT_NUM :
  145.                 case VAR_DUMP_START_ELEMENT_STR :
  146.                     if ($this->depth[$c> 0{
  147.                         $offset $this->depth[$c$this->options['offset'];
  148.                         if ($this->options['mode'== 'wide'{
  149.                             $offset += end($stackOffset);
  150.                         }
  151.                         if ($this->options['show_container']{
  152.                             $offset -= $this->options['offset'];
  153.                         }
  154.                         $txt .= str_repeat(' '$offset);
  155.                     }
  156.                     if ($this->options['mode'== 'compact'{
  157.                         $txt .= $this->_getStartElement($c);
  158.                         $offset += $this->_len($this->value[$c]);
  159.                     else {
  160.                         $txt .= sprintf(
  161.                             '%-' $this->keyLen[end($parent)'s',
  162.                             $this->_getStartElement($c)
  163.                         );
  164.                         $offset += $this->keyLen[end($parent)];
  165.                     }
  166.                     $txt .= $this->_getOperator();
  167.                     if ($this->family[$c]==VAR_DUMP_START_ELEMENT_NUM{
  168.                         $offset +=
  169.                             $this->_len($this->options['before_num_key']+
  170.                             $this->_len($this->options['after_num_key']);
  171.                     }
  172.                     if ($this->family[$c]==VAR_DUMP_START_ELEMENT_STR{
  173.                         $offset +=
  174.                             $this->_len($this->options['before_str_key']+
  175.                             $this->_len($this->options['after_str_key']);
  176.                     }
  177.                     $offset +=
  178.                         $this->_len($this->options['before_operator']+
  179.                         $this->_len($this->options['operator']+
  180.                         $this->_len($this->options['after_operator']+
  181.                         $this->_len($this->options['before_type']+
  182.                         $this->_len($this->options['after_type']);
  183.                     break;
  184.                 case VAR_DUMP_FINISH_ELEMENT :
  185.                     $txt .= $this->_getFinishElement($c"\n";
  186.                     break;
  187.                 case VAR_DUMP_FINISH_STRING :
  188.                     // offset is the value set during the previous pass
  189.                     // in VAR_DUMP_START_ELEMENT_*
  190.                     $txt .= preg_replace(
  191.                         '/(?<=\n)^/m',
  192.                         $this->options['after_value'.
  193.                             str_repeat(' '$offset $this->_len($this->type[$c]+ 1.
  194.                             $this->options['before_value'],
  195.                         $this->_getFinishElement($c)
  196.                     "\n";
  197.                     break;
  198.             }
  199.         }
  200.         $txt .= $this->options['after_text'];
  201.         return rtrim($txt);
  202.     }
  203.  
  204.     /**
  205.      * Returns the lenght of the shift (string without tags).
  206.      *
  207.      * @param string $string The string.
  208.      * @return integer Length of the shift.
  209.      * @access private
  210.      */
  211.     function _len($string)
  212.     {
  213.         if ($this->options['is_html']{
  214.             return strlen(strip_tags($string));
  215.         else {
  216.             return strlen($string);
  217.         }
  218.     }
  219.  
  220.     /**
  221.      * Returns the operator symbol.
  222.      *
  223.      * @return string The operator symbol.
  224.      * @access private
  225.      */
  226.     function _getOperator()
  227.     {
  228.         $txt $this->options['before_operator'];
  229.         if ($this->options['is_html']{
  230.             $txt .= htmlspecialchars($this->options['operator']);
  231.         else {
  232.             $txt .= $this->options['operator'];
  233.         }
  234.         $txt .= $this->options['after_operator'];
  235.         return $txt;
  236.     }
  237.  
  238.     /**
  239.      * Returns the key of the element.
  240.      *
  241.      * @param integer $c Index of the element.
  242.      * @return string The key of the element.
  243.      * @access private
  244.      */
  245.     function _getStartElement($c)
  246.     {
  247.         $comp ($this->family[$c== VAR_DUMP_START_ELEMENT_NUM'num' 'str';
  248.         $txt $this->options['before_' $comp '_key'];
  249.         if ($this->options['is_html']{
  250.             $txt .= htmlspecialchars($this->value[$c]);
  251.         else {
  252.             $txt .= $this->value[$c];
  253.         }
  254.         $txt .= $this->options['after_' $comp '_key'];
  255.         return $txt;
  256.     }
  257.  
  258.     /**
  259.      * Returns the value of the element.
  260.      *
  261.      * @param integer $c Index of the element.
  262.      * @return string The value of the element.
  263.      * @access private
  264.      */
  265.     function _getFinishElement($c)
  266.     {
  267.         $txt $this->options['before_type'];
  268.         if ($this->options['is_html']{
  269.             $txt .= htmlspecialchars($this->type[$c]);
  270.         else {
  271.             $txt .= $this->type[$c];
  272.         }
  273.         $txt .= $this->options['after_type'];
  274.         if (is_null($this->value[$c])) {
  275.             $txt .= ' ' $this->options['before_value'];
  276.             if ($this->options['is_html']{
  277.                 $string htmlspecialchars($this->value[$c]);
  278.             else {
  279.                 $string $this->value[$c];
  280.             }
  281.             if ($this->options['show_eol'!== FALSE{
  282.                 $string str_replace(
  283.                     "\n",
  284.                     $this->options['show_eol'"\n",
  285.                     $string
  286.                 );
  287.             }
  288.             $txt .= $string $this->options['after_value'];
  289.         }
  290.         return $txt;
  291.     }
  292.  
  293. }
  294.  
  295. ?>

Documentation generated on Mon, 11 Mar 2019 14:23:43 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.