[ PHPXref.com ] [ Generated: Sun Jul 20 19:05:09 2008 ] [ onPHP 0.4.6 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/main/Base/ -> Range.class.php (source)

   1  <?php
   2  /***************************************************************************
   3   *   Copyright (C) 2004-2006 by Konstantin V. Arkhipov                     *
   4   *                                                                         *
   5   *   This program is free software; you can redistribute it and/or modify  *
   6   *   it under the terms of the GNU General Public License as published by  *
   7   *   the Free Software Foundation; either version 2 of the License, or     *
   8   *   (at your option) any later version.                                   *
   9   *                                                                         *
  10   ***************************************************************************/
  11  /* $Id: Range.class.php 1684 2006-06-10 20:58:39Z voxus $ */
  12  
  13      /**
  14       * Integer's interval implementation and accompanying utility methods.
  15       * 
  16       * @ingroup Helpers
  17      **/
  18      class Range implements Stringable
  19      {
  20          private $min = null;
  21          private $max = null;
  22          
  23  		public function __construct($min = null, $max = null)
  24          {
  25              if ($min !== null)
  26                  Assert::isInteger($min);
  27              
  28              if ($max !== null)
  29                  Assert::isInteger($max);
  30              
  31              $this->min = $min;
  32              $this->max = $max;
  33          }
  34          
  35  		public static function create($min = null, $max = null)
  36          {
  37              return new self($min, $max);
  38          }
  39          
  40  		public static function lazyCreate($min = null, $max = null)
  41          {
  42              if ($min > $max)
  43                  self::swap($min, $max);
  44              
  45              return new self($min, $max);
  46          }
  47          
  48  		public function getMin()
  49          {
  50              return $this->min;
  51          }
  52          
  53  		public function setMin($min = null)
  54          {
  55              if ($min !== null)
  56                  Assert::isInteger($min);
  57              else
  58                  return $this;
  59              
  60              iF (($this->max !== null) && $min > $this->max)
  61                  throw new WrongArgumentException(
  62                      'can not set minimal value, which is greater than maximum one'
  63                  );
  64              else
  65                  $this->min = $min;
  66              
  67              return $this;
  68          }
  69          
  70  		public function getMax()
  71          {
  72              return $this->max;
  73          }
  74          
  75  		public function setMax($max = null)
  76          {
  77              if ($max !== null)
  78                  Assert::isInteger($max);
  79              else
  80                  return $this;
  81              
  82              if (($this->min !== null) && $max < $this->min)
  83                  throw new WrongArgumentException(
  84                      'can not set maximal value, which is lower than minimum one'
  85                  );
  86              else
  87                  $this->max = $max;
  88              
  89              return $max;
  90          }
  91  
  92          /// atavism wrt BC
  93  		public function toString($from = 'от', $to = 'до')
  94          {
  95              $out = null;
  96              
  97              if ($this->min)
  98                  $out .= "{$from} ".$this->min;
  99  
 100              if ($this->max)
 101                  $out .= " {$to} ".$this->max;
 102                  
 103              return trim($out);
 104          }
 105          
 106  		public function divide($factor, $precision = null)
 107          {
 108              if ($this->min)
 109                  $this->min = round($this->min / $factor, $precision);
 110  
 111              if ($this->max)
 112                  $this->max = round($this->max / $factor, $precision);
 113              
 114              return $this;
 115          }
 116          
 117  		public function multiply($multiplier)
 118          {
 119              if ($this->min)
 120                  $this->min = $this->min * $multiplier;
 121              
 122              if ($this->max)
 123                  $this->max = $this->max * $multiplier;
 124              
 125              return $this;
 126          }
 127  
 128  		public function equals(Range $range)
 129          {
 130              return ($this->min === $range->getMin() &&
 131                      $this->max === $range->getMax());
 132          }
 133          
 134  		public function isEmpty()
 135          {
 136              return
 137                  ($this->min === null)
 138                  && ($this->max === null);
 139          }
 140          
 141  		public static function swap(&$a, &$b)
 142          {
 143              $c = $a;
 144              $a = $b;
 145              $b = $c;
 146          }
 147      }
 148  ?>


[ Powered by PHPXref - Served by Debian GNU/Linux ]