https://github.com/windwalker-io/io
[READ ONLY] CLI in-out and HTTP request package.
https://github.com/windwalker-io/io
cli http input io psr7-request request
Last synced: about 1 month ago
JSON representation
[READ ONLY] CLI in-out and HTTP request package.
- Host: GitHub
- URL: https://github.com/windwalker-io/io
- Owner: windwalker-io
- Created: 2014-09-09T13:53:05.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2021-02-18T07:01:03.000Z (almost 5 years ago)
- Last Synced: 2025-02-18T21:08:56.731Z (11 months ago)
- Topics: cli, http, input, io, psr7-request, request
- Language: PHP
- Homepage: https://github.com/ventoviro/windwalker
- Size: 131 KB
- Stars: 1
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Windwalker IO
Windwalker IO package is an input & output handler to get request or send output to user terminal.
This package is heavily based on Joomla Input but has modified a lot, please see original concept of [Joomla Wiki](http://docs.joomla.org/Retrieving_request_data_using_JInput).
## Installation via Composer
Add this to the require block in your `composer.json`.
``` json
{
"require": {
"windwalker/io": "~3.0"
}
}
```
## Web Input
Mostly, we will need to get request data from http, the `$_GET`, `$_POST` or `$_REQUEST` provides us these data.
But it is very unsafe if we only use super global variables, the Input object can help us get values from these variables and clean every string.
``` php
use Windwalker\IO\Input;
$input = new Input;
$input->get('flower'); // Same as $_REQUEST['flower']
$input->set('flower', 'sakura');
```
The second argument is default value if request params not exists
``` php
$input->get('flower', 'default');
```
### Filter
Input use [Windwalker Filter](https://github.com/ventoviro/windwalker-filter) package to clean request string, the default filter type is `CMD`.
We can use other filter type:
``` php
// mysite.com/?flower=
to be, or not to be.
;
$input->get('flower'); // tobeornottobe (Default cmd filter)
$input->get('flower', 'default_value', InputFilter::STRING); // to be, or not to be
$input->getString('flower'); // to be, or not to be (Same as above, using magic method)
$input->getRaw('flower') //
to be, or not to be.
```
More filter usage please see: [Windwalker Filter](https://github.com/ventoviro/windwalker-filter)
### Compact and Get Array
Get data as an array.
``` php
// mysite.com/?flower[1]=sakura&flower[2]=olive;
$input->getArray('flower'); // Array( [1] => sakura [2] => olive)
// Get array and filter every values
$input->getArray('flower', null, '.', 'int');
```
Use `compact()` method
``` php
// mysite.com/?flower=sakura&foo=bar&king=Richard
// Get all request
$input->compact();
// To retrieve values you want
$array(
'flower' => '',
'king' => '',
);
$input->compact($array); // Array( [flower] => sakura [king] => Richard)
// Specify different filters for each of the inputs:
$array(
'flower' => InputFilter::CMD,
'king' => InputFilter::STRING,
);
// Use nested array to get more complicated hierarchies of values
$input->compact(array(
'windwalker' => array(
'title' => InputFilter::STRING,
'quantity' => InputFilter::INTEGER,
'state' => 'integer' // Same as above
)
));
```
### Get And Set Multi-Level
If we want to get value of `foo[bar][baz]`, just use `get('foo.bar.baz')`:
``` php
$value = $input->get('foo.bar.baz', 'default', InputFilter::STRING);
$input->set('foo.bar.baz', $data);
// Use custom separator
$input->get('foo/bar/baz', 'default', [filter], '/');
$input->set('foo/bar/baz', $data, '/');
```
### Get Value From Other Methods
We can get other methods as a new input object.
``` php
$post = $input->post;
$value = $post->get('foo', 'bar');
// Other inputs
$get = $input->get;
$put = $input->put;
$delete = $input->delete;
```
## Get SUPER GLOBALS
``` php
$env = $input->env;
$session = $input->session;
$cookie = $input->cookie;
$server = $input->server;
$server->get('REMOTE_ADDR'); // Same as $_SERVER['REMOTE_ADDR'];
```
See: [SUPER GLOBALS](http://php.net/manual/en/language.variables.superglobals.php)
### Get method of current request:
``` php
$method = $input->getMethod();
```
## Json Input
If you send a request with json body or `content-type: application/json`, you can use `$input->json` to
get `JsonInput` and parse json values.
## Files Input
The format that PHP returns file data in for arrays can at times be awkward, especially when dealing with arrays of files.
FilesInput provides a convenient interface for making life a little easier, grouping the data by file.
Suppose you have a form like:
``` html
```
Normally, PHP would put these in an array called `$_FILES` that looked like:
```
Array
(
[flower] => Array
(
[name] => Array
(
[test] => Array
(
[0] => youtube_icon.png
[1] => Younger_Son_2.jpg
)
)
[type] => Array
(
[test] => Array
(
[0] => image/png
[1] => image/jpeg
)
)
[tmp_name] => Array
(
[test] => Array
(
[0] => /tmp/phpXoIpSD
[1] => /tmp/phpWDE7ye
)
)
[error] => Array
(
[test] => Array
(
[0] => 0
[1] => 0
)
)
[size] => Array
(
[test] => Array
(
[0] => 34409
[1] => 99529
)
)
)
)
```
FilesInput produces a result that is cleaner and easier to work with:
``` php
$files = $input->files->get('flower');
```
`$files` then becomes:
```
Array
(
[test] => Array
(
[0] => Array
(
[name] => youtube_icon.png
[type] => image/png
[tmp_name] => /tmp/phpXoIpSD
[error] => 0
[size] => 34409
)
[1] => Array
(
[name] => Younger_Son_2.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpWDE7ye
[error] => 0
[size] => 99529
)
)
)
```
## CLI Input & Output
Please see [Cli README](Cli)