Completed
Branch 2.0.0 (814c19)
by Jimmy
03:05
created

CSV_Util   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 1
1
<?php
2
/**
3
 * Méthodes utilitaires pour les fichiers CSV.
4
 *
5
 * @author Eoxia <[email protected]>
6
 * @since 0.1.0
7
 * @version 1.0.0
8
 * @copyright 2015-2018 Eoxia
9
 * @package EO_Framework\Core\Util
10
 */
11
12
namespace eoxia;
13
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
if ( ! class_exists( '\eoxia\CSV_Util' ) ) {
19
20
	/**
21
	 * Méthodes utilitaires pour les fichiers CSV.
22
	 */
23
	class CSV_Util extends \eoxia\Singleton_Util {
24
		/**
25
		 * Le constructeur obligatoirement pour utiliser la classe \eoxia\Singleton_Util
26
		 *
27
		 * @since 0.1.0
28
		 * @version 1.0.0
29
		 *
30
		 * @return void
31
		 */
32
		protected function construct() {}
33
34
		/**
35
		 * Lit un fichier CSV et forme un tableau 2D selon $list_index
36
		 *
37
		 * @since 0.1.0
38
		 * @version 1.0.0
39
		 *
40
		 * @param string $csv_path   Le chemin vers le fichier .csv.
41
		 * @param array  $list_index Les index personnalisés.
42
		 * @return array 						 Le tableau 2D avec les données du csv
43
		 *
44
		 * @todo: Est-ce utile ? Utilisé par quel plugin ?
45
		 */
46
		public function read_and_set_index( $csv_path, $list_index = array() ) {
47
			if ( empty( $csv_path ) ) {
48
				return false;
49
			}
50
51
			$data = array();
52
			$csv_content = file( $csv_path );
53
			if ( ! empty( $csv_content ) ) {
54
				foreach ( $csv_content as $key => $line ) {
55
					if ( 0 !== $key ) {
56
						$data[ $key ] = str_getcsv( $line );
57
						foreach ( $data[ $key ] as $i => $entry ) {
58
							if ( ! empty( $list_index[ $i ] ) ) {
59
								$data[ $key ][ $list_index[ $i ] ] = $entry;
60
							}
61
						}
62
					}
63
				}
64
			}
65
66
			return $data;
67
		}
68
	}
69
} // End if().
70