Skip to content

Instantly share code, notes, and snippets.

@willwoodlief
Created December 6, 2017 05:09
Show Gist options
  • Save willwoodlief/9c431fbb55b376982b28cd20dc29d29f to your computer and use it in GitHub Desktop.
Save willwoodlief/9c431fbb55b376982b28cd20dc29d29f to your computer and use it in GitHub Desktop.
Some General Helpers I keep using
<?php
class JsonHelpers {
//helper function lifted from the transcription project
public static function printOkJSONAndDie($phpArray=[]) {
if (!is_array($phpArray)) {
$r=[];
$r['message'] = $phpArray;
$phpArray = $r;
}
header('Content-Type: application/json');
$phpArray['status'] = '200';
$phpArray['valid'] = true;
$out = json_encode($phpArray);
if ($out) {
print $out;
} else {
self::printErrorJSONAndDie( json_last_error_msg());
}
exit;
}
//helper function lifted from the transcription project
public static function printErrorJSONAndDie($message,$phpArray=[]) {
header('Content-Type: application/json');
$phpArray['status'] = 'error';
$phpArray['status'] = '500';
$phpArray['valid'] = false;
$phpArray['message'] = $message;
$out = json_encode($phpArray);
if ($out) {
print $out;
} else {
print json_last_error_msg();
}
exit;
}
//throws error if cannot convert
/**
* Converts a php array to a json string
* @param $phpArray
* @return string
* @throws Exception if json error
*/
public static function toString( $phpArray) {
$out = json_encode($phpArray);
if ($out) {
return $out;
} else {
print "error in json\n";
$oops = json_last_error_msg();
throw new Exception($oops);
}
}
/**
* Returns the decoded json from the input
* if input is null then returns null
* if input is empty returns []
* else converts the string value of the input from a json string to a php structure
* @param mixed $what
* @return array|mixed|null
* @throws Exception if json error
*/
public static function fromString($what) {
if (is_null($what) ) { return null;}
if (empty($what)) {return [];}
$what = strval($what);
if ( strcasecmp($what,'null') == 0) {return null;}
$out = json_decode(strval($what), true);
if (is_null($out)) {
$oops = json_last_error_msg();
$oops .= "\n Data: \n". $what;
throw new Exception($oops);
} else {
return $out;
}
}
/**
* if the param is an array then processed by toString,
* but if not an array then the string casting of mixed is returned
* if boolean returns a '1' or '0'
* finally, if null then null is returned
* @param $what mixed any value
* @throws Exception
* @return string
*/
public static function toStringAgnostic($what) {
if (is_null($what)) {return null;}
if (is_bool($what)) {
if ($what) {return '1';}
else {return '0';}
}
if (is_array($what)) {
return self::toString($what);
} else {
return strval($what);
}
}
/**
* Use this to inspect json returns
* Debug, prints array information to the screen in an easy to read html table
* I have been using this for years, and its not mine, forget where I found it
* @example print_nice($array)
* @param $elem, the only thing to use when calling it, the rest of the method params is for the recursion
* @param int $max_level
* @param array $print_nice_stack
* @return void it prints to the screen
*/
public static function print_nice($elem,$max_level=15,$print_nice_stack=array()){
//if (is_object($elem)) {$elem = object_to_array($elem);}
if(is_array($elem) || is_object($elem)){
if(in_array($elem,$print_nice_stack,true)){
echo "<span style='color:red'>RECURSION</span>";
return;
}
$print_nice_stack[]=&$elem;
if($max_level<1){
echo "<span style='color:red'>reached maximum level</span>";
return;
}
$max_level--;
echo "<table border=1 cellspacing=0 cellpadding=3 width=100%>";
if(is_array($elem)){
echo '<tr><td colspan=2 style="background-color:#333333;"><strong><span style="color:white">ARRAY</span></strong></td></tr>';
}else{
echo '<tr><td colspan=2 style="background-color:#333333;"><strong>';
echo '<span style="color:white">OBJECT Type: '.get_class($elem).'</span></strong></td></tr>';
}
$color=0;
foreach($elem as $k => $v){
if($max_level%2){
$rgb=($color++%2)?"#888888":"#44BBBB";
}else{
$rgb=($color++%2)?"#777777":"#22BBFF";
}
echo '<tr><td valign="top" style="width:40px;background-color:'.$rgb.';">';
echo '<strong style="color:black">'.$k."</strong></td><td style='background-color:white;color:black'>";
self::print_nice($v,$max_level,$print_nice_stack);
echo "</td></tr>";
}
echo "</table><br><br>";
return;
}
if($elem === null){
echo "<span style='color:green'>NULL</span>";
}elseif($elem === 0){
echo "0";
}elseif($elem === true){
echo "<span style='color:green'>TRUE</span>";
}elseif($elem === false){
echo "<span style='color:green'>FALSE</span>";
}elseif($elem === ""){
echo "<span style='color:green'>EMPTY STRING</span>";
}else{
echo str_replace("\n","<strong><span style='color:green'>*</span></strong><br>\n",$elem);
}
}
/**
* Outputs information about a class. Sometimes it helps to see data objects this way
* Not really json like, but it goes with print nice as a set of debug functions
* @param $object
* @throws Exception
*/
public static function TO($object){ //Test Object
if(!is_object($object)){
throw new Exception("This is not a Object");
}
if(class_exists(get_class($object), true)) echo "<pre>CLASS NAME = ".get_class($object);
$reflection = new ReflectionClass(get_class($object));
echo "<br />";
echo $reflection->getDocComment();
echo "<br />";
$metody = $reflection->getMethods();
foreach($metody as $key => $value){
echo "<br />". $value;
}
echo "<br />";
$vars = $reflection->getProperties();
foreach($vars as $key => $value){
echo "<br />". $value;
}
echo "</pre>";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment