Ejemplo n.º 1
0
	/**
	 * Search a value in a (multidimensional) array - replacement for array_search().
	 *
	 * This function returns the key of the element that contains the specified keyword.
	 * The element can be an array, it will be recursively scanned for the keyword.
	 * The key of the parent element (not the child element) which contains the keyword
	 * will be returned. If there are more than one elements with the specified keyword
	 * the key of the first found element will be returned. If nothing is found boolean
	 * FALSE will be returned.
	 *
	 * Example:
	 * <code>
	 * $mimeTypes = array(
	 *	'image/gif' => array('gif'),
	 *	'image/jpeg' => array('jpe', 'jpeg', 'jpg'),
	 *	'image/png' => array('png', 'x-png'),
	 * );
	 * echo Arrays::find($mimeTypes, 'jpg');
	 * // Output: image/jpeg
	 * </code>
	 *
	 * @param array Array to search in
	 * @param mxied Text to find
	 * @return mixed Key of array or false on failure
	 */
	public static function find(array $array, $keyword) {
		foreach($array as $key => $value) {
			if($keyword === $value || (is_array($value) == true && Arrays::find($array, $value) !== false)) {
				return $key;
			}
		}
		return false;
	}
	/**
	 * Returns the mime type for a specified file.
	 *
	 * You can specify the path to a file or an extension (with our without leading dot).
	 * If you specify a valid file name we try to use the fileinfo extension to 
	 * determine the mime type, if this fails we use the mapping array.
	 * If you specify an invalid file or just an extension we use the mapping array.
	 * This function returns a mime type (in lowercase) for an extension.
	 * NULL will be returned if no mime type is found.
	 * 
	 * Note: Be careful when specifying files like .htaccess or README. 
	 * This could be a valid file or an extension, this function assumes that
	 * every input is a vlid file and, if this fails, we check for the extension mapping.
	 * 
	 * @link http://www.php.net/fileinfo
	 * @param string File extension without dot
	 * @return string Mime type
	 */
	public static function getMimeType($file) {
		$mimeType = false;
		if (class_exists('finfo', false) && file_exists($file)) {
			$finfo = new finfo(FILEINFO_MIME_TYPE);
			if ($finfo) {
				$mimeType = $finfo->file($file);
			}
		}
		if ($mimeType === false) {
			$ext = new File($file);
			$mimeType = Arrays::find(self::$data, $ext->extension());
		}
		return ($mimeType === false) ? null : $mimeType;
	}