PrimitiveRange.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2004-2007 by Konstantin V. Arkhipov                     *
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 
00015     final class PrimitiveRange extends ComplexPrimitive
00016     {
00017         const MIN   = 'min';
00018         const MAX   = 'max';
00019 
00024         public function setValue(/* BaseRange */ $range)
00025         {
00026             Assert::isTrue(
00027                 $range instanceof BaseRange,
00028                 'only ranges accepted today'
00029             );
00030             
00031             $this->value = $range;
00032             
00033             return $this;
00034         }
00035         
00036         public function getMax()
00037         {
00038             if ($this->value)
00039                 return $this->value->getMax();
00040             
00041             return null;
00042         }
00043         
00044         public function getMin()
00045         {
00046             if ($this->value)
00047                 return $this->value->getMin();
00048             
00049             return null;
00050         }
00051 
00052         public function getActualMax()
00053         {
00054             if ($range = $this->getActualValue())
00055                 return $range->getMax();
00056             
00057             return null;
00058         }
00059         
00060         public function getActualMin()
00061         {
00062             if ($range = $this->getActualValue())
00063                 return $range->getMin();
00064             
00065             return null;
00066         }
00067         
00068         public function importSingle($scope)
00069         {
00070             if (!BasePrimitive::import($scope) || is_array($scope[$this->name]))
00071                 return null;
00072             
00073             if (isset($scope[$this->name]) && is_string($scope[$this->name])) {
00074                 $array = explode('-', $scope[$this->name], 2);
00075                 
00076                 $range =
00077                     BaseRange::lazyCreate(
00078                         ArrayUtils::getArrayVar($array, 0),
00079                         ArrayUtils::getArrayVar($array, 1)
00080                     );
00081                 
00082                 if (
00083                     $range
00084                     && $this->checkLimits($range)
00085                 ) {
00086                     $this->value = $range;
00087                     
00088                     return true;
00089                 }
00090             }
00091             
00092             return false;
00093         }
00094 
00095         public function importMarried($scope) // ;-)
00096         {
00097             if (
00098                 ($this->safeGet($scope, $this->name, self::MIN) === null)
00099                 && ($this->safeGet($scope, $this->name, self::MAX) === null)
00100             )
00101                 return null;
00102             
00103             $range =
00104                 BaseRange::lazyCreate(
00105                     $this->safeGet($scope, $this->name, self::MIN),
00106                     $this->safeGet($scope, $this->name, self::MAX)
00107                 );
00108             
00109             if (
00110                 $range
00111                 && $this->checkLimits($range)
00112             ) {
00113                 $this->value = $range;
00114                 $this->raw = $scope[$this->name];
00115                 
00116                 return $this->imported = true;
00117             }
00118             
00119             return false;
00120         }
00121         
00122         private function checkLimits(BaseRange $range)
00123         {
00124             if (
00125                 !(
00126                     ($this->min && $range->getMin())
00127                     && $range->getMin() < $this->min
00128                 ) &&
00129                 !(
00130                     ($this->max && $range->getMax())
00131                     && $range->getMax() > $this->max
00132                 )
00133             ) {
00134                 return true;
00135             }
00136             
00137             return false;
00138         }
00139         
00140         private function safeGet($scope, $firstDimension, $secondDimension)
00141         {
00142             if (isset($scope[$firstDimension]) && is_array($scope[$firstDimension])) {
00143                 if (
00144                     !empty($scope[$firstDimension][$secondDimension])
00145                     && is_array($scope[$firstDimension])
00146                 ) {
00147                     return $scope[$firstDimension][$secondDimension];
00148                 }
00149             }
00150             
00151             return null;
00152         }
00153     }
00154 ?>