1: <?php
2: /**
3: * Pry Framework
4: *
5: * LICENSE
6: *
7: * This source file is subject to the new BSD license that is bundled
8: * with this package in the file LICENSE.txt.
9: *
10: * @version $Revision: 276 $
11: */
12:
13: /**
14: * Gestion d'écriture de fichier csv
15: * @category Pry
16: * @package File
17: * @version 1.1.0
18: * @author Olivier ROGER <oroger.fr>
19: *
20: */
21: class File_FileCSV extends File_FileManager
22: {
23: /**
24: * Caractère de séparation
25: * @var string
26: */
27: private $glue;
28:
29: /**
30: * Nobre de colonne du fichier
31: * @var unknown_type
32: */
33: private $nbCols;
34:
35: /**
36: * Constructeur
37: * @param string $file Chemin vers le fichier
38: * @param string $glue Caractère de séparation
39: *
40: */
41: public function __construct($file,$glue=';')
42: {
43: parent::__construct($file);
44: $this->open(File_FileManager::WRITE);
45: $this->glue = $glue;
46: }
47:
48: /**
49: * Ajoute les colonnes du fichiers
50: * @param array $cols
51: * @return void
52: */
53: public function addColumns(array $cols)
54: {
55: if(!is_array($cols))
56: throw new InvalidArgumentException('Argument de type Array attendu pour les colonnes');
57:
58: $this->nbCols = count($cols);
59:
60: $this->writeLine(implode($this->glue,$cols));
61: }
62:
63: /**
64: * Ajoute une ligne vide
65: * @return void
66: */
67: public function addBlankLine()
68: {
69: $line = str_repeat(';',$this->nbCols-1);
70: $this->writeLine($line);
71: }
72:
73: /**
74: * Ajoute une ligne
75: * @param mixed $data
76: * @return void
77: */
78: public function addLine($data)
79: {
80: if(!is_array($data))
81: $data = explode($this->glue,$data);
82:
83: if(count($data)!=$this->nbCols)
84: throw new UnexpectedValueException('Le nombre de colonnes ne correspond pas au nombre d\'entête');
85:
86: $this->writeLine(implode($this->glue,$data));
87: }
88: }