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.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.         parent::Var_Dump_Renderer($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.                         str_repeat(' '$offset strlen($this->type[$c]+ 1),
  172.                         $this->getFinishElement($c)
  173.                     "\n";
  174.                     break;
  175.             }
  176.         }
  177.         $txt .= $this->options['after_text'];
  178.         return rtrim($txt);
  179.     }
  180.     // }}}
  181.  
  182.     // {{{ getOperator()
  183.     /**
  184.      * Returns the operator symbol.
  185.      * @return string The operator symbol.
  186.      * @access public
  187.      */
  188.     function getOperator()
  189.     {
  190.         $txt $this->options['before_operator'];
  191.         if ($this->options['is_html']{
  192.             $txt .= htmlspecialchars($this->options['operator']);
  193.         else {
  194.             $txt .= $this->options['operator'];
  195.         }
  196.         $txt .= $this->options['after_operator'];
  197.         return $txt;
  198.     }
  199.     // }}}
  200.  
  201.     // {{{ getStartElement()
  202.     /**
  203.      * Returns the key of the element.
  204.      * @param  integer $c Index of the element.
  205.      * @return string     The key of the element.
  206.      * @access public
  207.      */
  208.     function getStartElement($c)
  209.     {
  210.         $comp ($this->family[$c== VAR_DUMP_START_ELEMENT_NUM'num' 'str';
  211.         $txt $this->options['before_'.$comp.'_key'];
  212.         if ($this->options['is_html']{
  213.             $txt .= htmlspecialchars($this->value[$c]);
  214.         else {
  215.             $txt .= $this->value[$c];
  216.         }
  217.         $txt .= $this->options['after_'.$comp.'_key'];
  218.         return $txt;
  219.     }
  220.     // }}}
  221.  
  222.     // {{{ getFinishElement()
  223.     /**
  224.      * Returns the value of the element.
  225.      * @param  integer $c Index of the element.
  226.      * @return string     The value of the element.
  227.      * @access public
  228.      */
  229.     function getFinishElement($c)
  230.     {
  231.         $txt $this->options['before_type'];
  232.         if ($this->options['is_html']{
  233.             $txt .= htmlspecialchars($this->type[$c]);
  234.         else {
  235.             $txt .= $this->type[$c];
  236.         }
  237.         $txt .= $this->options['after_type'];
  238.         if (!empty($this->value[$c])) {
  239.             $txt .= ' ' $this->options['before_value'];
  240.             if ($this->options['is_html']{
  241.                 $txt .= htmlspecialchars($this->value[$c]);
  242.             else {
  243.                 $txt .= $this->value[$c];
  244.             }
  245.             $txt .= $this->options['after_value'];
  246.         }
  247.         return $txt;
  248.     }
  249.     // }}}
  250.     
  251. }
  252.  
  253. ?>

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