1<?php
2/* vim: set expandtab tabstop=4 shiftwidth=4: */
3/**
4 * Part of MIME_Type
5 *
6 * PHP version 4 and 5
7 *
8 * @category File
9 * @package  MIME_Type
10 * @author   Ian Eure <ieure@php.net>
11 * @license  http://www.gnu.org/copyleft/lesser.html LGPL
12 * @link     http://pear.php.net/package/MIME_Type
13 */
14
15/**
16 * Class for working with MIME type parameters
17 *
18 * @category File
19 * @package  MIME_Type
20 * @author   Ian Eure <ieure@php.net>
21 * @license  http://www.gnu.org/copyleft/lesser.html LGPL
22 * @version  Release: @version@
23 * @link     http://pear.php.net/package/MIME_Type
24 */
25class MIME_Type_Parameter
26{
27    /**
28     * Parameter name
29     *
30     * @var string
31     */
32    public $name;
33
34    /**
35     * Parameter value
36     *
37     * @var string
38     */
39    public $value;
40
41    /**
42     * Parameter comment
43     *
44     * @var string
45     */
46    public $comment;
47
48
49    /**
50     * Constructor.
51     *
52     * @param string $param MIME parameter to parse, if set.
53     */
54    public function __construct($param = false)
55    {
56        if ($param) {
57            $this->parse($param);
58        }
59    }
60
61
62    /**
63     * Parse a MIME type parameter and set object fields
64     *
65     * @param string $param MIME type parameter to parse
66     *
67     * @return void
68     */
69    function parse($param)
70    {
71        $comment = '';
72        $param   = MIME_Type::stripComments($param, $comment);
73        $this->name    = $this->getAttribute($param);
74        $this->value   = $this->getValue($param);
75        $this->comment = $comment;
76    }
77
78
79    /**
80     * Get a parameter attribute (e.g. name)
81     *
82     * @param string $param MIME type parameter
83     *
84     * @return string Attribute name
85     */
86    public static function getAttribute($param)
87    {
88        $tmp = explode('=', $param);
89        return trim($tmp[0]);
90    }
91
92
93    /**
94     * Get a parameter value
95     *
96     * @param string $param MIME type parameter
97     *
98     * @return string Value
99     */
100    public static function getValue($param)
101    {
102        $tmp = explode('=', $param, 2);
103        $value = $tmp[1];
104        $value = trim($value);
105        if ($value[0] == '"' && $value[strlen($value)-1] == '"') {
106            $value = substr($value, 1, -1);
107        }
108        $value = str_replace('\\"', '"', $value);
109        return $value;
110    }
111
112
113    /**
114     * Get a parameter comment
115     *
116     * @param string $param MIME type parameter
117     *
118     * @return string Parameter comment
119     * @see    hasComment()
120     */
121    public static function getComment($param)
122    {
123        $cs = strpos($param, '(');
124        if ($cs === false) {
125            return null;
126        }
127        $comment = substr($param, $cs);
128        return trim($comment, '() ');
129    }
130
131
132    /**
133     * Does this parameter have a comment?
134     *
135     * @param string $param MIME type parameter
136     *
137     * @return boolean true if $param has a comment, false otherwise
138     * @static
139     */
140    public static function hasComment($param)
141    {
142        if (strstr($param, '(')) {
143            return true;
144        }
145        return false;
146    }
147
148
149    /**
150     * Get a string representation of this parameter
151     *
152     * This function performs the oppsite of parse()
153     *
154     * @return string String representation of parameter
155     */
156    public function get()
157    {
158        $val = $this->name . '="' . str_replace('"', '\\"', $this->value) . '"';
159        if ($this->comment) {
160            $val .= ' (' . $this->comment . ')';
161        }
162        return $val;
163    }
164}
165?>