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

title

Body

[close]

/core/Form/ -> PrimitiveRange.class.php (source)

   1  <?php
   2  /***************************************************************************
   3   *   Copyright (C) 2004-2005 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: PrimitiveRange.class.php 1684 2006-06-10 20:58:39Z voxus $ */
  12  
  13      /**
  14       * @ingroup Primitives
  15      **/
  16      class PrimitiveRange extends ComplexPrimitive
  17      {
  18          const MIN    = 'min';
  19          const MAX    = 'max';
  20  
  21          private $swapAllowed = true;
  22  
  23  		public function setSwapAllowed()
  24          {
  25              $this->swapAllowed = true;
  26  
  27              return $this;
  28          }
  29  
  30  		public function setSwapDisallowed()
  31          {
  32              $this->swapAllowed = false;
  33  
  34              return $this;
  35          }
  36  
  37          // to be E_STRICT compatible
  38  		public function setValue(/* Range */ $range)
  39          {
  40              Assert::isTrue(
  41                  $range instanceof Range,
  42                  'only ranges accepted today'
  43              );
  44  
  45              $this->value = $range;
  46  
  47              return $this;
  48          }
  49  
  50  		public function importSingle(&$scope)
  51          {
  52              if (!BasePrimitive::import($scope) || is_array($scope[$this->name]))
  53                  return null;
  54  
  55              if (isset($scope[$this->name]) && is_string($scope[$this->name])) {
  56                  $arr = explode('-', $scope[$this->name], 2);
  57  
  58                  $range =
  59                      Range::buildObject(
  60                          ArrayUtils::getArrayVar($arr, 0),
  61                          ArrayUtils::getArrayVar($arr, 1),
  62                          $this->swapAllowed
  63                      );
  64  
  65                  if ($range &&
  66                      !(
  67                          ($this->min && $range->getMin()) &&
  68                          $range->getMin() < $this->min
  69                      ) &&
  70                      !(
  71                          ($this->max && $range->getMax()) &&
  72                          $range->getMax() > $this->max
  73                      )
  74                  ) {
  75                      $this->value = $range;
  76  
  77                      return true;
  78                  }
  79              }
  80  
  81              return false;
  82          }
  83  
  84  		public function importMarried(&$scope) // ;-)
  85          {
  86              $name = &$this->name;
  87  
  88              if (
  89                  ($this->safeGet($scope, $name, self::MIN) === null) &&
  90                  ($this->safeGet($scope, $name, self::MAX) === null)
  91              )
  92                  return null;
  93  
  94              $range =
  95                  Range::buildObject(
  96                      $this->safeGet($scope, $name, self::MIN),
  97                      $this->safeGet($scope, $name, self::MAX),
  98                      $this->swapAllowed
  99                  );
 100  
 101              if ($range &&
 102                      !(
 103                          ($this->min && $range->getMin()) &&
 104                          $range->getMin() < $this->min
 105                      ) &&
 106                      !(
 107                          ($this->max && $range->getMax()) &&
 108                          $range->getMax() > $this->max
 109                      )
 110              ) {
 111                  $this->value = $range;
 112  
 113                  return true;
 114              }
 115  
 116              return false;
 117          }
 118  
 119  		private function safeGet(&$scope, $firstDimension, $secondDimension)
 120          {
 121              if (isset($scope[$firstDimension]) && is_array($scope[$firstDimension])) {
 122                  if (
 123                      isset($scope[$firstDimension][$secondDimension])
 124                      && is_array($scope[$firstDimension])
 125                      && !empty($scope[$firstDimension][$secondDimension])
 126                  ) {
 127                      return $scope[$firstDimension][$secondDimension];
 128                  }
 129              }
 130  
 131              return null;
 132          }
 133      }
 134  ?>


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