azjezz/http-normalizer

Normalize Http super globals

v1.1.0 2020-12-13 02:04 UTC

This package is auto-updated.

Last update: 2024-10-13 10:06:08 UTC


README

Normalize Http super globals.

Installation

This package can be install with Composer.

$ composer require azjezz/http-normalizer

Usage

Normalize

Normalize super globals : $_GET, $_POST, and $_COOKIE ... etc :

$_GET = [
  'a' => ['b' => '4'],
  'c' => [0 => 4, 'c' => ['s' => ['f' => [0 => '3']]]],
  'b' => '3',
  'foo' => [0 => 'baz', 1 => 'qux'],
];

$query = AzJezz\HttpNormalizer\normalize($_GET);

/**
 * dict [
 *   "a[b]" => "4",
 *   "c[0]" => "4",
 *   "c[c][s][f][0]" => "3",
 *   "b" => "3",
 *   "foo[0]" => "baz",
 *   "foo[1]" => "qux",
 * ]
 */

Normalize $_FILES super global :

$_FILES = [
  'slide-shows' => [
    'tmp_name' => [
      0 => ['slides' => [0 => 'tmpfoo', 1 => 'tmpbar']],
    ],
    'error' => [0 => ['slides' => [0 => 0, 1 => 0]]],
    'name' => [0 => ['slides' => [0 => 'foo.txt', 1 => 'bar.txt']]],
    'size' => [0 => ['slides' => [0 => 123, 1 => 200]]],
    'type' => [0 => ['slides' => [0 => 'text/plain', 1 => 'text/plain']]],
  ],
];

$files = AzJezz\HttpNormalizer\normalize_files($_FILES);

/**
 * dict [
 *   "slide-shows[0][slides][0]" => shape(
 *     'tmp_name' => 'tmpfoo',
 *     'size' => 123,
 *     'error' => 0,
 *     'name' => 'foo.txt',
 *     'type' =>'text/plain',
 *   ),
 *   "slide-shows[0][slides][1]" => shape(
 *     'tmp_name' => 'tmpbar',
 *     'size' => 200,
 *     'error' => 0,
 *     'name' => 'bar.txt',
 *     'type' =>'text/plain',
 *   ),
 * ]
 */

Parse

Parse http request body, query strings, cookie strings ... etc :

$input = 'a=b&c[]=4&b=3&a[b]=4&c[c][s][f][]=3&foo[]=baz&foo[]=qux';
$query = AzJezz\HttpNormalizer\parse($input);

/**
 * vec [
 *   "a[b]" => "4",
 *   "c[0]" => "4",
 *   "c[c][s][f][0]" => "3",
 *   "b" => "3",
 *   "foo[0]" => "baz",
 *   "foo[1]" => "qux",
 * ]
 */

License

The Http Normalizer Project is open-sourced software licensed under the MIT-licensed.