diff --git a/src/A.php b/src/A.php index 4c232a8..3123a9d 100644 --- a/src/A.php +++ b/src/A.php @@ -36,6 +36,38 @@ class A { return false; } + /** + * s'assurer que $array est un tableau de $size éléments, en complétant avec + * des occurrences de $default si nécessaire + * + * @return bool true si le tableau a été modifié, false sinon + */ + static final function ensure_size(?array &$array, int $size, $default=null): bool { + $modified = false; + if ($array === null) { + $array = []; + $modified = true; + } + if ($size < 0) return $modified; + $count = count($array); + if ($count == $size) return $modified; + if ($count < $size) { + # agrandir le tableau + while ($count++ < $size) { + $array[] = $default; + } + return true; + } + # rétrécir le tableau + $tmparray = []; + foreach ($array as $key => $value) { + if ($size-- == 0) break; + $tmparray[$key] = $value; + } + $array = $tmparray; + return true; + } + static function merge(&$dest, ...$merges): void { self::ensure_narray($dest); $dest = cl::merge($dest, ...$merges); diff --git a/src/file/csv/AbstractReader.php b/src/file/csv/AbstractReader.php index 7c3db9f..8f502b9 100644 --- a/src/file/csv/AbstractReader.php +++ b/src/file/csv/AbstractReader.php @@ -1,6 +1,7 @@ headers)); $row = array_combine($this->headers, $row); return true; }