PHP Classes

PHP Request Parameter Options: Process request parameters assuming default values

Recommend this page to a friend!
  Info   View files Example   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum (2)   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 160 This week: 1All time: 8,960 This week: 560Up
Version License PHP version Categories
parameters-handler 1.0.2BSD License5.2HTTP, PHP 5
Description 

Author

This class can process request parameters assuming default values.

It can take an associative array with default values for given parameters.

The class checks GET and POST parameters or a request that sends a JSON object value in the body.

It merges the request values by a given order and fill the missing parameters with the defaults values.

Innovation Award
PHP Programming Innovation award nominee
November 2015
Number 13
PHP has built-in support to retrieve POST or GET request parameters since the first PHP versions.

However, when you want to decode parameters passed via PUT or other methods, you need to extract and decode the values in whatever format they are coming.

This class can extract request parameters including JSON objects passed in the request body besides any POST or GET values.

Manuel Lemos
Picture of Vallo Reima
  Performance   Level  
Name: Vallo Reima is available for providing paid consulting. Contact Vallo Reima .
Classes: 6 packages by
Country: Estonia Estonia
Age: ???
All time rank: 8333 in Estonia Estonia
Week rank: 411 Up3 in Estonia Estonia Equal
Innovation award
Innovation award
Nominee: 3x

Example

<?php

/*
 * PHP parameters and Options handler
 *
 * Usage sample
 *
 * @package System
 * @author Vallo Reima
 * @copyright (C)2015
 */

error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);
ini_set('log_errors', false);

/* sample default options */
$def = array(
   
'sgn' => true,
   
'exf' => ['*.min.*'],
   
'sfx' => '_pkd',
   
'aon' => [],
   
'arc' => 'zip',
   
'tml' => 30
);

/* sample request */
$_GET = array('sgn' => '', 'arc' => '7z','aon' => ['add']);
$_POST = array('sfx' => '', 'arc' => 'tar', 'exf' => null);

require(
'ParmOpts.php'); // load the class

$obj = new ParmOpts(); // instantiate with default priority

$opt = $obj->Opts($def); // assign settings, update with the request values

$prm = $obj->Get(); // request parameters

/* display result */
header('Content-Type: text/html; charset=utf-8');
echo
'ParmOpts usage sample<br><br>';
echo
'Options<br>';
echo
'<pre>';
print_r($opt); // updated options
echo '</pre>';
echo
'Parameters<br>';
echo
'<pre>';
print_r($prm); // accepted parameters
echo '</pre>';


Details

PHP parameters and Options handler

There are two typical tasks, among the others, when you initialise the PHP script:

  • accept the request or CLI parameters;
  • assign the settings' values basing on the default options and the parameter values.

The ParmOpts class supplies the methods simplifying these tasks.

How it works

The data input is detected during the class instantiation and the request or CLI parameters are saved. The request parameters are accepted from different sources by overwriting the values of the same key according to given priority.

The default options are updated with the input parameter values by adjusting the data types taken from the defaults. The allowance is checked before adjusting the type or replacing the default value with the empty one. The saved parameters and options can be accessed as arrays or objects.

The usage

Instantiation

$obj = new ParmOpts( [ $pty ] );

$pty - the merging priority of the request parameters (higher to a lower, default by 'JPG'):

  • J - json data
  • P - post data
  • G - get data

The parameters are saved by the constructor.

Methods

$obj->Opts( $opt [, $rqt = null ] );

Save the options, updating the defaults with the parameter values.

  • $opt - default options associative array ('name' => 'default value')
  • $rqt - request parameters (default by input parameters)

$obj->Get( [ $prp = 'rqt' [, $flg = false ] ] );

Read the saved data.

  • $prp - property: - 'rqt' - request or CLI parameter values - 'opt' - option values - 'jsn' - json request flag (true/false)
  • $flg - data format (ignored for 'jsn'): - true - dual, can be accessed as array or object - false - array

The package

Upload the files to any web directory and run the example.php. The following files are included:

  • ParmOpts.php - the class to handle parameters and options
  • example.php - usage sample
  • readme.md

ChangeLog

01 Apr 2016

  • ParmOpts.php - jsn property - json content (array) or missing

22 Nov 2017

  • ParmOpts.php - mds property - several modes (array): - http2 - HTTP/2 protocol (true) - https - secured connection (true) - xhr - AJAX request (true) - rqm - request method: CLI - command line, else GET, POST,...

Please [contact] on any product-related questions.

[contact]: mailto://vallo@vregistry.com


  Files folder image Files  
File Role Description
Accessible without login Plain text file example.php Example Example script
Plain text file ParmOpts.php Class Class source
Accessible without login Plain text file README.md Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:160
This week:1
All time:8,960
This week:560Up
User Comments (1)