RSI helpers  0.1
RSI helpers
Csv.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Wrapper;
4 
5 /**
6  * CSV wrapper.
7  */
8 class Csv{
9 
10  public $keys = null;
11 
12  protected $_handle = null;
13  protected $_delimiter = null;
14  protected $_enclosure = null;
15  protected $_escape = null;
16 
17  public function __construct($filename,$mode = 'r',$delimiter = ',',$enclosure = '"',$escape = '\\'){
18  $this->_handle = fopen($filename,$mode);
19  $this->_delimiter = $delimiter;
20  $this->_enclosure = $enclosure;
21  $this->_escape = $escape;
22  }
23 
24  public function __destruct(){
25  $this->close();
26  }
27  /**
28  * Read a single record from the file.
29  * @param string $filler Filler to use if record is shorter than keys (if any).
30  * @return array Record, ordered and with keys if any.
31  */
32  public function get($filler = null){
33  $data = fgetcsv($this->_handle,0,$this->_delimiter,$this->_enclosure,$this->_escape);
34  return $this->keys && ($data !== false) ? \Rsi\Record::combine($this->keys,$data,$filler) : $data;
35  }
36  /**
37  * Write a single record to the file.
38  * @param array $fields Values.
39  * @param string $filler Filler to use if array of values does not contain all keys (if any).
40  * @return int Length of the written string or FALSE on failure.
41  */
42  public function put($fields,$filler = null){
43  if($this->keys){
44  $record = [];
45  foreach($this->keys as $key) $record[] = \Rsi\Record::get($fields,$key,$filler);
46  $fields = $record;
47  }
48  return fputcsv($this->_handle,$fields,$this->_delimiter,$this->_enclosure,$this->_escape);
49  }
50  /**
51  * Read the keys from the file and store them.
52  * @return array
53  */
54  public function getKeys(){
55  $this->keys = null;
56  return $this->keys = $this->get();
57  }
58  /**
59  * Write the keys to the file and store them.
60  * @param array $keys
61  */
62  public function putKeys($keys){
63  $this->keys = null;
64  $this->put($keys);
65  $this->keys = $keys;
66  }
67  /**
68  * Return all remaining records from the file.
69  * @param string $filler Filler to use if record is shorter than keys (if any).
70  * @return array Array of records.
71  */
72  public function getAll($filler = null){
73  $records = [];
74  while(!$this->eof()) if(($record = $this->get($filler)) !== false) $records[] = $record;
75  return $records;
76  }
77  /**
78  * Close the file.
79  */
80  public function close(){
81  if($this->_handle) fclose($this->_handle);
82  $this->_handle = null;
83  }
84 
85  public function __call($func_name,$params){
86  return call_user_func_array('f' . \Rsi\Str::snake($func_name),array_merge([$this->_handle],$params));
87  }
88 
89 }
__call($func_name, $params)
Definition: Csv.php:85
getAll($filler=null)
Return all remaining records from the file.
Definition: Csv.php:72
putKeys($keys)
Write the keys to the file and store them.
Definition: Csv.php:62
__destruct()
Definition: Csv.php:24
close()
Close the file.
Definition: Csv.php:80
__construct($filename, $mode='r', $delimiter=',', $enclosure='"',$escape = '\)
Definition: Csv.php:17
CSV wrapper.
Definition: Csv.php:8
static combine($keys, $values, $filler=null)
Creates an array by using one array for keys and another for its values.
Definition: Record.php:251
Definition: Color.php:3
static snake($str, $delimiter='_')
Converts a CamelCased string to snake_case.
Definition: Str.php:193
getKeys()
Read the keys from the file and store them.
Definition: Csv.php:54
put($fields, $filler=null)
Write a single record to the file.
Definition: Csv.php:42