1  <?php
  2  /* Copyright (c) 2013, Geert Bergman (geert@scrivo.nl)
  3   * All rights reserved.
  4   *
  5   * Redistribution and use in source and binary forms, with or without
  6   * modification, are permitted provided that the following conditions are met:
  7   *
  8   * 1. Redistributions of source code must retain the above copyright notice,
  9   *    this list of conditions and the following disclaimer.
 10   * 2. Redistributions in binary form must reproduce the above copyright notice,
 11   *    this list of conditions and the following disclaimer in the documentation
 12   *    and/or other materials provided with the distribution.
 13   * 3. Neither the name of "Scrivo" nor the names of its contributors may be
 14   *    used to endorse or promote products derived from this software without
 15   *    specific prior written permission.
 16   *
 17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 18   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 19   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 20   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 21   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 22   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 23   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 24   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 25   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 26   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 27   * POSSIBILITY OF SUCH DAMAGE.
 28   *
 29   * $Id: Request.php 858 2013-08-22 13:03:03Z geert $
 30   */
 31  
 32  /**
 33   * Implementation of the \Scrivo\Session class.
 34   */
 35  
 36  namespace Scrivo;
 37  
 38  /**
 39   */
 40  class Request {
 41  
 42      const TYPE_INTEGER 1;
 43      const TYPE_STRING 2;
 44      const TYPE_BOOLEAN 3;
 45      const TYPE_DATE_TIME 4;
 46  
 47      const DEF "_scrivo_def_param_";
 48  
 49      /* Fix GPC Variables */
 50      private static function fetch2($v$t$d) {
 51          if (is_array($v)) {
 52  
 53              foreach($v as $k2=>$v2) {
 54                  $v[$k2] = self::fetch2($v2$t$d);
 55              }
 56              return $v;
 57  
 58          } else {
 59  
 60              if (self::TYPE_BOOLEAN != $t) {
 61                  if (null === $v) {
 62                      if ($d) {
 63                          return $d;
 64                      }
 65                      throw new \Scrivo\SystemException(
 66                              "Invalid request parameter");
 67                  }
 68              }
 69  
 70              switch ($t) {
 71                  case self::TYPE_STRING:
 72                      return new \Scrivo\String($v);
 73                  case self::TYPE_INTEGER:
 74                      return intval($v);
 75                  case self::TYPE_BOOLEAN:
 76                      return null !== $v;
 77                  case self::TYPE_DATE_TIME:
 78                      $dt "\d{4}-\d{2}-\d{2}";
 79                      if (preg_match("/^$dt \d{2}:\d{2}:\d{2}$|^$dt$/"$v)) {
 80                          return new \DateTime($v);
 81                      }
 82                      return null;
 83              }
 84  
 85              throw new \Scrivo\SystemException("Invalid request parameter type");
 86          }
 87      }
 88  
 89      private static function fetch($arr$name$type$default) {
 90  
 91          if (self::TYPE_BOOLEAN == $type) {
 92              return isset($arr[$name]) ?
 93                  self::fetch2($arr[$name], $type$default) : false;
 94          } else {
 95              if (array_key_exists($name$arr)) {
 96                  return self::fetch2(@$arr[$name], $type$default);
 97              } else {
 98                  if ($default !== self::DEF) {
 99                      return $default;
100                  }
101              }
102          }
103          throw new \Scrivo\SystemException(
104              "Invalid request parameter type '$name'");
105      }
106  
107      public static function get($name$type$default=self::DEF) {
108          return self::fetch($_GET$name$type$default);
109      }
110      public static function post($name$type$default=self::DEF) {
111          return self::fetch($_POST$name$type$default);
112      }
113      public static function request($name$type$default=self::DEF) {
114          return self::fetch($_REQUEST$name$type$default);
115      }
116  
117  }
118  
119  ?>

Documentation generated by phpDocumentor 2.0.0a12 and ScrivoDocumentor on August 29, 2013