]> git.mxchange.org Git - core.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Thu, 26 Aug 2021 02:11:07 +0000 (04:11 +0200)
committerRoland Häder <roland@mxchange.org>
Thu, 26 Aug 2021 02:12:33 +0000 (04:12 +0200)
- CsvInputFile->readCsvFileLine() now accepts optional 2nd parameter
  $expectedMatches
- it also may throw 2 different exceptions on you

Signed-off-by: Roland Häder <roland@mxchange.org>
framework/main/classes/file_directories/text/input/csv/class_CsvInputFile.php
framework/main/interfaces/io/file/csv/class_CsvInputStreamer.php

index 51c04439c437570d3d0a97566f734f7356e9ee8d..b9c5f16284f5db06b2df52c18c4eaf4f519328a3 100644 (file)
@@ -7,7 +7,9 @@ use Org\Mxchange\CoreFramework\Filesystem\Text\BaseInputTextFile;
 use Org\Mxchange\CoreFramework\Stream\Filesystem\CsvInputStreamer;
 
 // Import SPL stuff
+use \InvalidArgumentException;
 use \SplFileInfo;
+use \UnexpectedValueException;
 
 /**
  * A CSV file input class for writing CSV files
@@ -65,19 +67,38 @@ class CsvInputFile extends BaseInputTextFile implements CsvInputStreamer {
         * column separators will be parsed or they may be interpreted incorrectly.
         *
         * @param       $columnSeparator        Character to use separting columns
+        * @param       $expectedMatches        Expected matches, 0 is default and means flexible
         * @return      $lineArray                      An indexed array with the read line
+        * @throws      InvalidArgumentException        If a parameter is invalid
+        * @throws      UnexpectedValueException        If the array count is not matching expected count
         */
-       public function readCsvFileLine (string $columnSeparator) {
-               // Read raw line
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] columnSeparator=%s - CALLED!', __METHOD__, __LINE__, $columnSeparator));
-               $data = $this->getPointerInstance()->readLine();
+       public function readCsvFileLine (string $columnSeparator, int $expectedMatches = 0) {
+               // Validate parameter
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] columnSeparator=%s,expectedMatches=%d - CALLED!', __METHOD__, __LINE__, $columnSeparator, $expectedMatches));
+               if (strlen($columnSeparator) === 0) {
+                       // No empty column separator
+                       throw new InvalidArgumentException('columnSeparator cannot be empty.');
+               } elseif ($expectedMatches < 0) {
+                       // Below zero is not valid
+                       throw new InvalidArgumentException(sprintf('expectedMatches=%d is below zero', $expectedMatches));
+               }
+
+               // Read raw line and trim anything unwanted away
+               $data = trim($this->getPointerInstance()->readLine());
 
                // Parse data
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] data()=%d', __METHOD__, __LINE__, strlen($data)));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] data(%d)=%s', __METHOD__, __LINE__, strlen($data), $data));
                $lineArray = $this->parseDataToIndexedArray($data, $columnSeparator);
 
+               // Is the expected count found?
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] expectedMatches=%d,lineArray()=%d', __METHOD__, __LINE__, $expectedMatches, count($lineArray)));
+               if (($expectedMatches > 0) && (count($lineArray) !== $expectedMatches)) {
+                       // Invalid line found as strict count matching is requested
+                       throw new UnexpectedValueException(sprintf('lineArray()=%d has not expected count %d', count($lineArray), $expectedMatches));
+               }
+
                // Return it
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] lineArray()=%d - EXIT!', __METHOD__, __LINE__, count($lineArray)));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] lineArray()=%d - EXIT!', __METHOD__, __LINE__, count($lineArray)));
                return $lineArray;
        }
 
index b5be1e3834a18965b444fc2d368d98aa13a1cd8a..3f383d65c9a033f0b628a86c067878cb51589069 100644 (file)
@@ -32,8 +32,10 @@ interface CsvInputStreamer extends FileInputStreamer {
         * Reads a line from CSV file and returns it as an indexed array
         *
         * @param       $columnSeparator        Character to use separting columns
+        * @param       $expectedMatches        Expected matches, 0 is default and means flexible
         * @return      $lineArray                      An index array with the read line
+        * @throws      InvalidArgumentException        If a parameter is invalid
         */
-       function readCsvFileLine (string $columnSeparator);
+       function readCsvFileLine (string $columnSeparator, int $expectedMatches = 0);
 
 }