Esempio n. 1
0
 /**
  * @param string $value
  *
  * @throws ResponseCode
  * @throws \Exception
  *
  * @return $this|self
  */
 public static function createFromInput($value)
 {
     $timestamp = strtotime($value);
     try {
         // trim java ms
         $value = preg_replace('`(\\.[0-9]{3})\\+`', '+', $value);
         $datetime = DateTime::createFromFormat(self::DATE_FORMAT, $value);
         if ($timestamp != $datetime->getTimestamp()) {
             throw new ResponseCode("Invalid datetime with value '{$value}'", 422);
         }
     } catch (\Exception $exception) {
         throw new ResponseCode("Invalid datetime format with value '{$value}'", 422);
     }
     return $datetime;
 }
Esempio n. 2
0
 /**
  * Return a date + 1 day
  *
  * @httpmethod GET
  * @auth None
  *
  * @param \Cawa\SwaggerServer\DateTime $date the initial date
  *
  * @return \Cawa\SwaggerServer\DateTime the generated datetime
  */
 public function date(DateTime $date)
 {
     return ['value' => $date->add(new \DateInterval('P1D'))];
 }
 /**
  * @param string $type
  * @param string $name
  * @param mixed $value
  * @param bool $optionnal
  *
  * @throws ResponseCode
  * @throws \Exception
  *
  * @return array|bool|float|string
  */
 private function getFinalValue(string $type, string $name, $value, bool $optionnal)
 {
     if ($optionnal && ($value === null || $value === '')) {
         return null;
     }
     if (strpos($type, '[]') !== false) {
         $return = [];
         foreach (is_array($value) ? $value : [$value] as $sKey => $currentValue) {
             $return[$sKey] = $this->getFinalValue(str_replace('[]', '', $type), $name, $currentValue, $optionnal);
         }
         return $return;
     }
     switch ($type) {
         case 'int':
             $value = filter_var($value, FILTER_VALIDATE_INT, ['flags' => FILTER_NULL_ON_FAILURE]);
             if (is_null($value)) {
                 throw new ResponseCode("Invalid integer parameters '{$name}' with value '{$value}'", 422);
             }
             break;
         case 'float':
             $value = filter_var($value, FILTER_VALIDATE_FLOAT, ['flags' => FILTER_NULL_ON_FAILURE]);
             if (is_null($value)) {
                 throw new ResponseCode("Invalid float parameters '{$name}' with value '{$value}'", 422);
             }
             break;
         case 'string':
             if (!is_string($value) || !$value) {
                 throw new ResponseCode("Invalid string parameters '{$name}' with value '{$value}'", 422);
             }
             break;
         case 'bool':
             if ($value !== '1' && $value !== 1 && $value !== '0' && $value !== 0 && $value !== 'true' && $value !== 'false') {
                 throw new ResponseCode("Invalid bool parameters '{$name}' with value '{$value}'", 422);
             }
             if ($value == '1') {
                 $value = true;
             } elseif ($value === 'true') {
                 $value = true;
             } elseif ($value == '0') {
                 $value = false;
             } elseif ($value === 'false') {
                 $value = false;
             }
             if (!is_bool($value)) {
                 throw new ResponseCode("Invalid bool parameters '{$name}' with value '{$value}'", 422);
             }
             break;
         case '\\Cawa\\SwaggerServer\\DateTime':
             $value = DateTime::createFromInput($value);
             break;
         case 'array':
             if (!is_array($value)) {
                 $jsonValue = @json_decode($value, true);
                 if ($jsonValue === false || !is_array($jsonValue)) {
                     throw new ResponseCode("Invalid array parameters '{$name}' with value '{$value}'", 422);
                 }
                 $value = $jsonValue;
             }
             break;
         case 'mixed':
             if (is_numeric($value)) {
                 $value = 0 + $value;
             }
             break;
         default:
             throw new \Exception('Invalid ' . $type . " parameters '{$name}' with value '{$value}'");
             break;
     }
     return $value;
 }