Files
spa-api/laravel/input.php

189 lines
4.2 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
*/
2011-09-03 22:36:27 -05:00
protected $input;
2011-08-23 21:04:40 -05:00
/**
2011-09-08 17:49:16 -05:00
* The $_FILES array for the request.
2011-08-23 21:04:40 -05:00
*
* @var array
*/
2011-09-08 17:49:16 -05:00
protected $files;
2011-08-23 21:04:40 -05:00
/**
2011-09-08 17:49:16 -05:00
* The file manager instance.
2011-08-23 21:04:40 -05:00
*
2011-09-08 17:49:16 -05:00
* @var File
2011-08-23 21:04:40 -05:00
*/
2011-09-08 17:49:16 -05:00
protected $file;
2011-08-23 21:04:40 -05:00
/**
* The cookie engine instance.
2011-08-23 21:04:40 -05:00
*
* @var Cookie
2011-08-23 21:04:40 -05:00
*/
public $cookies;
/**
* Create a new Input manager instance.
2011-08-23 21:04:40 -05:00
*
2011-09-08 17:49:16 -05:00
* @param Cookie $cookies
2011-08-26 21:42:04 -05:00
* @param array $input
* @param array $files
* @return void
2011-08-23 21:04:40 -05:00
*/
2011-09-08 17:49:16 -05:00
public function __construct(File $file, Cookie $cookies, $input, $files)
2011-08-23 21:04:40 -05:00
{
2011-09-08 17:49:16 -05:00
$this->file = $file;
2011-08-26 21:42:04 -05:00
$this->input = $input;
2011-08-23 21:04:40 -05:00
$this->files = $files;
$this->cookies = $cookies;
}
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
*/
2011-08-23 21:04:40 -05:00
public function all()
2011-07-12 20:04:14 -05:00
{
2011-08-23 21:04:40 -05:00
return array_merge($this->get(), $this->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
*/
2011-08-23 21:04:40 -05:00
public function has($key)
2011-06-08 23:45:08 -05:00
{
2011-08-23 21:04:40 -05:00
return ( ! is_null($this->get($key)) and trim((string) $this->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
*/
2011-08-23 21:04:40 -05:00
public function get($key = null, $default = null)
2011-06-08 23:45:08 -05:00
{
2011-08-23 21:04:40 -05:00
return Arr::get($this->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
*/
2011-08-23 21:04:40 -05:00
public function had($key)
{
2011-08-23 21:04:40 -05:00
return ( ! is_null($this->old($key)) and trim((string) $this->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 function old($key = null, $default = null)
2011-06-08 23:45:08 -05:00
{
2011-09-08 17:49:16 -05:00
if (IoC::container()->resolve('laravel.config')->get('session.driver') == '')
{
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
*/
2011-08-23 21:04:40 -05:00
public function file($key = null, $default = null)
2011-06-26 08:44:31 -05:00
{
2011-08-23 21:04:40 -05:00
return Arr::get($this->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
*/
2011-08-23 21:04:40 -05:00
public function upload($key, $path)
2011-08-22 22:38:20 -05:00
{
2011-09-08 17:49:16 -05:00
return array_key_exists($key, $this->files) ? $this->file->upload($key, $path, $this->files) : false;
2011-08-22 22:38:20 -05:00
}
2011-06-08 23:45:08 -05:00
/**
2011-08-23 21:04:40 -05:00
* Magic Method for retrieving items from the request input.
*
* This method is particularly helpful in controllers where access to the IoC container
* is provided through the controller's magic __get method.
*
* <code>
* // Retrieve the "name" input item from a controller method
* $name = $this->input->name;
* </code>
2011-06-08 23:45:08 -05:00
*/
2011-08-23 21:04:40 -05:00
public function __get($key)
2011-06-08 23:45:08 -05:00
{
2011-08-23 21:04:40 -05:00
return $this->get($key);
2011-06-08 23:45:08 -05:00
}
}