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