The Data service provides a convient wrapper around using HTTP GET, POST, PUT, and DELETE data, as well as helping determine if posted data/files are over the imposed server limits set by `max_post_size` and `upload_max_filesize` ini values.

It also aids in the creation of cookies and managing data set in $_COOKIE.

It also aids in CSRF token generation and validation using the openssl_random_pseudo_bytes(32) function.

PHP comes out of the box with the two suberglobals $_POST and $_GET, it does not however, provide the same superglobal access for what you would think would be $_PUT and $_DELETE data. The Data service will parse incoming data from a PUT or DELETE stream when the HTTP request is one of the two types of methods and allow the same type of superglobal access through its methods.

Examples #

// Getting values
$getVar = Data::get('someGetVariable');
$postVar = Data::post('somePostVariable');
$putVar = Data::put('somePutVariable');
$delVar = Data::delete('someDeleteVariable');


// Getting values that match a condition
$getVar = Data::get()->where('getVar','integer');
$postVar = Data::post()->where('postVar','^[a-zA-Z\s]+$');


// Getting multiple values that match a condition
$postData = Data::post()->where([
        'var1' => 'integer',
        'var2' => 'alpha_numeric',
        'var3' => '[a-z]{1,4}'
    ]);


// If the value returned is false then one of the matching conditions failed
if($postData !== false){
    // Good to go
}
else {
    // Handle the error
}


// Get values in an array
$postData = Data::post(['var1','var2','var3']);


// Get all the values
$allPostData = Data::post()->all();
$allDeleteData = Data::delete()->all();


// Delete a value
Data::post()->remove('post-value-to-remove');


// Setting values
Data::delete()->set('someDelete',52);
Data::get()->set('someGet','donut');
Data::post()->set('somePost','pictures');
Data::put()->set('somePut','someputvalue');


// Determine if the posted data exceeded the `max_post_size` ini limit imposed by the server
if( Data::isPostedDataSizeOverLimit() ){
    //yep
}


// Determine if the posted files size exceeded the `upload_max_filesize` ini limit imposed by the server
if( Data::isPostedFileSizeOverLimit() ){
    //yep
}


//Human friendly file size of `upload_max_filesize` ini value.
$human = Data::iniMaxFileSize();


// Get the value of the current CSRF token, if a token doesn't exist, it will be created. 
// When the token is created it is stored in in the session variable identified by the key `CSRF_TOKEN_NAME` specified in `app/config/config.php`
// If you pass boolean `true` to the function it will regenerate the CSRF token regardless of if one exists already
$token = Data::getCSRFToken();


// Get a hidden input that contains the name and value of the CSRF Token.
$input = Data::getCSRFTokenInput();


// Validate the CSRF token passed with the request, either via HTTP Headers (where it should be identified  by the header key `HTTP_X_CSRF_TOKEN`) 
// or in the POST, PUT, or DELETE data (where it should be identified by the key `CSRF_TOKEN_NAME` specified in `app/config/config.php`).
$validates = Data::validateCSRFToken();


// Create a cookie that will be available across the entire domain and expires after 30 days (the default time)
// The setCookie & setRawCookie accepts the same paramaters as the SPL function setcookie & setrawcookie
// With the exception that array or object values passed will be JSON encoded prior to being stored (see getComplexCookie) below
// And expiration times passed can be passed in as a string ie `+2 days`, 
// or as an integer (seconds) that will be added to the current time ie `180` means expire in 3 minutes
Data::setCookie('key', 'value');

// Create a cookie that lasts for 10 days
Data::setRawCookie('key', 'value', '+10 days');

// Create a cookie that lasts for 3 minutes
Data::setCookie('key', 'value', 180);

// Is a cookie set?
if( Data::hasCookie('key') ) {
    // When you attempt to get a cookie that is not set it will return `false`
    $value = Data::getCookie('key');
}

// Delete a cookie
Data::deleteCookie('key');

// You can create cookies with values that are arrays or objects, the array or object will be JSON encoded before it is stored
Data::createCookie('key', ['foo' => 'bar', 'baz' => 'foz']);

// Get the value of a complex cookie, JSON decoding the value as an array before returning it
$foos = Data::getComplexCookie('key');
'bar' === $foos['foo'];