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

Generated on Sun Dec 9 21:56:23 2007 for onPHP by  doxygen 1.5.4