Change error reporting during the execution of a class

It’s generally not a good idea to mess about with error reporting levels – just turn them on while developing and off in production.

However, I recently came across a situation where I wanted to suppress the error notices being generated while developing a particular class.

An autoloader class was generating notices while looking for files – a useful feature as it shows the paths where it wasn’t finding the files, so I didn’t want to change the source of the error notices.

Instead, the solution was to change the error reporting levels in the class using the autoloader using PHP’s error_reporting function. This function returns the previous error reporting level when called, allowing it to be set back later on.

The code:

function __construct( \PDO $db, $p = array() )
{
	// Change error reporting level
	$this->old_error_reporting_level = error_reporting( E_ERROR | E_WARNING | E_PARSE );
}

....

function __destruct()
{
	// Change error reporting level back
	error_reporting( $this->old_error_reporting_level );
}

Maybe someone else will find this useful!

Using Font Awsome icons in a file list

In a recent WordPress project, the client needed the ability to display a list of downloads on certain pages.

The site was already using the aptly named Font Awesome, which has an icon for every occasion including many for file types, so I thought why not use an icon as a nice visual aid in the list of files.

This PHP function retrieves either the Font Awesome icon class or character code based on a mime-type by looking it up in a multi-dimensional array. If it can’t find the mime-type, it falls back on a generic file icon.

function getFontawesomeFileIcon( $mime_type, $class = true )
{
	$data = array(

		'default' => array('fa-file', 'f15b'),
		'application/x-7z-compressed' => array('fa-file-archive-o', 'f1c6'),
		'application/x-bzip' => array('fa-file-archive-o', 'f1c6'),
		'application/x-bzip2' => array('fa-file-archive-o', 'f1c6'),
		'application/x-rar-compressed' => array('fa-file-archive-o', 'f1c6'),
		'application/x-tar' => array('fa-file-archive-o', 'f1c6'),

		'audio/x-wav' => array('fa-file-audio-o', 'f1c7'),
		'audio/x-aac' => array('fa-file-audio-o', 'f1c7'),
		'audio/x-aiff' => array('fa-file-audio-o', 'f1c7'),
		'audio/x-ms-wma' => array('fa-file-audio-o', 'f1c7'),
		'audio/mpeg' => array('fa-file-audio-o', 'f1c7'),
		'audio/mp4' => array('fa-file-audio-o', 'f1c7'),
		'audio/ogg' => array('fa-file-audio-o', 'f1c7'),
		'audio/webm' => array('fa-file-audio-o', 'f1c7'),
		'audio/x-wav' => array('fa-file-audio-o', 'f1c7'),

		'text/css' => array('fa-file-code-o', 'f1c9'),
		'text/x-java-source,java' => array('fa-file-code-o', 'f1c9'),
		'application/javascript' => array('fa-file-code-o', 'f1c9'),
		'application/json' => array('fa-file-code-o', 'f1c9'),
		'text/x-pascal' => array('fa-file-code-o', 'f1c9'),

		'application/vnd.ms-excel' => array('fa-file-excel-o', 'f1c3'),
		'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => array('fa-file-excel-o', 'f1c3'),
		'text/csv' => array('fa-file-excel-o', 'f1c3'),

		'image/png' => array('fa-file-image-o', 'f1c5'),
		'image/svg+xml' => array('fa-file-image-o', 'f1c5'),
		'image/tiff' => array('fa-file-image-o', 'f1c5'),
		'image/bmp' => array('fa-file-image-o', 'f1c5'),
		'image/vnd.dwg' => array('fa-file-image-o', 'f1c5'),
		'image/gif' => array('fa-file-image-o', 'f1c5'),
		'image/jpeg' => array('fa-file-image-o', 'f1c5'),
		'image/vnd.adobe.photoshop' => array('fa-file-image-o', 'f1c5'),
		'image/x-pict' => array('fa-file-image-o', 'f1c5'),

		'video/quicktime' => array('fa-file-movie-o', 'f1c8'),
		'video/x-msvideo' => array('fa-file-movie-o', 'f1c8'),
		'video/x-flv' => array('fa-file-movie-o', 'f1c8'),
		'video/x-ms-asf' => array('fa-file-movie-o', 'f1c8'),
		'video/x-ms-wmv' => array('fa-file-movie-o', 'f1c8'),
		'video/mpeg' => array('fa-file-movie-o', 'f1c8'),
		'video/mp4' => array('fa-file-movie-o', 'f1c8'),
		'video/ogg' => array('fa-file-movie-o', 'f1c8'),
		'video/webm' => array('fa-file-movie-o', 'f1c8'),
		'video/x-matroska' => array('fa-file-movie-o', 'f1c8'),

		'application/pdf' => array('fa-file-pdf-o', 'f1c1'),

		'application/vnd.ms-powerpoint' => array('fa-file-powerpoint-o', 'f1c4'),
		'application/vnd.openxmlformats-officedocument.presentationml.presentation' => array('fa-file-powerpoint-o', 'f1c4'),

		'application/msword' => array('fa-file-word-o', 'f1c2'),
		'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => array('fa-file-word-o', 'f1c2'),

		'application/zip' => array('fa-file-zip-o', 'f1c6'),
	);

	if( !array_key_exists( $mime_type, $data) )
	{
		$mime_type = 'default';
	}

	if( $class )
	{
		return $data[$mime_type][0];
	}

	return $data[$mime_type][1];

}

Determining if an array is multidimensional

This is quick boolean test to see if an array is multi-dimensional (contains other arrays). It simply checks each element in an array to see if it is also an array and returns true as soon as one is found:

function isArrayMultiDimensional( $arr = [] )
{
	if( is_array($arr) )
	{
		foreach( $arr as $key => $value )
		{
			if( is_array($value) )
			{
				return true;
			}
		}
	}
	return false;
}

Useful array functions

Here area couple of array functions which I keep on finding myself searching for.

objectToArray converts an object into an array. This is not as trivial as it sounds, but luckily a PHP function exists that sort of does this, json_decode. The object is first converted into JSON then decoded from JSON into an associative array:

function objectToArray( $object )
	{
		if( is_object( $object ) )
		{
			return json_decode( json_encode($object), true );
		}

		return $object;
	}

flattenArray converts a multi-dimensional array into a flat array containing only the values:

function flattenArray( $arr = [] )
{
	$return = array();
	array_walk_recursive($arr, function($a) use (&$return) { $return[] = $a; });
	return $return;
}

This came from StackOverflow: http://stackoverflow.com/questions/1319903/how-to-flatten-a-multidimensional-array

sanitizeFilename function

Been using this for a while and I can’t remember exactly where it came from.

EDIT: It’s a simplified version of sanitize_file_name used by WordPress.

function sanitizeFilename( $filename )
{
	$special_chars = array("?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}", chr(0));

	$filename = preg_replace( "#\x{00a0}#siu", ' ', $filename );
	$filename = str_replace($special_chars, '', $filename);
	$filename = str_replace( array( '%20', '+' ), '-', $filename );
	$filename = preg_replace('/[\s-]+/', '-', $filename);
	$filename = trim($filename, '.-_');
	
	return $filename;
}