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.                         if ($this->options['is_html']{
  125.                             $txt .= htmlspecialchars($this->value[$c]);
  126.                         else {
  127.                             $txt .= $this->value[$c];
  128.                         }
  129.                         $txt .= ' ' $this->options['opening'"\n";
  130.                     }
  131.                     break;
  132.                 case VAR_DUMP_FINISH_GROUP :
  133.                     if ($this->depth[$c> 0{
  134.                         $offset $this->depth[$c$this->options['offset'];
  135.                         if ($this->options['mode'== 'wide'{
  136.                             $offset += end($stackOffset);
  137.                         }
  138.                         if (!$this->options['show_container']{
  139.                             $offset -= $this->options['offset'];
  140.                         }
  141.                         $txt .= str_repeat(' '$offset);
  142.                     }
  143.                     if ($this->options['show_container'or $this->depth[$c> 0{
  144.                         $txt .= $this->options['closing'"\n";
  145.                     }
  146.                     array_pop($parent);
  147.                     array_pop($stackOffset);
  148.                     break;
  149.                 case VAR_DUMP_START_ELEMENT_NUM :
  150.                 case VAR_DUMP_START_ELEMENT_STR :
  151.                     if ($this->depth[$c> 0{
  152.                         $offset $this->depth[$c$this->options['offset'];
  153.                         if ($this->options['mode'== 'wide'{
  154.                             $offset += end($stackOffset);
  155.                         }
  156.                         if ($this->options['show_container']{
  157.                             $offset -= $this->options['offset'];
  158.                         }
  159.                         $txt .= str_repeat(' '$offset);
  160.                     }
  161.                     if ($this->options['mode'== 'compact'{
  162.                         $txt .= $this->_getStartElement($c);
  163.                         $offset += $this->_len($this->value[$c]);
  164.                     else {
  165.                         $txt .= sprintf(
  166.                             '%-' $this->keyLen[end($parent)'s',
  167.                             $this->_getStartElement($c)
  168.                         );
  169.                         $offset += $this->keyLen[end($parent)];
  170.                     }
  171.                     $txt .= $this->_getOperator();
  172.                     if ($this->family[$c]==VAR_DUMP_START_ELEMENT_NUM{
  173.                         $offset +=
  174.                             $this->_len($this->options['before_num_key']+
  175.                             $this->_len($this->options['after_num_key']);
  176.                     }
  177.                     if ($this->family[$c]==VAR_DUMP_START_ELEMENT_STR{
  178.                         $offset +=
  179.                             $this->_len($this->options['before_str_key']+
  180.                             $this->_len($this->options['after_str_key']);
  181.                     }
  182.                     $offset +=
  183.                         $this->_len($this->options['before_operator']+
  184.                         $this->_len($this->options['operator']+
  185.                         $this->_len($this->options['after_operator']+
  186.                         $this->_len($this->options['before_type']+
  187.                         $this->_len($this->options['after_type']);
  188.                     break;
  189.                 case VAR_DUMP_FINISH_ELEMENT :
  190.                     $txt .= $this->_getFinishElement($c"\n";
  191.                     break;
  192.                 case VAR_DUMP_FINISH_STRING :
  193.                     // offset is the value set during the previous pass
  194.                     // in VAR_DUMP_START_ELEMENT_*
  195.                     $txt .= preg_replace(
  196.                         '/(?<=\n)^/m',
  197.                         $this->options['after_value'.
  198.                             str_repeat(' '$offset $this->_len($this->type[$c]+ 1.
  199.                             $this->options['before_value'],
  200.                         $this->_getFinishElement($c)
  201.                     "\n";
  202.                     break;
  203.             }
  204.         }
  205.         $txt .= $this->options['after_text'];
  206.         return rtrim($txt);
  207.     }
  208.  
  209.     /**
  210.      * Returns the lenght of the shift (string without tags).
  211.      *
  212.      * @param string $string The string.
  213.      * @return integer Length of the shift.
  214.      * @access private
  215.      */
  216.     function _len($string)
  217.     {
  218.         if ($this->options['is_html']{
  219.             return strlen(strip_tags($string));
  220.         else {
  221.             return strlen($string);
  222.         }
  223.     }
  224.  
  225.     /**
  226.      * Returns the operator symbol.
  227.      *
  228.      * @return string The operator symbol.
  229.      * @access private
  230.      */
  231.     function _getOperator()
  232.     {
  233.         $txt $this->options['before_operator'];
  234.         if ($this->options['is_html']{
  235.             $txt .= htmlspecialchars($this->options['operator']);
  236.         else {
  237.             $txt .= $this->options['operator'];
  238.         }
  239.         $txt .= $this->options['after_operator'];
  240.         return $txt;
  241.     }
  242.  
  243.     /**
  244.      * Returns the key of the element.
  245.      *
  246.      * @param integer $c Index of the element.
  247.      * @return string The key of the element.
  248.      * @access private
  249.      */
  250.     function _getStartElement($c)
  251.     {
  252.         $comp ($this->family[$c== VAR_DUMP_START_ELEMENT_NUM'num' 'str';
  253.         $txt $this->options['before_' $comp '_key'];
  254.         if ($this->options['is_html']{
  255.             $txt .= htmlspecialchars($this->value[$c]);
  256.         else {
  257.             $txt .= $this->value[$c];
  258.         }
  259.         $txt .= $this->options['after_' $comp '_key'];
  260.         return $txt;
  261.     }
  262.  
  263.     /**
  264.      * Returns the value of the element.
  265.      *
  266.      * @param integer $c Index of the element.
  267.      * @return string The value of the element.
  268.      * @access private
  269.      */
  270.     function _getFinishElement($c)
  271.     {
  272.         $txt $this->options['before_type'];
  273.         if ($this->options['is_html']{
  274.             $txt .= htmlspecialchars($this->type[$c]);
  275.         else {
  276.             $txt .= $this->type[$c];
  277.         }
  278.         $txt .= $this->options['after_type'];
  279.         if (is_null($this->value[$c])) {
  280.             $txt .= ' ' $this->options['before_value'];
  281.             if ($this->options['is_html']{
  282.                 $string htmlspecialchars($this->value[$c]);
  283.             else {
  284.                 $string $this->value[$c];
  285.             }
  286.             if ($this->options['show_eol'!== FALSE{
  287.                 $string str_replace(
  288.                     "\n",
  289.                     $this->options['show_eol'"\n",
  290.                     $string
  291.                 );
  292.             }
  293.             $txt .= $string $this->options['after_value'];
  294.         }
  295.         return $txt;
  296.     }
  297.  
  298. }
  299.  
  300. ?>

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