| 1 | <?php |
||
| 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 |