BaseRange.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *    Copyright (C) 2009 by Denis M. Gabaidulin                            *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00017     class BaseRange implements Stringable
00018     {
00019         protected $min = null;
00020         protected $max = null;
00021         
00022         public function __construct($min = null, $max = null)
00023         {
00024             $this->min = $min;
00025             $this->max = $max;
00026         }
00027         
00031         public static function lazyCreate($min = null, $max = null)
00032         {
00033             if ($min > $max)
00034                 self::swap($min, $max);
00035             
00036             return new self($min, $max);
00037         }
00038         
00039         public function getMin()
00040         {
00041             return $this->min;
00042         }
00043         
00048         public function setMin($min = null)
00049         {
00050             if (($this->max !== null) && $min > $this->max)
00051                 throw new WrongArgumentException(
00052                     'can not set minimal value, which is greater than maximum one'
00053                 );
00054             else
00055                 $this->min = $min;
00056             
00057             return $this;
00058         }
00059         
00060         public function getMax()
00061         {
00062             return $this->max;
00063         }
00064         
00069         public function setMax($max = null)
00070         {
00071             if (($this->min !== null) && $max < $this->min)
00072                 throw new WrongArgumentException(
00073                     'can not set maximal value, which is lower than minimum one'
00074                 );
00075             else
00076                 $this->max = $max;
00077             
00078             return $this;
00079         }
00080 
00082         public function toString($from = 'от', $to = 'до')
00083         {
00084             $out = null;
00085             
00086             if ($this->min)
00087                 $out .= "{$from} ".$this->min;
00088 
00089             if ($this->max)
00090                 $out .= " {$to} ".$this->max;
00091                 
00092             return trim($out);
00093         }
00094         
00098         public function divide($factor, $precision = null)
00099         {
00100             if ($this->min)
00101                 $this->min = round($this->min / $factor, $precision);
00102 
00103             if ($this->max)
00104                 $this->max = round($this->max / $factor, $precision);
00105             
00106             return $this;
00107         }
00108         
00112         public function multiply($multiplier)
00113         {
00114             if ($this->min)
00115                 $this->min = $this->min * $multiplier;
00116             
00117             if ($this->max)
00118                 $this->max = $this->max * $multiplier;
00119             
00120             return $this;
00121         }
00122 
00123         public function equals(BaseRange $range)
00124         {
00125             return ($this->min === $range->getMin() &&
00126                     $this->max === $range->getMax());
00127         }
00128         
00129         public function intersects(BaseRange $range)
00130         {
00131             return ($this->max >= $range->getMin() &&
00132                     $this->min <= $range->getMax());
00133         }
00134         
00135         public function isEmpty()
00136         {
00137             return
00138                 ($this->min === null)
00139                 && ($this->max === null);
00140         }
00141         
00142         public static function swap(&$a, &$b)
00143         {
00144             $c = $a;
00145             $a = $b;
00146             $b = $c;
00147         }
00148     }
00149 ?>