]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - extlib/MIME/Type/Parameter.php
Merge branch '0.8.x' of git@gitorious.org:+laconica-developers/laconica/dev into...
[quix0rs-gnu-social.git] / extlib / MIME / Type / Parameter.php
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 2007/03/25 10:10:21 cweiske Exp $
20
21 /**
22  * Class for working with MIME type parameters
23  *
24  * @version 1.2.0
25  * @package MIME_Type
26  * @author Ian Eure <ieure@php.net>
27  */
28 class 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         $comment = '';
74         $param   = MIME_Type::stripComments($param, $comment);
75         $this->name    = $this->getAttribute($param);
76         $this->value   = $this->getValue($param);
77         $this->comment = $comment;
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, 2);
105         $value = $tmp[1];
106         $value = trim($value);
107         if ($value[0] == '"' && $value[strlen($value)-1] == '"') {
108             $value = substr($value, 1, -1);
109         }
110         $value = str_replace('\\"', '"', $value);
111         return $value;
112     }
113
114
115     /**
116      * Get a parameter comment
117      *
118      * @param  string $param MIME type parameter
119      * @return string Parameter comment
120      * @see getComment()
121      * @static
122      */
123     function getComment($param)
124     {
125         $cs = strpos($param, '(');
126         $comment = substr($param, $cs);
127         return trim($comment, '() ');
128     }
129
130
131     /**
132      * Does this parameter have a comment?
133      *
134      * @param  string  $param MIME type parameter
135      * @return boolean true if $param has a comment, false otherwise
136      * @static
137      */
138     function hasComment($param)
139     {
140         if (strstr($param, '(')) {
141             return true;
142         }
143         return false;
144     }
145
146
147     /**
148      * Get a string representation of this parameter
149      *
150      * This function performs the oppsite of parse()
151      *
152      * @return string String representation of parameter
153      */
154     function get()
155     {
156         $val = $this->name . '="' . str_replace('"', '\\"', $this->value) . '"';
157         if ($this->comment) {
158             $val .= ' (' . $this->comment . ')';
159         }
160         return $val;
161     }
162 }
163 ?>