| 1 |
<?php |
| 2 |
/** |
| 3 |
* TablePress Table Import Base Class |
| 4 |
* |
| 5 |
* @package TablePress |
| 6 |
* @subpackage Export/Import |
| 7 |
* @author Tobias Bäthge |
| 8 |
* @since 2.0.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
// Prohibit direct script loading. |
| 12 |
defined( 'ABSPATH' ) || die( 'No direct script access allowed!' ); |
| 13 |
|
| 14 |
/** |
| 15 |
* TablePress Table Import Base Class |
| 16 |
* |
| 17 |
* @package TablePress |
| 18 |
* @subpackage Export/Import |
| 19 |
* @author Tobias Bäthge |
| 20 |
* @since 2.0.0 |
| 21 |
*/ |
| 22 |
abstract class TablePress_Import_Base { |
| 23 |
|
| 24 |
/** |
| 25 |
* Makes sure that a passed table array is rectangular with all rows having the same number of columns (the highest one that is found). |
| 26 |
* |
| 27 |
* This function uses call by reference to save PHP memory on large arrays. |
| 28 |
* |
| 29 |
* @since 1.0.0 |
| 30 |
* @since 2.0.0 The $an_array parameter is handled by reference. |
| 31 |
* |
| 32 |
* @param array<int, array<int, mixed>> $an_array Two-dimensional array to be padded. |
| 33 |
*/ |
| 34 |
public function pad_array_to_max_cols( array &$an_array ): void { |
| 35 |
$max_columns = $this->count_max_columns( $an_array ); |
| 36 |
// Extend the array to at least one column. |
| 37 |
$max_columns = max( 1, $max_columns ); |
| 38 |
array_walk( |
| 39 |
$an_array, |
| 40 |
static function ( array &$row, int $col_idx ) use ( $max_columns ): void { |
| 41 |
$row = array_pad( $row, $max_columns, '' ); |
| 42 |
} |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Get the highest number of columns in the rows. |
| 48 |
* |
| 49 |
* @since 1.0.0 |
| 50 |
* |
| 51 |
* @param array<int, array<int, mixed>> $an_array Two-dimensional array. |
| 52 |
* @return int Highest number of columns in the rows of the array. |
| 53 |
*/ |
| 54 |
protected function count_max_columns( array $an_array ): int { |
| 55 |
$max_columns = 0; |
| 56 |
foreach ( $an_array as $row_idx => $row ) { |
| 57 |
$num_columns = count( $row ); |
| 58 |
$max_columns = max( $num_columns, $max_columns ); |
| 59 |
} |
| 60 |
return $max_columns; |
| 61 |
} |
| 62 |
|
| 63 |
} // class TablePress_Import_Base |
| 64 |
|