Skip to content

Commit

Permalink
MagicInput class
Browse files Browse the repository at this point in the history
  • Loading branch information
ilham-fuead committed May 8, 2017
1 parent 7f892d6 commit de94757
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 12 deletions.
54 changes: 54 additions & 0 deletions src/mandryn_v1/MagicInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Handle inputs from GET, POST & Raw JSON and copy as object properties
*
* Magicly copy and sanitize inputs
*
* @category Utility, Security
* @package Mandryn/Mandryn
* @author Mohd Ilhammuddin Bin Mohd Fuead <ilham.fuead@gmail.com>
* @copyright 2017-2022 The Mandryn Team
* @license http://www.php.net/license/3_01.txt PHP License 3.01
* @version Release: 1.0.0
* @since Class available since Release 2.1.0
*/
class MagicInput extends MagicObject{
/**
*
* @param boolean $apply_sanitize sanitize input before assign to object. Default to true.
*/
public function copy_GET_properties($apply_sanitize=true){

$GET_array=$apply_sanitize?filter_input_array(INPUT_GET,FILTER_SANITIZE_STRING):$_GET;

$this->copyArrayProperties($GET_array, true);
}

/**
*
* @param boolean $apply_sanitize sanitize input before assign to object. Default to true.
*/
public function copy_POST_properties($apply_sanitize=true){

$POST_array=$apply_sanitize?filter_input_array(INPUT_POST,FILTER_SANITIZE_STRING):$_POST;

$this->copyArrayProperties($POST_array, true);
}

/**
*
* @param boolean $apply_sanitize sanitize input before assign to object. Default to true.
*/
public function copy_RAW_JSON_properties($apply_sanitize=true){
$request = file_get_contents('php://input');

/*2nd parameter supply true to convert request as input array, false as input object*/
$input = json_decode($request, true);

if($apply_sanitize){
$input=filter_var_array($input,FILTER_SANITIZE_STRING);
}

$this->copyArrayProperties($input);
}
}
46 changes: 34 additions & 12 deletions src/mandryn_v1/MagicObject.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
<?php

/**
* Create universal object and handle on-the-fly properties
*
* Create object with on-the-fly properties
*
* @category Utility
* @package Mandryn/Mandryn
* @author Mohd Ilhammuddin Bin Mohd Fuead <ilham.fuead@gmail.com>
* @copyright 2017-2022 The Mandryn Team
* @license http://www.php.net/license/3_01.txt PHP License 3.01
* @version Release: 1.1.0
* @since Class available since Release 2.0.0
*/
class MagicObject {

private $property;
Expand All @@ -21,22 +33,32 @@ public function __get($name) {
public function __isset($name) {
return isset($name);
}

public function getJsonString() {
$jsonStr='{';
foreach ($this->property as $key=>$val){
if($jsonStr!='{'){
$jsonStr.=',';
}
$jsonStr.="\"$key\":\"$val\"";

/**
*
* @param array $array
* @param boolean $disposeSource
*/
public function copyArrayProperties(array &$array,$disposeSource=false){
foreach ($array as $key=>$value){
$this->property[$key]=$value;
}
$jsonStr.='}';

return $jsonStr;
if($disposeSource){
unset($array);
}
}


public function getJsonString() {
return json_encode($this->property);
}

public function __unset($name) {
unset($name);
}

public function __destruct() {
unset($this->property);
}

}

0 comments on commit de94757

Please sign in to comment.