2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
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 // +----------------------------------------------------------------------+
19 // $Id: Parameter.php,v 1.1 2007/03/25 10:10:21 cweiske Exp $
22 * Class for working with MIME type parameters
26 * @author Ian Eure <ieure@php.net>
28 class MIME_Type_Parameter {
54 * @param string $param MIME parameter to parse, if set.
57 function MIME_Type_Parameter($param = false)
66 * Parse a MIME type parameter and set object fields
68 * @param string $param MIME type parameter to parse
71 function parse($param)
74 $param = MIME_Type::stripComments($param, $comment);
75 $this->name = $this->getAttribute($param);
76 $this->value = $this->getValue($param);
77 $this->comment = $comment;
82 * Get a parameter attribute (e.g. name)
84 * @param string MIME type parameter
85 * @return string Attribute name
88 function getAttribute($param)
90 $tmp = explode('=', $param);
96 * Get a parameter value
98 * @param string $param MIME type parameter
99 * @return string Value
102 function getValue($param)
104 $tmp = explode('=', $param, 2);
106 $value = trim($value);
107 if ($value[0] == '"' && $value[strlen($value)-1] == '"') {
108 $value = substr($value, 1, -1);
110 $value = str_replace('\\"', '"', $value);
116 * Get a parameter comment
118 * @param string $param MIME type parameter
119 * @return string Parameter comment
123 function getComment($param)
125 $cs = strpos($param, '(');
126 $comment = substr($param, $cs);
127 return trim($comment, '() ');
132 * Does this parameter have a comment?
134 * @param string $param MIME type parameter
135 * @return boolean true if $param has a comment, false otherwise
138 function hasComment($param)
140 if (strstr($param, '(')) {
148 * Get a string representation of this parameter
150 * This function performs the oppsite of parse()
152 * @return string String representation of parameter
156 $val = $this->name . '="' . str_replace('"', '\\"', $this->value) . '"';
157 if ($this->comment) {
158 $val .= ' (' . $this->comment . ')';