GithubHelp home page GithubHelp logo

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.

Installation via Composer

Add this to the require block in your composer.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.

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

$input->get('flower', 'default');

Filter

Input use Windwalker Filter package to clean request string, the default filter type is CMD. We can use other filter type:

// mysite.com/?flower=<p>to be, or not to be.</p>;

$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') // <p>to be, or not to be.</p>

More filter usage please see: Windwalker Filter

Compact and Get Array

Get data as an array.

// 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

// 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'):

$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.

$post = $input->post;

$value = $post->get('foo', 'bar');

// Other inputs
$get    = $input->get;
$put    = $input->put;
$delete = $input->delete;

Get SUPER GLOBALS

$env     = $input->env;
$session = $input->session;
$cookie  = $input->cookie;
$server  = $input->server;

$server->get('REMOTE_ADDR'); // Same as $_SERVER['REMOTE_ADDR'];

See: SUPER GLOBALS

Get method of current request:

$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:

<form action="..." enctype="multipart/form-data" method="post">
    <input type="file" name="flower[test][]" />
    <input type="file" name="flower[test][]" />
    <input type="submit" value="submit" />
</form>

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:

$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

Windwalker's Projects

3to4 icon 3to4

Windwalker 3 to 4 migration

art icon art

The art work of windwalker

authentication icon authentication

[READ ONLY] Authentication package to support multiple login methods.

cache icon cache

[READ ONLY] PSR-6 compatible cache package.

compare icon compare

[DEPRECATED] String comparation library.

console icon console

[DEPRECATED] A powerful console package for PHP, an alternative of Symfonys.

core icon core

The framework extended library for Windwalker Starter application.

crypt icon crypt

[READ ONLY] Openssl and libsodium encryption and password hashing adapters for PHP.

data icon data

[READ ONLY] A library contains data/collection objects with null-object pattern.

database icon database

[READ ONLY] Database adapter and type mapping to access multiple DB systems.

datamapper icon datamapper

[READ ONLY] DataMapper pattern object to access database.

di icon di

[READ ONLY] Dependency Injection library for PHP.

dom icon dom

[READ ONLY] A php Virtual-DOM library to help us build DOM string dynamically.

edge icon edge

[READ ONLY] A Blade compatible template engine with much extendable interface.

environment icon environment

[READ ONLY] A library to provider runtime server and browser information.

event icon event

[READ ONLY] Event dispatchers for PHP.

filesystem icon filesystem

[READ ONLY] Simple filesystem classes to wrap native PHP file operations.

filter icon filter

[READ ONLY] Input and output string filters for PHP.

form icon form

[READ ONLY] A HTML form builder based on Windwalker HTML and Virtual-DOM libraries.

framework icon framework

PHP Rapid Application Development Framework

html icon html

[READ ONLY] A powerful HTML building helpers.

http icon http

[READ ONLY] PSR-7 streaming, and HTTP foundation, client library, support httplug interface.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.