8000 Added support for multi-field validation · ReservedDeveloper/csv-bundle@dcac780 · GitHub
[go: up one dir, main page]

Skip to content

Commit dcac780

Browse files
author
Daniel Lakes
committed
Added support for multi-field validation
* Updated notes re:extension of validateHeader/validateRow methods * moved validation of each data row to separate method, called from validateRow method * added method for validation of multi-fields (ex: 3 part date field)
1 parent c5aecf9 commit dcac780

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

FileReader/Validator/AbstractCsvFileValidator.php

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ abstract class AbstractCsvFileValidator
2424
/**
2525
* validate the header row
2626
*
27+
* as header rows do not have the same
28+
* nuance as data rows, decided to cast as
29+
* final to help ensure consistent return type
30+
*
2731
* @param array $headerRow
2832
*
29-
* @return ValidatorResponse
33+
* @return ValidatorResponse $response
3034
*/
3135
public final function validateHeader(array $headerRow)
3236
{
@@ -51,14 +55,46 @@ abstract protected function validateHeaderField($fieldValue);
5155

5256
/**
5357
* validate non-header/data rows
58+
* Checks for validation on each field, then checks
59+
* for validation on multiple/joined fields
60+
*
61+
* Decided not to cast as final in the event that
62+
* a validation situation comes up not covered by one
63+
* of the below called methods
64+
*
65+
* As such, any classes extending this method should
66+
* pay close attention to the method return signature
67+
* for compatibility purposes
68+
*
69+
*
5470
*
5571
* @param array $rowData - the array of row data to be checked
5672
*
57-
* @return ValidatorResponse
73+
* @return ValidatorResponse $response
5874
*/
5975
public function validateDataRow(array $rowData)
6076
{
6177
$response = new ValidatorResponse();
78+
79+
$response = $this->validateDataRowFields($rowData, $response);
80+
$response = $this->validateDataRowMultiFields($rowData, $response);
81+
82+
return $response;
83+
}
84+
85+
/**
86+
* validates each pair in the provided $rowData
87+
*
88+
* @param array $rowData
89+
* @param ValidatorResponse $response - a preexisting response object to modify, if any
90+
* @return ValidatorResponse $response
91+
*/
92+
protected final function validateDataRowFields(array $rowData, ValidatorResponse $response = null)
93+
{
94+
$response = $response
95+
? $response
96+
: new ValidatorResponse();
97+
6298
foreach ($rowData as $dataFieldKey => $dataFieldValue) {
6399
if (!$this->validateDataField($dataFieldKey, $dataFieldValue)) {
64100
$response->addErrorForField($dataFieldKey, new InvalidDataFieldException($dataFieldKey, $dataFieldValue));
@@ -68,6 +104,14 @@ public function validateDataRow(array $rowData)
68104
return $response;
69105
}
70106

107+
/**
108+
* @param array $rowData
109+
* @param ValidatorResponse $response
110+
*
111+
* @return ValidatorResponse $response
112+
*/
113+
abstract protected function validateDataRowMultiFields(array $rowData, ValidatorResponse $response = null);
114+
71115
/**
72116
* validates the given field for expected format
73117
*

0 commit comments

Comments
 (0)
0