Created
October 31, 2017 08:09
-
-
Save aonurdemir/c76a969342d5ccca63fca1f81081d002 to your computer and use it in GitHub Desktop.
JsonValidator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: aonurdemir | |
* Date: 31/10/2017 | |
* Time: 10:03 | |
*/ | |
class JsonValidator | |
{ | |
const BOOLEAN = "boolean"; | |
const INTEGER = "integer"; | |
const DOUBLE = "double"; | |
const STRING = "string"; | |
const ARRAY_ = "array"; | |
const OBJECT = "object"; | |
//TODO required check | |
//TODO optional check | |
public static function validateJson($json,$mappings){ | |
if(empty($mappings)){ | |
throw new Exception("mappings array is empty"); | |
} | |
foreach ($mappings as $mapping_key=>$mapping_value){ | |
//base case | |
if(gettype($mapping_value) !== self::ARRAY_){ | |
if(gettype($json[$mapping_key]) !== $mapping_value){ | |
return false; | |
} | |
} | |
//recursive part. Since it passes base case, json value corresponding to the mapping key should be an array. | |
else{ | |
if(gettype($mapping_key) === self::INTEGER){//checks for array of objects. i.e "regions":[{},{}] | |
$result = true; | |
foreach ($json as $json_key=>$json_value){ | |
$result = $result && self::validateJson($json_value,$mapping_value); | |
} | |
return $result; | |
} | |
else{ | |
return self::validateJson($json[$mapping_key],$mapping_value); | |
} | |
} | |
} | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$validator = array("setting"=>array( | |
"student_id"=>JsonValidator::INTEGER, | |
"regions"=>array(//this indicates that, regions is an array consisting of multiple objects | |
//including below fields. | |
array( | |
"lat"=>JsonValidator::DOUBLE, | |
"lon"=>JsonValidator::DOUBLE, | |
"radius"=>JsonValidator::DOUBLE, | |
"gsm"=>JsonValidator::STRING, | |
"day_part"=>JsonValidator::STRING, | |
"is_pn_enabled"=>JsonValidator::BOOLEAN, | |
"is_sms_enabled"=>JsonValidator::BOOLEAN)))); | |
$validated = JsonValidator::validateJson($setting,$validator); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"setting": { | |
"student_id": 123, | |
"regions": [ | |
{ | |
"lat": 11.0, | |
"lon": 22.0, | |
"radius": 1.0, | |
"gsm": "5051230055", | |
"day_part": "morning", | |
"is_pn_enabled": true, | |
"is_sms_enabled": true | |
}, | |
{ | |
"lat": 33.0, | |
"lon": 44.0, | |
"radius": 5.0, | |
"gsm": "5554443322", | |
"day_part": "evening", | |
"is_pn_enabled": false, | |
"is_sms_enabled": false | |
} | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment