Files
spa-api/laravel/input.php

153 lines
2.9 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-08-26 21:42:04 -05:00
private $input;
2011-08-23 21:04:40 -05:00
/**
* The $_GET array for the request.
*
* @var array
*/
public $get;
/**
* The $_POST array for the request.
*
* @var array
*/
public $post;
/**
* The cookie engine instance.
2011-08-23 21:04:40 -05:00
*
* @var Cookie
2011-08-23 21:04:40 -05:00
*/
public $cookies;
/**
* The $_FILES array for the request.
*
* @var array
*/
public $files;
/**
* Create a new Input instance.
*
2011-08-26 21:42:04 -05:00
* @param array $input
* @param array $files
* @param Cookie $cookies
* @return void
2011-08-23 21:04:40 -05:00
*/
2011-08-26 21:42:04 -05:00
public function __construct($input, $files, Cookie $cookies)
2011-08-23 21:04:40 -05:00
{
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-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-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
{
$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-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-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-08-23 21:04:40 -05:00
return array_key_exists($key, $this->files) ? move_uploaded_file($this->files[$key]['tmp_name'], $path) : 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.
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
}
}