00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00018 class Range implements Stringable
00019 {
00020 private $min = null;
00021 private $max = null;
00022
00023 public function __construct($min = null, $max = null)
00024 {
00025 if ($min !== null)
00026 Assert::isInteger($min);
00027
00028 if ($max !== null)
00029 Assert::isInteger($max);
00030
00031 $this->min = $min;
00032 $this->max = $max;
00033 }
00034
00038 public static function create($min = null, $max = null)
00039 {
00040 return new self($min, $max);
00041 }
00042
00046 public static function lazyCreate($min = null, $max = null)
00047 {
00048 if ($min > $max)
00049 self::swap($min, $max);
00050
00051 return new self($min, $max);
00052 }
00053
00054 public function getMin()
00055 {
00056 return $this->min;
00057 }
00058
00063 public function setMin($min = null)
00064 {
00065 if ($min !== null)
00066 Assert::isInteger($min);
00067 else
00068 return $this;
00069
00070 iF (($this->max !== null) && $min > $this->max)
00071 throw new WrongArgumentException(
00072 'can not set minimal value, which is greater than maximum one'
00073 );
00074 else
00075 $this->min = $min;
00076
00077 return $this;
00078 }
00079
00080 public function getMax()
00081 {
00082 return $this->max;
00083 }
00084
00089 public function setMax($max = null)
00090 {
00091 if ($max !== null)
00092 Assert::isInteger($max);
00093 else
00094 return $this;
00095
00096 if (($this->min !== null) && $max < $this->min)
00097 throw new WrongArgumentException(
00098 'can not set maximal value, which is lower than minimum one'
00099 );
00100 else
00101 $this->max = $max;
00102
00103 return $max;
00104 }
00105
00107 public function toString($from = 'от', $to = 'до')
00108 {
00109 $out = null;
00110
00111 if ($this->min)
00112 $out .= "{$from} ".$this->min;
00113
00114 if ($this->max)
00115 $out .= " {$to} ".$this->max;
00116
00117 return trim($out);
00118 }
00119
00123 public function divide($factor, $precision = null)
00124 {
00125 if ($this->min)
00126 $this->min = round($this->min / $factor, $precision);
00127
00128 if ($this->max)
00129 $this->max = round($this->max / $factor, $precision);
00130
00131 return $this;
00132 }
00133
00137 public function multiply($multiplier)
00138 {
00139 if ($this->min)
00140 $this->min = $this->min * $multiplier;
00141
00142 if ($this->max)
00143 $this->max = $this->max * $multiplier;
00144
00145 return $this;
00146 }
00147
00148 public function equals(Range $range)
00149 {
00150 return ($this->min === $range->getMin() &&
00151 $this->max === $range->getMax());
00152 }
00153
00154 public function intersects(Range $range)
00155 {
00156 return ($this->max >= $range->getMin() &&
00157 $this->min <= $range->getMax());
00158 }
00159
00160 public function isEmpty()
00161 {
00162 return
00163 ($this->min === null)
00164 && ($this->max === null);
00165 }
00166
00167 public static function swap(&$a, &$b)
00168 {
00169 $c = $a;
00170 $a = $b;
00171 $b = $c;
00172 }
00173 }
00174 ?>