Example #1
0
 /**
  * Gets a value from the request data
  *
  * A value that exactly equals `''` and is not cast to a specific type will
  * become `NULL`.
  *
  * Valid `$cast_to` types include:
  *  - `'string'`
  *  - `'binary'`
  *  - `'int'`
  *  - `'integer'`
  *  - `'bool'`
  *  - `'boolean'`
  *  - `'array'`
  *  - `'date'`
  *  - `'time'`
  *  - `'timestamp'`
  *
  * It is possible to append a `?` to a data type to return `NULL`
  * whenever the `$key` was not specified in the request, or if the value
  * was a blank string.
  *
  * The `array` and unspecified `$cast_to` types allow for multi-dimensional
  * arrays of string data. It is possible to cast an input value as a
  * single-dimensional array of a specific type by appending `[]` to the
  * `$cast_to`.
  *
  * All `string`, `array` or unspecified `$cast_to` will result in the value(s)
  * being interpreted as UTF-8 string and appropriately cleaned of invalid
  * byte sequences. Also, all low-byte, non-printable characters will be
  * stripped from the value. This includes all bytes less than the value of
  * 32 (Space) other than Tab (`\t`), Newline (`\n`) and Cariage Return
  * (`\r`).
  *
  * To preserve low-byte, non-printable characters, or get the raw value
  * without cleaning invalid UTF-8 byte sequences, plase use the value of
  * `binary` for the `$cast_to` parameter.
  *
  * Any integers that are beyond the range of 32bit storage will be returned
  * as a string. The returned value can be forced to always be a real
  * integer, which may cause truncation of the value, by passing `integer!`
  * as the `$cast_to`.
  *
  * @param string $key The key to get - array elements can be accessed via `[sub-key]`
  * @param string $cast_to Cast the value to this data type
  * @param mixed $default_value If the parameter is not set use this value instead.
  * @param boolean $default_on_blank Return NULL if request value and default are empty
  * @return mixed The value
  */
 public function get($key, $cast_to = NULL, $default = NULL, $default_on_blank = FALSE)
 {
     $value = $default;
     if ($cast_to == 'file') {
         try {
             $name = Flourish\Core::dereference($key, $this->files['name']);
             $file = Flourish\Core::dereference($key, $this->files['tmp_name']);
             $error = Flourish\Core::dereference($key, $this->files['error']);
             if (!$error) {
                 $value = new App\File($file);
                 $value->mask($name);
             } else {
                 // UPLOAD_ERR_INI_SIZE: 1
                 // UPLOAD_ERR_FORM_SIZE: 2
                 // UPLOAD_ERR_NO_TMP_DIR: 6
                 // UPLOAD_ERR_CANT_WRITE: 7
                 // UPLOAD_ERR_EXTENSION: 8
                 // UPLOAD_ERR_PARTIAL: 3
             }
         } catch (Flourish\ProgrammerException $e) {
             if ($default) {
                 $value = new App\File($default);
             }
         }
     } else {
         try {
             $value = Flourish\Core::dereference($key, $this->data);
             if ($value === '' && $default_on_blank && $default !== NULL) {
                 $value = $default;
             }
         } catch (Flourish\ProgrammerException $e) {
             $value = $default;
         }
         //
         // This allows for data_type? casts to allow NULL through
         //
         if ($cast_to !== NULL && substr($cast_to, -1) == '?') {
             if ($value === NULL || $value === '') {
                 return NULL;
             }
             $cast_to = substr($cast_to, 0, -1);
         }
         $value = self::cast($value, $cast_to);
     }
     return $value;
 }