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 
00015     final class ContentTypeHeader
00016     {
00017         private $mediaType  = null;
00018         private $parameters = array();
00019         
00020         private $charset    = null; // reference
00021         
00025         public static function create()
00026         {
00027             return new self;
00028         }
00029         
00033         public function setMediaType($mediaType)
00034         {
00035             $this->mediaType = $mediaType;
00036             
00037             return $this;
00038         }
00039         
00040         public function getMediaType()
00041         {
00042             return $this->mediaType;
00043         }
00044         
00048         public function setParameter($attribute, $value)
00049         {
00050             $this->parameters[$attribute] = $value;
00051             
00052             return $this;
00053         }
00054         
00058         public function dropParameter($attribute)
00059         {
00060             if (!isset($this->parameters[$attribute]))
00061                 throw new MissingElementException();
00062             
00063             unset($this->parameters[$attribute]);
00064             
00065             return $this;
00066         }
00067         
00068         public function hasParameter($attribute)
00069         {
00070             return isset($this->parameters[$attribute]);
00071         }
00072         
00073         public function getParameter($attribute)
00074         {
00075             if (!isset($this->parameters[$attribute]))
00076                 throw new MissingElementException();
00077             
00078             return $this->parameters[$attribute];
00079         }
00080         
00084         public function setParametersList($parameters)
00085         {
00086             Assert::isArray($parameters);
00087             
00088             $this->parameters = $parameters;
00089             
00090             return $this;
00091         }
00092         
00093         public function getParametersList()
00094         {
00095             return $this->parameters;
00096         }
00097         
00098         public function getCharset()
00099         {
00100             return $this->charset;
00101         }
00102         
00106         public function setCharset($charset)
00107         {
00108             if (!$this->charset) {
00109                 $this->parameters['charset'] = $charset;
00110                 $this->charset = &$this->parameters['charset'];
00111             } else
00112                 $this->charset = $charset;
00113             
00114             return $this;
00115         }
00116         
00122         public function import($string)
00123         {
00124             $this->charset = null;
00125             $this->parameters = array();
00126             $matches = array();
00127             
00128             if (
00129                 preg_match(
00130                     '~^[\s\t]*([^/\s\t;]+/[^\s\t;]+)[\s\t]*(.*)$~',
00131                     $string, $matches
00132                 )
00133             ) {
00134                 $this->mediaType = $matches[1];
00135                 $remainingString = $matches[2];
00136                 
00137                 preg_match_all(
00138                     '~;[\s\t]*([^\s\t\=]+)[\s\t]*\=[\s\t]*' // 1: attribute
00139                     .'(?:([^"\s\t;]+)|(?:"(.*?(?<!\\\))"))' // 2 or 3: value
00140                     .'[\s\t]*~',
00141                     $remainingString, $matches
00142                 );
00143                     
00144                 foreach ($matches[1] as $i => $attribute) {
00145                     $attribute = strtolower($attribute);
00146                     
00147                     $value =
00148                         empty($matches[2][$i])
00149                         ? $matches[3][$i]
00150                         : $matches[2][$i];
00151                     
00152                     $this->parameters[$attribute] = $value;
00153                     
00154                     if ($attribute == 'charset')
00155                         // NOTE: reference
00156                         $this->charset = &$this->parameters[$attribute];
00157                 }
00158             }
00159             
00160             return $this;
00161         }
00162         
00163         public function toString()
00164         {
00165             $parts = array($this->mediaType);
00166             
00167             foreach ($this->parameters as $attribute => $value) {
00168                 $parts[] = $attribute.'="'.$value.'"';
00169             }
00170             
00171             return implode('; ', $parts);
00172         }
00173     }
00174 ?>