# Csv.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /***************************************************************************
    00003  *   Copyright (C) 2008 by Michael V. Tchervyakov                          *
    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
    00017     final class Csv
    00018     {
    00019         const SEPARATOR                 = "\x2C";
    00020         const QUOTE                     = "\x22";
    00021         const CRLF                      = "\x0D\x0A";
    00022         const QUOTE_REQUIRED_PATTERN    = "/(\x2C|\x22|\x0D|\x0A)/";
    00023
    00024         private $separator              = self::SEPARATOR;
    00025
    00026         private $header = false;
    00027         private $data   = array();
    00028
    00032         public static function create($header = false)
    00033         {
    00034             return new self($header);
    00035         }
    00036
    00037         public function __construct($header = false)
    00038         {
    00039             $this->header = (true === $header);
    00040         }
    00041
    00042         public function getArray()
    00043         {
    00044             return $this->data;
    00045         }
    00046
    00050         public function setArray($array)
    00051         {
    00052             Assert::isArray($array);
    00053
    00054             $this->data = $array;
    00055
    00056             return $this;
    00057         }
    00058
    00062         public function setSeparator($separator)
    00063         {
    00064             $this->separator = $separator;
    00065
    00066             return $this;
    00067         }
    00068
    00069         public function parse($rawData)
    00070         {
    00071             throw new UnimplementedFeatureException('is not implemented yet');
    00072         }
    00073
    00074         public function render($forceQuotes = false)
    00075         {
    00076             Assert::isNotNull($this->separator);
    00077
    00078             $csvString  = null;
    00079
    00080             foreach ($this->data as $row) {
    00081                 Assert::isArray($row);
    00082
    00083                 $rowString = null;
    00084
    00085                 foreach ($row as $value) {
    00086                     if (
    00087                         $forceQuotes
    00088                         || preg_match(self::QUOTE_REQUIRED_PATTERN, $value)
    00089                     )
    00090                         $value =
    00091                             self::QUOTE
    00092                             .mb_ereg_replace(
    00093                                 self::QUOTE,
    00094                                 self::QUOTE.self::QUOTE,
    00095                                 $value
    00096                             )
    00097                             .self::QUOTE;
    00098
    00099                     $rowString .=
    00100                         (
    00101                             $rowString
    00102                                 ? $this->separator
    00103                                 : null
    00104                         )
    00105                         .$value;
    00106                 }
    00107
    00108                 $csvString .= $rowString.self::CRLF;
    00109             }
    00110
    00111             return $csvString;
    00112         }
    00113
    00117         public function getContentTypeHeader()
    00118         {
    00119             return
    00120                 ContentTypeHeader::create()->
    00121                 setParameter(
    00122                     'header',
    00123                     $this->header
    00124                         ? 'present'
    00125                         : 'absent'
    00126                 )->
    00127                 setMediaType('text/csv');
    00128         }
    00129     }
    00130 ?>