1<?php 2/* vim: set expandtab tabstop=4 shiftwidth=4: */ 3// +----------------------------------------------------------------------+ 4// | PHP version 4 | 5// +----------------------------------------------------------------------+ 6// | Copyright (c) 1997-2002 The PHP Group | 7// +----------------------------------------------------------------------+ 8// | This source file is subject to version 3.0 of the PHP license, | 9// | that is bundled with this package in the file LICENSE, and is | 10// | available at through the world-wide-web at | 11// | http://www.php.net/license/3_0.txt. | 12// | If you did not receive a copy of the PHP license and are unable to | 13// | obtain it through the world-wide-web, please send a note to | 14// | license@php.net so we can mail you a copy immediately. | 15// +----------------------------------------------------------------------+ 16// | Authors: Ian Eure <ieure@php.net> | 17// +----------------------------------------------------------------------+ 18// 19// $Id: Parameter.php,v 1.1 2004/06/16 08:46:19 ieure Exp $ 20 21/** 22 * Class for working with MIME type parameters 23 * 24 * @version 1.0.0 25 * @package MIME_Type 26 * @author Ian Eure <ieure@php.net> 27 */ 28class MIME_Type_Parameter { 29 /** 30 * Parameter name 31 * 32 * @var string 33 */ 34 var $name; 35 36 /** 37 * Parameter value 38 * 39 * @var string 40 */ 41 var $value; 42 43 /** 44 * Parameter comment 45 * 46 * @var string 47 */ 48 var $comment; 49 50 51 /** 52 * Constructor. 53 * 54 * @param string $param MIME parameter to parse, if set. 55 * @return void 56 */ 57 function MIME_Type_Parameter($param = false) 58 { 59 if ($param) { 60 $this->parse($param); 61 } 62 } 63 64 65 /** 66 * Parse a MIME type parameter and set object fields 67 * 68 * @param string $param MIME type parameter to parse 69 * @return void 70 */ 71 function parse($param) 72 { 73 $this->name = $this->getAttribute($param); 74 $this->value = $this->getValue($param); 75 if ($this->hasComment($param)) { 76 $this->comment = $this->getComment($param); 77 } 78 } 79 80 81 /** 82 * Get a parameter attribute (e.g. name) 83 * 84 * @param string MIME type parameter 85 * @return string Attribute name 86 * @static 87 */ 88 function getAttribute($param) 89 { 90 $tmp = explode('=', $param); 91 return trim($tmp[0]); 92 } 93 94 95 /** 96 * Get a parameter value 97 * 98 * @param string $param MIME type parameter 99 * @return string Value 100 * @static 101 */ 102 function getValue($param) 103 { 104 $tmp = explode('=', $param); 105 $value = $tmp[1]; 106 if (MIME_Type_Parameter::hasComment($param)) { 107 $cs = strpos($value, '('); 108 $value = substr($value, 0, $cs); 109 } 110 return trim($value, '" '); 111 } 112 113 114 /** 115 * Get a parameter comment 116 * 117 * @param string $param MIME type parameter 118 * @return string Parameter comment 119 * @see getComment() 120 * @static 121 */ 122 function getComment($param) 123 { 124 $cs = strpos($param, '('); 125 $comment = substr($param, $cs); 126 return trim($comment, '() '); 127 } 128 129 130 /** 131 * Does this parameter have a comment? 132 * 133 * @param string $param MIME type parameter 134 * @return boolean true if $param has a comment, false otherwise 135 * @static 136 */ 137 function hasComment($param) 138 { 139 if (strstr($param, '(')) { 140 return true; 141 } 142 return false; 143 } 144 145 146 /** 147 * Get a string representation of this parameter 148 * 149 * This function performs the oppsite of parse() 150 * 151 * @return string String representation of parameter 152 */ 153 function get() 154 { 155 $val = $this->name.'="'.$this->value.'"'; 156 if ($this->comment) { 157 $val .= ' ('.$this->comment.')'; 158 } 159 return $val; 160 } 161} 162?>