HeaderParser.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007 by Anton E. Lebedevich                             *
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 HeaderParser
00016     {
00017         private $headers        = array();
00018         private $currentHeader  = null;
00019         
00020         public static function create()
00021         {
00022             return new self;
00023         }
00024         
00029         public function parse($data)
00030         {
00031             $lines = explode("\n", $data);
00032             
00033             foreach ($lines as $line) {
00034                 $this->doLine($line);
00035             }
00036             
00037             return $this;
00038         }
00039         
00040         public function doLine($line)
00041         {
00042             $line = trim($line, "\r\n");
00043             $matches = array();
00044 
00045             if (preg_match("/^([\w-]+):\s+(.+)/", $line, $matches)) {
00046                 
00047                 $name = strtolower($matches[1]);
00048                 $value = $matches[2];
00049                 $this->currentHeader = $name;
00050 
00051                 if (isset($this->headers[$name])) {
00052                     if (!is_array($this->headers[$name])) {
00053                         $this->headers[$name] = array($this->headers[$name]);
00054                     }
00055                     $this->headers[$name][] = $value;
00056                 } else {
00057                     $this->headers[$name] = $value;
00058                 }
00059                 
00060             } elseif (
00061                 preg_match("/^\s+(.+)$/", $line, $matches)
00062                 && $this->currentHeader !== null
00063             ) {
00064                 if (is_array($this->headers[$this->currentHeader])) {
00065                     $lastKey = count($this->headers[$this->currentHeader]) - 1;
00066                     $this->headers[$this->currentHeader][$lastKey]
00067                         .= $matches[1];
00068                 } else {
00069                     $this->headers[$this->currentHeader] .= $matches[1];
00070                 }
00071             }
00072             
00073             return $this;
00074         }
00075         
00079         public function getHeaders()
00080         {
00081             return $this->headers;
00082         }
00083         
00084         public function hasHeader($name)
00085         {
00086             return isset($this->headers[strtolower($name)]);
00087         }
00088         
00089         public function getHeader($name)
00090         {
00091             return $this->headers[strtolower($name)];
00092         }
00093     }
00094 ?>