ContentTypeHeader.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007 by Ivan Y. Khvostishkov                            *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: ContentTypeHeader.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     final class ContentTypeHeader
00017     {
00018         private $mediaType  = null;
00019         private $parameters = array();
00020         
00021         private $charset    = null; // reference
00022         
00026         public static function create()
00027         {
00028             return new self;
00029         }
00030         
00034         public function setMediaType($mediaType)
00035         {
00036             $this->mediaType = $mediaType;
00037             
00038             return $this;
00039         }
00040         
00041         public function getMediaType()
00042         {
00043             return $this->mediaType;
00044         }
00045         
00049         public function setParameter($attribute, $value)
00050         {
00051             $this->parameters[$attribute] = $value;
00052             
00053             return $this;
00054         }
00055         
00059         public function dropParameter($attribute)
00060         {
00061             if (!isset($this->parameters[$attribute]))
00062                 throw new MissingElementException();
00063             
00064             unset($this->parameters[$attribute]);
00065             
00066             return $this;
00067         }
00068         
00069         public function hasParameter($attribute)
00070         {
00071             return isset($this->parameters[$attribute]);
00072         }
00073         
00074         public function getParameter($attribute)
00075         {
00076             if (!isset($this->parameters[$attribute]))
00077                 throw new MissingElementException();
00078             
00079             return $this->parameters[$attribute];
00080         }
00081         
00085         public function setParametersList($parameters)
00086         {
00087             Assert::isArray($parameters);
00088             
00089             $this->parameters = $parameters;
00090             
00091             return $this;
00092         }
00093         
00094         public function getParametersList()
00095         {
00096             return $this->parameters;
00097         }
00098         
00099         public function getCharset()
00100         {
00101             return $this->charset;
00102         }
00103         
00107         public function setCharset($charset)
00108         {
00109             if (!$this->charset) {
00110                 $this->parameters['charset'] = $charset;
00111                 $this->charset = &$this->parameters['charset'];
00112             } else
00113                 $this->charset = $charset;
00114             
00115             return $this;
00116         }
00117         
00123         public function import($string)
00124         {
00125             $this->charset = null;
00126             $this->parameters = array();
00127             $matches = array();
00128             
00129             if (
00130                 preg_match(
00131                     '~^[\s\t]*([^/\s\t;]+/[^\s\t;]+)[\s\t]*(.*)$~',
00132                     $string, $matches
00133                 )
00134             ) {
00135                 $this->mediaType = $matches[1];
00136                 $remainingString = $matches[2];
00137                 
00138                 preg_match_all(
00139                     '~;[\s\t]*([^\s\t\=]+)[\s\t]*\=[\s\t]*' // 1: attribute
00140                     .'(?:([^"\s\t;]+)|(?:"(.*?(?<!\\\))"))' // 2 or 3: value
00141                     .'[\s\t]*~',
00142                     $remainingString, $matches
00143                 );
00144                     
00145                 foreach ($matches[1] as $i => $attribute) {
00146                     $attribute = strtolower($attribute);
00147                     
00148                     $value =
00149                         empty($matches[2][$i])
00150                         ? $matches[3][$i]
00151                         : $matches[2][$i];
00152                     
00153                     $this->parameters[$attribute] = $value;
00154                     
00155                     if ($attribute == 'charset')
00156                         // NOTE: reference
00157                         $this->charset = &$this->parameters[$attribute];
00158                 }
00159             }
00160             
00161             return $this;
00162         }
00163         
00164         public function toString()
00165         {
00166             $parts = array($this->mediaType);
00167             
00168             foreach ($this->parameters as $attribute => $value) {
00169                 $parts[] = $attribute.'="'.$value.'"';
00170             }
00171             
00172             return implode('; ', $parts);
00173         }
00174     }
00175 ?>

Generated on Sun Dec 9 21:56:24 2007 for onPHP by  doxygen 1.5.4