Created
October 31, 2016 13:25
-
-
Save davialexandre/6df1ac26450563cf59ac294a9bc5987e to your computer and use it in GitHub Desktop.
Visitor pattern example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CSVAbsenceTypeCleaner extends CSVCleanerVisitor { | |
public function visit(array $row) { | |
$this->deleteRecord('HRAbsenceType', ['name' => $row['name']]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CSVAbsenceTypeImporter extends CSVImporterVisitor { | |
function importRecord(array $row) { | |
// code specific to import absence type here | |
} | |
public function visit(array $row) { | |
$this->importRecord($row); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class CSVCleanerVisitor implements CSVProcessingVisitor { | |
protected function deleteRecord($entity, $searchParams) { | |
//code to delete record here | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class CSVImporterVisitor implements CSVProcessingVisitor { | |
abstract function importRecord(array $row); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface CSVProcessingVisitor { | |
public function visit(array $row); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CSVProcessor { | |
private $fileHandler; | |
public function __construct(SplFileObject $file) { | |
$this->fileHandler = $file; | |
} | |
public function process(CSVProcessingVisitor $visitor) { | |
$header = null; | |
while (!$this->fileHandler->eof()) { | |
$row = $this->fileHandler->fgetcsv(); | |
if ($header === null) { | |
$header = $row; | |
continue; | |
} | |
$row = array_combine($header, $row); | |
$visitor->visit($row); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$file = new SplFileObject('absence_type.csv'); | |
$processor = new CSVProcessor($file); | |
// Clean things | |
$absenceTypeCleaner = new CSVAbsenceTypeCleaner(); | |
$processor->process($absenceTypeCleaner); | |
//Import things | |
$absenceTypeImporter = new CSVAbsenceTypeImporter(); | |
$processor->process($absenceTypeImporter); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment