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 <frederic.poeydomenge@free.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 <frederic.poeydomenge@free.fr>
  31.  */
  32.  
  33. {
  34.  
  35.     /**
  36.      * Default configuration options.
  37.      *     show_container  : bool,    Show the root Element or not
  38.      *     mode            : string,  Can be one of the following displaying modes
  39.      *       'compact' = no keys alignment
  40.      *       'normal'  = keys alignment, proportional spacing
  41.      *       'wide'    = keys alignment, wider spacing
  42.      *     offset          : integer, Offset between the start of a group and the content
  43.      *     opening         : string,  Opening character
  44.      *     closing         : string,  Closing character
  45.      *     operator        : string,  Operator symbol
  46.      *     is_html         : bool,    Do we need to htmlspecialchars() the texts
  47.      *     before_text     : string,  Text to insert before the text
  48.      *     after_text      : string,  Text to insert after the text
  49.      *     before_num_key  : string,  Text to insert before a numerical key
  50.      *     after_num_key   : string,  Text to insert after a numerical key
  51.      *     before_str_key  : string,  Text to insert before a string key
  52.      *     after_str_key   : string,  Text to insert after a string key
  53.      *     before_operator : string,  Text to insert before the operator
  54.      *     after_operator  : string,  Text to insert after the operator
  55.      *     before_type     : string,  Text to insert before a type
  56.      *     after_type      : string,  Text to insert after a type
  57.      *     before_value    : string,  Text to insert before a value
  58.      *     after_value     : string,  Text to insert after a value
  59.      * @var    array 
  60.      * @access public
  61.      */
  62.     var $defaultOptions = array(
  63.         'show_container'  => TRUE,
  64.         'mode'            => 'compact',
  65.         'offset'          => 2,
  66.         'opening'         => '{',
  67.         'closing'         => '}',
  68.         'operator'        => ' => ',
  69.         'is_html'         => FALSE,
  70.         'before_text'     => '',
  71.         'after_text'      => '',
  72.         'before_num_key'  => '',
  73.         'after_num_key'   => '',
  74.         'before_str_key'  => '',
  75.         'after_str_key'   => '',
  76.         'before_operator' => '',
  77.         'after_operator'  => '',
  78.         'before_type'     => '',
  79.         'after_type'      => '',
  80.         'before_value'    => '',
  81.         'after_value'     => ''
  82.     );
  83.  
  84.     // {{{ constructor
  85.     /**
  86.      * Class constructor.
  87.      * @param array $options Parameters for the rendering.
  88.      * @access public
  89.      */
  90.     function Var_Dump_Renderer_Text($options = array())
  91.     {
  92.         $this->setOptions($options);
  93.     }
  94.     // }}}
  95.  
  96.     // {{{ toString()
  97.     /**
  98.      * Returns the string representation of a variable.
  99.      * @return string The string representation of the variable.
  100.      * @access public
  101.      */
  102.     function toString()
  103.     {
  104.         $parent = array();
  105.         $stackOffset = array(0);
  106.         $txt $this->options['before_text'];
  107.         $counter count($this->family);
  108.         for ($c = 0 ; $c $counter $c++{
  109.             switch ($this->family[$c]{
  110.                 case VAR_DUMP_START_GROUP :
  111.                     if(!empty($parent)) {
  112.                         $offset end($stackOffset)
  113.                             + $this->keyLen[end($parent)]
  114.                             + strlen($this->options['operator']);
  115.                         array_push($stackOffset$offset);
  116.                     }
  117.                     array_push($parent$c);
  118.                     if ($this->options['show_container'or $this->depth[$c> 0{
  119.                         $txt .= $this->value[$c' ' $this->options['opening'"\n";
  120.                     }
  121.                     break;
  122.                 case VAR_DUMP_FINISH_GROUP :
  123.                     if ($this->depth[$c> 0{
  124.                         $offset $this->depth[$c$this->options['offset'];
  125.                         if ($this->options['mode'== 'wide'{
  126.                             $offset += end($stackOffset);
  127.                         }
  128.                         if (!$this->options['show_container']{
  129.                             $offset -= $this->options['offset'];
  130.                         }
  131.                         $txt .= str_repeat(' '$offset);
  132.                     }
  133.                     if ($this->options['show_container'or $this->depth[$c> 0{
  134.                         $txt .= $this->options['closing'"\n";
  135.                     }
  136.                     array_pop($parent);
  137.                     array_pop($stackOffset);
  138.                     break;
  139.                 case VAR_DUMP_START_ELEMENT_NUM :
  140.                 case VAR_DUMP_START_ELEMENT_STR :
  141.                     if ($this->depth[$c> 0{
  142.                         $offset $this->depth[$c$this->options['offset'];
  143.                         if ($this->options['mode'== 'wide'{
  144.                             $offset += end($stackOffset);
  145.                         }
  146.                         if(!$this->options['show_container']{
  147.                             $offset -= $this->options['offset'];
  148.                         }
  149.                         $txt .= str_repeat(' '$offset);
  150.                     }
  151.                     if ($this->options['mode'== 'compact'{
  152.                         $txt .= $this->_getStartElement($c);
  153.                         $offset += strlen($this->value[$c]);
  154.                     else {
  155.                         $txt .= sprintf(
  156.                             '%-' $this->keyLen[end($parent)'s',
  157.                             $this->_getStartElement($c)
  158.                         );
  159.                         $offset += $this->keyLen[end($parent)];
  160.                     }
  161.                     $txt .= $this->_getOperator();
  162.                     $offset += strlen($this->options['operator']);
  163.                     break;
  164.                 case VAR_DUMP_FINISH_ELEMENT :
  165.                     $txt .= $this->_getFinishElement($c"\n";
  166.                     break;
  167.                 case VAR_DUMP_FINISH_ELEMENT_NL :
  168.                     // offset is the value set during the previous pass in VAR_DUMP_START_ELEMENT_*
  169.                     $txt .= preg_replace(
  170.                         '/(?<=\n)^/m',
  171.                                                 $this->options['after_value'.
  172.                             str_repeat(' '$offset strlen($this->type[$c]+ 1.
  173.                             $this->options['before_value'],
  174.                         $this->_getFinishElement($c)
  175.                     "\n";
  176.                     break;
  177.             }
  178.         }
  179.         $txt .= $this->options['after_text'];
  180.         return rtrim($txt);
  181.     }
  182.     // }}}
  183.  
  184.     // {{{ _getOperator()
  185.     /**
  186.      * Returns the operator symbol.
  187.      * @return string The operator symbol.
  188.      * @access private
  189.      */
  190.     function _getOperator()
  191.     {
  192.         $txt $this->options['before_operator'];
  193.         if ($this->options['is_html']{
  194.             $txt .= htmlspecialchars($this->options['operator']);
  195.         else {
  196.             $txt .= $this->options['operator'];
  197.         }
  198.         $txt .= $this->options['after_operator'];
  199.         return $txt;
  200.     }
  201.     // }}}
  202.  
  203.     // {{{ _getStartElement()
  204.     /**
  205.      * Returns the key of the element.
  206.      * @param integer $c Index of the element.
  207.      * @return string The key of the element.
  208.      * @access private
  209.      */
  210.     function _getStartElement($c)
  211.     {
  212.         $comp ($this->family[$c== VAR_DUMP_START_ELEMENT_NUM'num' 'str';
  213.         $txt $this->options['before_'.$comp.'_key'];
  214.         if ($this->options['is_html']{
  215.             $txt .= htmlspecialchars($this->value[$c]);
  216.         else {
  217.             $txt .= $this->value[$c];
  218.         }
  219.         $txt .= $this->options['after_'.$comp.'_key'];
  220.         return $txt;
  221.     }
  222.     // }}}
  223.  
  224.     // {{{ _getFinishElement()
  225.     /**
  226.      * Returns the value of the element.
  227.      * @param integer $c Index of the element.
  228.      * @return string The value of the element.
  229.      * @access private
  230.      */
  231.     function _getFinishElement($c)
  232.     {
  233.         $txt $this->options['before_type'];
  234.         if ($this->options['is_html']{
  235.             $txt .= htmlspecialchars($this->type[$c]);
  236.         else {
  237.             $txt .= $this->type[$c];
  238.         }
  239.         $txt .= $this->options['after_type'];
  240.         if (!empty($this->value[$c])) {
  241.             $txt .= ' ' $this->options['before_value'];
  242.             if ($this->options['is_html']{
  243.                 $txt .= htmlspecialchars($this->value[$c]);
  244.             else {
  245.                 $txt .= $this->value[$c];
  246.             }
  247.             $txt .= $this->options['after_value'];
  248.         }
  249.         return $txt;
  250.     }
  251.     // }}}
  252.     
  253. }
  254.  
  255. ?>

Documentation generated on Mon, 11 Mar 2019 10:16:32 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.