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

Source for file DM.php

Documentation is available at DM.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: HTML :: Progress                                             |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2004 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 3.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/3_0.txt.                                  |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Author: Laurent Laville <pear@laurent-laville.org>                   |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: DM.php,v 1.4 2004/07/03 14:48:14 farell Exp $
  19.  
  20. /**
  21.  * The HTML_Progress_DM class handles any mathematical issues
  22.  * arising from assigning faulty values.
  23.  *
  24.  * @version    1.2.0
  25.  * @author     Laurent Laville <pear@laurent-laville.org>
  26.  * @access     public
  27.  * @package    HTML_Progress
  28.  * @subpackage Progress_DM
  29.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  30.  */
  31.  
  32. {
  33.     /**
  34.      * The progress bar's minimum value.
  35.      * The default is 0.
  36.      *
  37.      * @var        integer 
  38.      * @since      1.0
  39.      * @access     private
  40.      * @see        getMinimum(), setMinimum()
  41.      */
  42.     var $_minimum;
  43.  
  44.     /**
  45.      * The progress bar's maximum value.
  46.      * The default is 100.
  47.      *
  48.      * @var        integer 
  49.      * @since      1.0
  50.      * @access     private
  51.      * @see        getMaximum(), setMaximum()
  52.      */
  53.     var $_maximum;
  54.  
  55.     /**
  56.      * The progress bar's increment value.
  57.      * The default is +1.
  58.      *
  59.      * @var        integer 
  60.      * @since      1.0
  61.      * @access     private
  62.      * @see        getIncrement(), setIncrement()
  63.      */
  64.     var $_increment;
  65.  
  66.     /**
  67.      * The progress bar's current value.
  68.      *
  69.      * @var        integer 
  70.      * @since      1.0
  71.      * @access     private
  72.      * @see        getValue(), setvalue(), incValue()
  73.      */
  74.     var $_value;
  75.  
  76.     /**
  77.      * Package name used by PEAR_ErrorStack functions
  78.      *
  79.      * @var        string 
  80.      * @since      1.0
  81.      * @access     private
  82.      */
  83.     var $_package;
  84.  
  85.  
  86.     /**
  87.      * The data model class constructor
  88.      *
  89.      * Constructor Summary
  90.      *
  91.      * o Creates a progress mathematical model with a minimum value set to 0,
  92.      *   a maximum value set to 100, and a increment value set to +1.
  93.      *   By default, the value is initialized to be equal to the minimum value.
  94.      *   <code>
  95.      *   $html = new HTML_Progress_DM();
  96.      *   </code>
  97.      *
  98.      * o Creates a progress mathematical model with minimum and maximum set to
  99.      *   specified values, and a increment value set to +1.
  100.      *   By default, the value is initialized to be equal to the minimum value.
  101.      *   <code>
  102.      *   $html = new HTML_Progress_DM($min, $max);
  103.      *   </code>
  104.      *
  105.      * o Creates a progress mathematical model with minimum, maximum and increment
  106.      *   set to specified values.
  107.      *   By default, the value is initialized to be equal to the minimum value.
  108.      *   <code>
  109.      *   $html = new HTML_Progress_DM($min, $max, $inc);
  110.      *   </code>
  111.      *
  112.      * @since      1.0
  113.      * @access     public
  114.      * @throws     HTML_PROGRESS_ERROR_INVALID_INPUT
  115.      */
  116.     function HTML_Progress_DM()
  117.     {
  118.         $this->_package 'HTML_Progress';
  119.         $this->_minimum = 0;
  120.         $this->_maximum = 100;
  121.         $this->_increment = +1;
  122.  
  123.         $args func_get_args();
  124.         
  125.         switch (count($args)) {
  126.          case 2:
  127.             /*   int min, int max   */
  128.  
  129.             if (!is_int($args[0])) {
  130.                 return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT'exception',
  131.                     array('var' => '$min',
  132.                           'was' => $args[0],
  133.                           'expected' => 'integer',
  134.                           'paramnum' => 1));
  135.  
  136.             elseif ($args[0< 0{
  137.                 return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT'error',
  138.                     array('var' => '$min',
  139.                           'was' => $args[0],
  140.                           'expected' => 'positive',
  141.                           'paramnum' => 1));
  142.  
  143.             elseif ($args[0$args[1]{
  144.                 return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT'error',
  145.                     array('var' => '$min',
  146.                           'was' => $args[0],
  147.                           'expected' => 'less than $max = '.$args[1],
  148.                           'paramnum' => 1));
  149.             }
  150.             $this->_minimum $args[0];
  151.  
  152.  
  153.             if (!is_int($args[1])) {
  154.                 return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT'exception',
  155.                     array('var' => '$max',
  156.                           'was' => $args[1],
  157.                           'expected' => 'integer',
  158.                           'paramnum' => 2));
  159.  
  160.             elseif ($args[1< 0{
  161.                 return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT'error',
  162.                     array('var' => '$max',
  163.                           'was' => $args[1],
  164.                           'expected' => 'positive',
  165.                           'paramnum' => 2));
  166.             }
  167.             $this->_maximum $args[1];
  168.             break;
  169.          case 3:
  170.             /*   int min, int max, int inc   */
  171.  
  172.             if (!is_int($args[0])) {
  173.                 return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT'exception',
  174.                     array('var' => '$min',
  175.                           'was' => $args[0],
  176.                           'expected' => 'integer',
  177.                           'paramnum' => 1));
  178.  
  179.             elseif ($args[0< 0{
  180.                 return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT'error',
  181.                     array('var' => '$min',
  182.                           'was' => $args[0],
  183.                           'expected' => 'positive',
  184.                           'paramnum' => 1));
  185.  
  186.             elseif ($args[0$args[1]{
  187.                 return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT'error',
  188.                     array('var' => '$min',
  189.                           'was' => $args[0],
  190.                           'expected' => 'less than $max = '.$args[1],
  191.                           'paramnum' => 1));
  192.             }
  193.             $this->_minimum $args[0];
  194.  
  195.             if (!is_int($args[1])) {
  196.                 return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT'exception',
  197.                     array('var' => '$max',
  198.                           'was' => $args[1],
  199.                           'expected' => 'integer',
  200.                           'paramnum' => 2));
  201.  
  202.             elseif ($args[1< 0{
  203.                 return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT'error',
  204.                     array('var' => '$max',
  205.                           'was' => $args[1],
  206.                           'expected' => 'positive',
  207.                           'paramnum' => 2));
  208.             }
  209.             $this->_maximum $args[1];
  210.  
  211.             if (!is_int($args[2])) {
  212.                 return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT'exception',
  213.                     array('var' => '$inc',
  214.                           'was' => $args[2],
  215.                           'expected' => 'integer',
  216.                           'paramnum' => 3));
  217.  
  218.             elseif ($args[2< 1{
  219.                 return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT'error',
  220.                     array('var' => '$inc',
  221.                           'was' => $args[2],
  222.                           'expected' => 'greater than zero',
  223.                           'paramnum' => 3));
  224.             }
  225.             $this->_increment $args[2];
  226.             break;
  227.          default:
  228.         }
  229.         $this->_value $this->_minimum;
  230.     }
  231.  
  232.     /**
  233.      * Returns the progress bar's minimum value. The default value is 0.
  234.      *
  235.      * @return     integer 
  236.      * @since      1.0
  237.      * @access     public
  238.      * @see        setMinimum()
  239.      * @tutorial   dm.getminimum.pkg
  240.      */
  241.     function getMinimum()
  242.     {
  243.         return $this->_minimum;
  244.     }
  245.  
  246.     /**
  247.      * Sets the progress bar's minimum value.
  248.      *
  249.      * @param      integer   $min           progress bar's minimal value
  250.      *
  251.      * @return     void 
  252.      * @since      1.0
  253.      * @access     public
  254.      * @throws     HTML_PROGRESS_ERROR_INVALID_INPUT
  255.      * @see        getMinimum()
  256.      * @tutorial   dm.setminimum.pkg
  257.      */
  258.     function setMinimum($min)
  259.     {
  260.         if (!is_int($min)) {
  261.             return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT'exception',
  262.                 array('var' => '$min',
  263.                       'was' => gettype($min),
  264.                       'expected' => 'integer',
  265.                       'paramnum' => 1));
  266.  
  267.         elseif ($min < 0{
  268.             return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT'error',
  269.                 array('var' => '$min',
  270.                       'was' => $min,
  271.                       'expected' => 'positive',
  272.                       'paramnum' => 1));
  273.  
  274.         elseif ($min $this->getMaximum()) {
  275.             return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT'error',
  276.                 array('var' => '$min',
  277.                       'was' => $min,
  278.                       'expected' => 'less than $max = '.$this->getMaximum(),
  279.                       'paramnum' => 1));
  280.         }
  281.         $this->_minimum $min;
  282.  
  283.         /* set current value to minimum if less than minimum */
  284.         if ($this->getValue($min{
  285.             $this->setValue($min);
  286.         }
  287.     }
  288.  
  289.     /**
  290.      * Returns the progress bar's maximum value. The default value is 100.
  291.      *
  292.      * @return     integer 
  293.      * @since      1.0
  294.      * @access     public
  295.      * @see        setMaximum()
  296.      * @tutorial   dm.getmaximum.pkg
  297.      */
  298.     function getMaximum()
  299.     {
  300.         return $this->_maximum;
  301.     }
  302.  
  303.     /**
  304.      * Sets the progress bar's maximum value.
  305.      *
  306.      * @param      integer   $max           progress bar's maximal value
  307.      *
  308.      * @return     void 
  309.      * @since      1.0
  310.      * @access     public
  311.      * @throws     HTML_PROGRESS_ERROR_INVALID_INPUT
  312.      * @see        getMaximum()
  313.      * @tutorial   dm.setmaximum.pkg
  314.      */
  315.     function setMaximum($max)
  316.     {
  317.         if (!is_int($max)) {
  318.             return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT'exception',
  319.                 array('var' => '$max',
  320.                       'was' => gettype($max),
  321.                       'expected' => 'integer',
  322.                       'paramnum' => 1));
  323.  
  324.         elseif ($max < 0{
  325.             return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT'error',
  326.                 array('var' => '$max',
  327.                       'was' => $max,
  328.                       'expected' => 'positive',
  329.                       'paramnum' => 1));
  330.  
  331.         elseif ($max $this->getMinimum()) {
  332.             return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT'error',
  333.                 array('var' => '$max',
  334.                       'was' => $max,
  335.                       'expected' => 'greater than $min = '.$this->getMinimum(),
  336.                       'paramnum' => 1));
  337.         }
  338.         $this->_maximum $max;
  339.  
  340.         /* set current value to maximum if greater to maximum */
  341.         if ($this->getValue($max{
  342.             $this->setValue($max);
  343.         }
  344.     }
  345.  
  346.     /**
  347.      * Returns the progress bar's increment value. The default value is +1.
  348.      *
  349.      * @return     integer 
  350.      * @since      1.0
  351.      * @access     public
  352.      * @see        setIncrement()
  353.      * @tutorial   dm.getincrement.pkg
  354.      */
  355.     function getIncrement()
  356.     {
  357.         return $this->_increment;
  358.     }
  359.  
  360.     /**
  361.      * Sets the progress bar's increment value.
  362.      *
  363.      * @param      integer   $inc           progress bar's increment value
  364.      *
  365.      * @return     void 
  366.      * @since      1.0
  367.      * @access     public
  368.      * @throws     HTML_PROGRESS_ERROR_INVALID_INPUT
  369.      * @see        getIncrement()
  370.      * @tutorial   dm.setincrement.pkg
  371.      */
  372.     function setIncrement($inc)
  373.     {
  374.         if (!is_int($inc)) {
  375.             return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT'exception',
  376.                 array('var' => '$inc',
  377.                       'was' => gettype($inc),
  378.                       'expected' => 'integer',
  379.                       'paramnum' => 1));
  380.  
  381.         elseif ($inc == 0{
  382.             return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT'error',
  383.                 array('var' => '$inc',
  384.                       'was' => $inc,
  385.                       'expected' => 'not equal zero',
  386.                       'paramnum' => 1));
  387.         }
  388.         $this->_increment $inc;
  389.     }
  390.  
  391.     /**
  392.      * Returns the progress bar's current value. The value is always between
  393.      * the minimum and maximum values, inclusive.
  394.      * By default, the value is initialized with the minimum value.
  395.      *
  396.      * @return     integer 
  397.      * @since      1.0
  398.      * @access     public
  399.      * @see        setValue()
  400.      * @tutorial   dm.getvalue.pkg
  401.      */
  402.     function getValue()
  403.     {
  404.         return $this->_value;
  405.     }
  406.  
  407.     /**
  408.      * Sets the progress bar's current value.
  409.      * If the new value is different from previous value, all change listeners
  410.      * are notified.
  411.      *
  412.      * @param      integer   $val           progress bar's current value
  413.      *
  414.      * @return     void 
  415.      * @since      1.0
  416.      * @access     public
  417.      * @throws     HTML_PROGRESS_ERROR_INVALID_INPUT
  418.      * @see        getValue()
  419.      * @tutorial   dm.setvalue.pkg
  420.      */
  421.     function setValue($val)
  422.     {
  423.         if (!is_int($val)) {
  424.             return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT'exception',
  425.                 array('var' => '$val',
  426.                       'was' => gettype($val),
  427.                       'expected' => 'integer',
  428.                       'paramnum' => 1));
  429.  
  430.         elseif ($val $this->getMinimum()) {
  431.             return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT'error',
  432.                 array('var' => '$val',
  433.                       'was' => $val,
  434.                       'expected' => 'greater than $min = '.$this->getMinimum(),
  435.                       'paramnum' => 1));
  436.  
  437.         elseif ($val $this->getMaximum()) {
  438.             return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT'error',
  439.                 array('var' => '$val',
  440.                       'was' => $val,
  441.                       'expected' => 'less than $max = '.$this->getMaximum(),
  442.                       'paramnum' => 1));
  443.         }
  444.         $this->_value $val;
  445.     }
  446.  
  447.     /**
  448.      * Updates the progress bar's current value by adding increment value.
  449.      *
  450.      * @return     void 
  451.      * @since      1.0
  452.      * @access     public
  453.      * @see        getValue(), setValue()
  454.      * @tutorial   dm.incvalue.pkg
  455.      */
  456.     function incValue()
  457.     {
  458.         $newVal $this->getValue($this->getIncrement();
  459.         $newVal min($this->getMaximum()$newVal);
  460.         $this->setValue$newVal );
  461.     }
  462.  
  463.     /**
  464.      * Returns the percent complete for the progress bar. Note that this number is
  465.      * between 0.00 and 1.00.
  466.      *
  467.      * @return     float 
  468.      * @since      1.0
  469.      * @access     public
  470.      * @see        getValue(), getMaximum()
  471.      * @tutorial   dm.getpercentcomplete.pkg
  472.      */
  473.     function getPercentComplete()
  474.     {
  475.         $percent sprintf("%01.2f",
  476.                       ( ($this->getValue($this->getMinimum()) $this->getMaximum() )
  477.                    );
  478.  
  479.         if (function_exists('floatval')) {
  480.             return floatval($percent);  // use for PHP 4.2+
  481.         else {
  482.             return (float)$percent;     // use for PHP 4.1.x
  483.         }
  484.     }
  485. }
  486.  
  487. ?>

Documentation generated on Mon, 11 Mar 2019 13:52:37 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.