PHP code example of josantonius / request
1. Go to this page and download the library: Download josantonius/request library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
josantonius / request example snippets
Request::isGet();
Request::isPost();
Request::isPut();
Request::isDelete();
Request::input($type);
asArray($filters, $default);
asObject($filters, $default);
asJson($default);
asString($default);
asInteger($default);
asFloat($default);
asBoolean($default);
asIp($default);
asUrl($default);
asEmail($default);
use Josantonius\Request\Request;
use Josantonius\Request\Request;
use Josantonius\Validate\Validate;
'user_name' => 'John'
'user_surname' => 'Doe'
'user_age' => 35
'user_rating' => 8.5
'user_ip' => '89.58.54.188'
'user_website' => 'http://www.site.com/'
'user_email' => '[email protected] '
'user_address' => [
'street' => 'unknown'
'locality' => 'Seville'
'country' => 'Spain'
]
'is_active' => true
Request::isGet(); // true or false
Request::isPost(); // true or false
Request::isPut(); // true or false
Request::isDelete(); // true or false
$_GET = Request::input('GET');
$_POST = Request::input('POST');
$_PUT = Request::input('PUT');
$_DELETE = Request::input('DELETE');
$array = $_GET()->asArray();
$array = $_POST()->asArray();
$array = $_PUT()->asArray();
$array = $_DELETE()->asArray();
var_dump($array);
/*
array(9) {
["user_name"]=>
string(4) "John"
["user_surname"]=>
string(3) "Doe"
["user_age"]=>
int(35)
["user_rating"]=>
float(8.5)
["user_ip"]=>
string(12) "89.58.54.188"
["user_website"]=>
string(20) "http://www.site.com/"
["user_email"]=>
string(13) "[email protected] "
["user_address"]=>
array(3) {
["street"]=>
string(7) "unknown"
["locality"]=>
string(7) "Seville"
["country"]=>
string(5) "Spain"
}
["is_active"]=>
bool(true)
}
*/
$filters = [
'user_name' => 'string',
'user_age' => 'string',
'is_online' => 'boolean'
];
$array = $_GET()->asArray($filters);
$array = $_POST()->asArray($filters);
$array = $_PUT()->asArray($filters);
$array = $_DELETE()->asArray($filters);
var_dump($array['user_name']); // string(4) "John"
var_dump($array['user_age']); // string(2) "35" (although an integer is received, it's returned as a string)
var_dump($array['user_age']); // NULL (doesn't exist, the default value is returned)
$filters = [
'user_rating' => 'float',
'is_active' => 'boolean',
'is_online' => 'boolean'
];
$array = $_GET()->asArray($filters, '');
$array = $_POST()->asArray($filters, '');
$array = $_PUT()->asArray($filters, '');
$array = $_DELETE()->asArray($filters, '');
var_dump($array['user_rating']); // float(8.5)
var_dump($array['is_active']); // bool(true)
var_dump($array['is_online']); // string(0) "" (doesn't exist, the default value is returned)
$object = $_GET()->asObject();
$object = $_POST()->asObject();
$object = $_PUT()->asObject();
$object = $_DELETE()->asObject();
var_dump($object);
/*
object(stdClass)#1 (9) {
["user_name"]=>
string(4) "John"
["user_surname"]=>
string(3) "Doe"
["user_age"]=>
int(35)
["user_rating"]=>
float(8.5)
["user_ip"]=>
string(12) "89.58.54.188"
["user_website"]=>
string(20) "http://www.site.com/"
["user_email"]=>
string(13) "[email protected] "
["user_address"]=>
object(stdClass)#2 (3) {
["street"]=>
string(7) "unknown"
["locality"]=>
string(7) "Seville"
["country"]=>
string(5) "Spain"
}
["is_active"]=>
bool(true)
}
*/
$filters = [
'user_name' => 'string',
'user_age' => 'integer',
'is_online' => 'boolean'
];
$object = $_GET()->asObject($filters);
$object = $_POST()->asObject($filters);
$object = $_PUT()->asObject($filters);
$object = $_DELETE()->asObject($filters);
var_dump($object->user_name); // string(4) "John"
var_dump($object->user_age); // int(35)
var_dump($object->user_age); // NULL (doesn't exist, the default value is returned)
$filters = [
'user_rating' => 'float',
'user_surname' => 'boolean',
'is_online' => 'boolean',
'is_member' => 'boolean'
];
$object = $_GET()->asObject($filters, false);
$object = $_POST()->asObject($filters, false);
$object = $_PUT()->asObject($filters, false);
$object = $_DELETE()->asObject($filters, false);
var_dump($object->user_rating); // float(8.5)
var_dump($object->user_surname); // string(3) "Doe"
var_dump($object->is_online); // bool(false) (doesn't exist, the default value is returned)
var_dump($object->is_member); // bool(false) (doesn't exist, the default value is returned)
$json = $_GET()->asJson();
$json = $_POST()->asJson();
$json = $_PUT()->asJson();
$json = $_DELETE()->asJson();
var_dump($json);
/*
string(260) "{"user_name":"John","user_surname":"Doe","user_age":35,"user_rating":8.5,"user_ip":"89.58.54.188","user_website":"http:\/\/www.site.com\/","user_email":"[email protected] ","user_address":{"street":"unknown","locality":"Seville","country":"Spain"},"is_active":true}"
*/
$json = $_GET('user_address')->asJson();
var_dump($json); // string(59) "{"street":"unknown","locality":"Seville","country":"Spain"}"
$json = $_POST('user_name')->asJson();
var_dump($json); // string(6) ""John""
$json = $_PUT('is_online')->asJson();
var_dump($json); // NULL (doesn't exist, the default value is returned)
$json = $_DELETE('user_address')->asJson([]);
var_dump($json); // string(2) "[]" (doesn't exist, the default value is returned)
$string = $_GET('user_age')->asString();
var_dump($string); // string(2) "35" (although an integer is received, it's returned as a string)
$string = $_POST('user_name')->asString();
var_dump($string); // string(4) "John"
$string = $_PUT('user_address')->asString();
var_dump($string); // NULL (it's an array, the default value is returned)
$string = $_DELETE('user_address')->asString('unknown');
var_dump($string); // string(7) "unknown" (it's an array, the default value is returned)
$integer = $_GET('user_age')->asInteger();
var_dump($integer); // int(35)
$integer = $_PUT('user_rating')->asInteger();
var_dump($integer); // NULL (it's a float, the default value is returned)
$integer = $_DELETE('user_rating')->asInteger(5);
var_dump($integer); // int(5) (it's a float, the default value is returned)
$float = $_GET('user_age')->asFloat();
var_dump($float); // float(35) (although an integer is received, it's returned as a float)
$float = $_POST('user_rating')->asFloat();
var_dump($float); // float(8.5)
$float = $_PUT('user_name')->asFloat();
var_dump($float); // NULL (it's a string, the default value is returned)
$float = $_DELETE('user_name')->asFloat(5.5);
var_dump($float); // float(5.5) (it's a string, the default value is returned)
$_GET['is_active'] = true;
$boolean = $_GET('is_active')->asBoolean();
var_dump($boolean); // bool(true)
$_GET['is_active'] = 'true';
$boolean = $_GET('is_active')->asBoolean();
var_dump($boolean); // bool(true)
$_POST['is_active'] = '1';
$boolean = $_POST('is_active')->asBoolean();
var_dump($boolean); // bool(true)
$_POST['is_active'] = 1;
$boolean = $_POST('is_active')->asBoolean();
var_dump($boolean); // bool(true)
$_GET['is_active'] = false;
$boolean = $_GET('is_active')->asBoolean();
var_dump($boolean); // bool(false)
$_GET['is_active'] = 'false';
$boolean = $_GET('is_active')->asBoolean();
var_dump($boolean); // bool(false)
$_POST['is_active'] = '0';
$boolean = $_POST('is_active')->asBoolean();
var_dump($boolean); // bool(false)
$_POST['is_active'] = 0;
$boolean = $_POST('is_active')->asBoolean();
var_dump($boolean); // bool(false)
$boolean = $_PUT('user_name')->asBoolean();
var_dump($boolean); // NULL (it's a string, the default value is returned)
$boolean = $_DELETE('is_online')->asBoolean(false);
var_dump($boolean); // bool(false) (doesn't exist, the default value is returned)
$ip = $_GET('user_ip')->asIp();
var_dump($ip); // string(12) "89.58.54.188"
$ip = $_POST('user_rating')->asIp();
var_dump($ip); // NULL (it's not an IP, the default value is returned)
$ip = $_DELETE('user_name')->asIp("87.32.48.164");
var_dump($ip); // string(12) "87.32.48.164" (it's not an IP, the default value is returned)
$url = $_GET('user_website')->asUrl();
var_dump($url); // string(20) "http://www.site.com/"
$url = $_POST('user_rating')->asUrl();
var_dump($url); // NULL (it's not an URL, the default value is returned)
$url = $_DELETE('user_name')->asUrl("http://www.site.com/");
var_dump($url); // string(20) "http://www.site.com/" (it's not an URL, the default value is returned)
$email = $_GET('user_website')->asEmail();
var_dump($email); // string(13) "[email protected] "
$email = $_POST('user_rating')->asEmail();
var_dump($email); // NULL (it's not an email, the default value is returned)
$email = $_DELETE('user_name')->asEmail("[email protected] ");
var_dump($email); // string(13) "[email protected] " (it's not an email, the default value is returned)