smartbooster /
etl-bundle
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Smart\EtlBundle\Utils; |
||
| 4 | |||
| 5 | use Smart\EtlBundle\Exception\Utils\ArrayUtils\MultiArrayHeaderConsistencyException; |
||
| 6 | use Smart\EtlBundle\Exception\Utils\ArrayUtils\MultiArrayNbMaxRowsException; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * @author Mathieu Ducrot <[email protected]> |
||
| 10 | */ |
||
| 11 | class ArrayUtils |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Clean data from an array |
||
| 15 | * - remove null value and empty string |
||
| 16 | */ |
||
| 17 | public static function removeEmpty(array $array): array |
||
| 18 | { |
||
| 19 | return array_filter($array, function ($item) { |
||
| 20 | return $item !== null && (!is_string($item) || $item !== ''); |
||
| 21 | }); |
||
| 22 | } |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Convert and clean data from string to array |
||
| 26 | */ |
||
| 27 | public static function getArrayFromString(?string $string): array |
||
| 28 | { |
||
| 29 | $toReturn = explode(PHP_EOL, $string); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 30 | $toReturn = array_map(function ($row) { |
||
| 31 | return trim($row); |
||
| 32 | }, $toReturn); |
||
| 33 | |||
| 34 | return array_unique(self::removeEmpty($toReturn)); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Convert and clean data from string to a multidimensional array |
||
| 39 | * |
||
| 40 | * @param array $options list all options to configure the transform behavior. Current available options : |
||
| 41 | * - delimiter (string) delimiter used to explode the row data |
||
| 42 | * - nb_max_row (integer) maximum limit number of row allowed in the data content to import |
||
| 43 | * - fix_header_consistency (boolean) allow to fix header consistency error |
||
| 44 | * @throws MultiArrayNbMaxRowsException |
||
| 45 | * @throws MultiArrayHeaderConsistencyException |
||
| 46 | */ |
||
| 47 | public static function getMultiArrayFromString(string $string, array $header, array $options = []): array |
||
| 48 | { |
||
| 49 | $nbRows = StringUtils::countRows($string); |
||
| 50 | if (isset($options['nb_max_row']) && $nbRows > $options['nb_max_row']) { |
||
| 51 | throw new MultiArrayNbMaxRowsException($options['nb_max_row'], $nbRows); |
||
| 52 | } |
||
| 53 | |||
| 54 | $toReturn = self::getArrayFromString($string); |
||
| 55 | $nbHeader = count($header); |
||
| 56 | $headerConsistencyError = []; |
||
| 57 | |||
| 58 | // options config |
||
| 59 | $delimiterOption = $options['delimiter'] ?? ','; |
||
| 60 | $fixHeaderConsistencyOption = (isset($options['fix_header_consistency']) && $options['fix_header_consistency']); |
||
| 61 | |||
| 62 | foreach ($toReturn as $key => $row) { |
||
| 63 | $rowData = str_getcsv($row, $delimiterOption); |
||
| 64 | |||
| 65 | // @todo extract in method mapTrimNull |
||
| 66 | $rowData = array_map(function ($data) { |
||
| 67 | if ($data !== null) { |
||
| 68 | $data = trim($data); |
||
| 69 | |||
| 70 | if ($data === '') { |
||
| 71 | $data = null; |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | return $data; |
||
| 76 | }, $rowData); |
||
| 77 | |||
| 78 | $validRowConsistency = true; |
||
| 79 | if ($fixHeaderConsistencyOption) { |
||
| 80 | // @todo extract in method syncHeader |
||
| 81 | // fill missing field on the row with null values |
||
| 82 | $rowData = array_pad($rowData, $nbHeader, null); |
||
| 83 | // slice extra data on the row |
||
| 84 | $rowData = array_slice($rowData, 0, $nbHeader); |
||
| 85 | } else { |
||
| 86 | if (count($rowData) != $nbHeader) { |
||
| 87 | $headerConsistencyError[] = $key + 1; |
||
| 88 | $validRowConsistency = false; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | // combine to form the multidimensional array |
||
| 93 | if ($validRowConsistency) { |
||
| 94 | $toReturn[$key] = array_combine($header, $rowData); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | if (count($headerConsistencyError) > 0) { |
||
| 99 | throw new MultiArrayHeaderConsistencyException($headerConsistencyError); |
||
| 100 | } |
||
| 101 | |||
| 102 | return $toReturn; |
||
| 103 | } |
||
| 104 | } |
||
| 105 |