Files
spa-api/laravel/input.php

167 lines
3.6 KiB
PHP
Raw Normal View History

2011-08-18 19:56:29 -05:00
<?php namespace Laravel;
2011-06-08 23:45:08 -05:00
2011-08-30 22:35:32 -05:00
class Input {
2011-06-08 23:45:08 -05:00
/**
2011-08-23 21:04:40 -05:00
* The applicable input for the request.
2011-06-08 23:45:08 -05:00
*
* @var array
*/
public static $input;
2011-06-08 23:45:08 -05:00
2011-07-12 20:04:14 -05:00
/**
* Get all of the input data for the request.
*
2011-08-23 21:04:40 -05:00
* This method returns a merged array containing $input->get() and $input->files().
2011-07-12 20:04:14 -05:00
*
* @return array
*/
public static function all()
2011-07-12 20:04:14 -05:00
{
return array_merge(static::get(), static::file());
2011-07-12 20:04:14 -05:00
}
/**
* Determine if the input data contains an item.
2011-06-08 23:45:08 -05:00
*
2011-06-16 22:45:33 -05:00
* @param string $key
2011-06-08 23:45:08 -05:00
* @return bool
*/
public static function has($key)
2011-06-08 23:45:08 -05:00
{
return ( ! is_null(static::get($key)) and trim((string) static::get($key)) !== '');
2011-06-08 23:45:08 -05:00
}
/**
2011-06-14 17:27:11 -05:00
* Get an item from the input data.
2011-06-08 23:45:08 -05:00
*
* This method should be used for all request methods (GET, POST, PUT, and DELETE).
*
2011-09-08 17:49:16 -05:00
* <code>
* // Get an item from the input to the application
* $value = Input::get('name');
*
* // Get an item from the input and return "Fred" if the item doesn't exist
* $value = Input::get('name', 'Fred');
* </code>
*
2011-06-08 23:45:08 -05:00
* @param string $key
2011-06-14 17:27:11 -05:00
* @param mixed $default
* @return mixed
2011-06-08 23:45:08 -05:00
*/
public static function get($key = null, $default = null)
2011-06-08 23:45:08 -05:00
{
return Arr::get(static::$input, $key, $default);
2011-06-08 23:45:08 -05:00
}
/**
* Determine if the old input data contains an item.
*
2011-06-16 22:45:33 -05:00
* @param string $key
* @return bool
*/
public static function had($key)
{
return ( ! is_null(static::old($key)) and trim((string) static::old($key)) !== '');
2011-06-08 23:45:08 -05:00
}
/**
* Get input data from the previous request.
*
2011-09-08 17:49:16 -05:00
* <code>
* // Get an item from the previous request's input
* $value = Input::old('name');
*
* // Get an item from the previous request's input and return "Fred" if it doesn't exist.
* $value = Input::old('name', 'Fred');
* </code>
*
2011-08-23 21:04:40 -05:00
* @param string $key
* @param mixed $default
2011-06-08 23:45:08 -05:00
* @return string
*/
public static function old($key = null, $default = null)
2011-06-08 23:45:08 -05:00
{
if (Config::get('session.driver') == '')
2011-09-08 17:49:16 -05:00
{
throw new \Exception('A session driver must be specified in order to access old input.');
}
$driver = IoC::container()->resolve('laravel.session');
2011-06-08 23:45:08 -05:00
2011-08-23 21:04:40 -05:00
return Arr::get($driver->get('laravel_old_input', array()), $key, $default);
2011-06-08 23:45:08 -05:00
}
2011-06-26 08:44:31 -05:00
/**
* Get an item from the uploaded file data.
*
* "Dot" syntax may be used to get a specific item from the file array.
*
2011-09-08 17:49:16 -05:00
* <code>
* // Get the array of information regarding an uploaded file
* $file = Input::file('picture');
*
* // Get an element from the array of information regarding an uploaded file
* $size = Input::file('picture.size');
* </code>
*
2011-06-26 08:44:31 -05:00
* @param string $key
* @param mixed $default
* @return array
*/
public static function file($key = null, $default = null)
2011-06-26 08:44:31 -05:00
{
return Arr::get($_FILES, $key, $default);
2011-06-26 08:44:31 -05:00
}
2011-08-22 22:38:20 -05:00
/**
* Move an uploaded file to permanent storage.
*
* This method is simply a convenient wrapper around move_uploaded_file.
*
2011-09-08 17:49:16 -05:00
* <code>
* // Move the "picture" file to a permament location on disk
* Input::upload('picture', PUBLIC_PATH.'img/picture.jpg');
* </code>
*
2011-08-22 22:38:20 -05:00
* @param string $key
* @param string $path
* @return bool
*/
public static function upload($key, $path)
2011-08-22 22:38:20 -05:00
{
return array_key_exists($key, $_FILES) ? File::upload($key, $path, $_FILES) : false;
2011-08-22 22:38:20 -05:00
}
}
/**
* Set the input values for the current request.
*/
$input = array();
switch (Request::method())
{
case 'GET':
$input = $_GET;
break;
case 'POST':
$input = $_POST;
break;
case 'PUT':
case 'DELETE':
if (Request::spoofed())
{
$input = $_POST;
}
else
{
parse_str(file_get_contents('php://input'), $input);
}
}
unset($input[Request::spoofer]);
2011-06-08 23:45:08 -05:00
Input::$input = $input;